Class: Bhf::Settings::YAMLParser

Inherits:
Object
  • Object
show all
Defined in:
lib/bhf/settings/yaml_parser.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(roles_array, area = nil) ⇒ YAMLParser

Returns a new instance of YAMLParser.



7
8
9
10
11
12
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
# File 'lib/bhf/settings/yaml_parser.rb', line 7

def initialize(roles_array, area = nil)
  roles_settings = roles_yml(roles_array, area)

  if Bhf.configuration.abstract_settings.any?
  
    tmp_pages = get_settings_array(Bhf.configuration.abstract_settings)['pages']
    abstract_platform_settings = tmp_pages.each_with_object({}) do |abstract_pages, hash|
      abstract_pages.each do |abstract_page|
        abstract_page[1].each do |abstract_platform|
          hash.merge!(abstract_platform)
        end
      end
    end
  
    roles_settings['pages'].each_with_index do |pages, i_1|
      pages.each_pair do |key_1, page|
        page.each_with_index do |platform, i_2|
          platform.each_pair do |key_2, value|
            abstract_platform_key = if value.to_h['extend_abstract']
              value.to_h['extend_abstract']
            elsif abstract_platform_settings[key_2]
              key_2
            end
            next unless abstract_platform_key
            roles_settings['pages'][i_1][key_1][i_2][key_2] = abstract_platform_settings[abstract_platform_key].deep_merge(value.to_h)
          end
        end
      end
    end
  end
  
  @settings_hash = roles_settings
end

Instance Attribute Details

#settings_hashObject

Returns the value of attribute settings_hash.



5
6
7
# File 'lib/bhf/settings/yaml_parser.rb', line 5

def settings_hash
  @settings_hash
end

Instance Method Details

#get_settings_array(array, dir = nil) ⇒ Object



42
43
44
45
46
47
# File 'lib/bhf/settings/yaml_parser.rb', line 42

def get_settings_array(array, dir = nil)
  array.each_with_object({'pages' => []}) do |r, |
    pages = load_yml("#{dir}/#{r}")['pages']
    ['pages'] += pages if pages
  end
end

#load_yml(suffix = nil) ⇒ Object



76
77
78
# File 'lib/bhf/settings/yaml_parser.rb', line 76

def load_yml(suffix = nil)
  YAML::load(IO.read("config/bhf#{suffix}.yml"))
end

#roles_yml(roles = nil, area = nil) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/bhf/settings/yaml_parser.rb', line 49

def roles_yml(roles = nil, area = nil)
  area_dir = "/#{area}" if area.present?
  if roles.is_a?(Array)
    files = get_settings_array(roles, area_dir)

    # find the same pages and merge them
    merged_files = {'pages' => []}
    files['pages'].each do |pages|
      merged = false
      pages.each_pair do |key, page|
        merged_files['pages'].each do |m_page|
          if m_page.include?(key)
            merged = true
            m_page[key] = m_page[key] + page
          end
        end
      end
      if !merged
        merged_files['pages'] << pages
      end
    end
    merged_files
  else
    load_yml(area.present? ? "/#{area}/bhf" : nil)
  end
end