Class: ExtendedYAML

Inherits:
Object
  • Object
show all
Defined in:
lib/extended_yaml.rb,
lib/extended_yaml/version.rb,
lib/extended_yaml/deep_merge.rb

Defined Under Namespace

Modules: DeepMerge

Constant Summary collapse

VERSION =
"0.2.3"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file, key: 'extends') ⇒ ExtendedYAML

Returns a new instance of ExtendedYAML.



16
17
18
# File 'lib/extended_yaml.rb', line 16

def initialize(file, key: 'extends')
  @file, @key = file, key
end

Instance Attribute Details

#fileObject (readonly)

Returns the value of attribute file.



7
8
9
# File 'lib/extended_yaml.rb', line 7

def file
  @file
end

#keyObject (readonly)

Returns the value of attribute key.



7
8
9
# File 'lib/extended_yaml.rb', line 7

def key
  @key
end

Class Method Details

.load(file, key: 'extends') ⇒ Hash, Array

Returns the parsed YAML.

Parameters:

  • file (String)

    path to YAML file

  • key (String) (defaults to: 'extends')

    YAML key to use for loading files

Returns:

  • (Hash, Array)

    the parsed YAML



12
13
14
# File 'lib/extended_yaml.rb', line 12

def self.load(file, key: 'extends')
  new(file, key: key).result
end

Instance Method Details

#evaluateString

Returns the YAML string, with evaluated and ERB.

Returns:

  • (String)

    the YAML string, with evaluated and ERB



34
35
36
# File 'lib/extended_yaml.rb', line 34

def evaluate
  ERB.new(File.read file).result
end

#resultHash, ...

Returns the parsed YAML.

Returns:

  • (Hash, Array, nil)

    the parsed YAML



21
22
23
24
25
26
27
28
29
30
31
# File 'lib/extended_yaml.rb', line 21

def result
  # ref: https://bugs.ruby-lang.org/issues/17866
  begin
    # ruby >= 3.1 / psych >= 4
    data = ::YAML.unsafe_load evaluate
  rescue NoMethodError
    # ruby < 3.1 / psych < 4
    data = ::YAML.load evaluate
  end
  data ? resolve_extends(data) : nil
end