Class: ReVIEW::YAMLLoader

Inherits:
Object show all
Defined in:
lib/review/yamlloader.rb

Instance Method Summary collapse

Constructor Details

#initializeYAMLLoader

Returns a new instance of YAMLLoader.



5
6
# File 'lib/review/yamlloader.rb', line 5

def initialize
end

Instance Method Details

#load_file(yamlfile) ⇒ Object

load YAML files

‘inherit: [3.yml, 6.yml]` in 7.yml; `inherit: [1.yml, 2.yml]` in 3.yml; `inherit: [4.yml, 5.yml]` in 6.yml

=> 7.yml > 6.yml > 5.yml > 4.yml > 3.yml > 2.yml > 1.yml


13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/review/yamlloader.rb', line 13

def load_file(yamlfile)
  file_queue = [File.expand_path(yamlfile)]
  loaded_files = {}
  yaml = {}

  loop do
    # Check exit condition
    if file_queue.empty?
      return yaml
    end

    current_file = file_queue.shift
    current_yaml = YAML.load_file(current_file)
    yaml = current_yaml.deep_merge(yaml)

    if yaml.key?('inherit')
      buf = []
      yaml['inherit'].reverse_each do |item|
        inherit_file = File.expand_path(item, File.dirname(yamlfile))

        # Check loop
        if loaded_files[inherit_file]
          raise "Found circular YAML inheritance '#{inherit_file}' in #{yamlfile}."
        end

        loaded_files[inherit_file] = true
        buf << inherit_file
      end
      yaml.delete('inherit')
      file_queue = buf + file_queue
    end
  end
end