Class: Kitchenplan::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/kitchenplan/config.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConfig

Returns a new instance of Config.



16
17
18
19
20
21
# File 'lib/kitchenplan/config.rb', line 16

def initialize
  self.detect_platform
  self.parse_default_config
  self.parse_people_config
  self.parse_group_configs
end

Instance Attribute Details

#default_configObject (readonly)

Returns the value of attribute default_config.



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

def default_config
  @default_config
end

#group_configsObject (readonly)

Returns the value of attribute group_configs.



14
15
16
# File 'lib/kitchenplan/config.rb', line 14

def group_configs
  @group_configs
end

#people_configObject (readonly)

Returns the value of attribute people_config.



13
14
15
# File 'lib/kitchenplan/config.rb', line 13

def people_config
  @people_config
end

#platformObject (readonly)

Returns the value of attribute platform.



11
12
13
# File 'lib/kitchenplan/config.rb', line 11

def platform
  @platform
end

Instance Method Details

#configObject



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/kitchenplan/config.rb', line 60

def config
  config = {}
  config['recipes'] = []
  config['recipes'] |= hash_path(@default_config, 'recipes', 'global') || []
  config['recipes'] |= hash_path(@default_config, 'recipes', @platform) || []
  @group_configs.each do |group_name, group_config|
    config['recipes'] |= hash_path(group_config, 'recipes', 'global') || []
    config['recipes'] |= hash_path(group_config, 'recipes', @platform) || []
  end
  people_recipes = @people_config['recipes'] || {}
  config['recipes'] |= people_recipes['global'] || []
  config['recipes'] |= people_recipes[@platform] || []
  config['attributes'] = {}
  config['attributes'].deep_merge!(@default_config['attributes'] || {}) { |key, old, new| Array.wrap(old) + Array.wrap(new) }
  @group_configs.each do |group_name, group_config|
    config['attributes'].deep_merge!(group_config['attributes']) { |key, old, new| Array.wrap(old) + Array.wrap(new) } unless group_config['attributes'].nil?
  end
  people_attributes = @people_config['attributes'] || {}
  config['attributes'].deep_merge!(people_attributes) { |key, old, new| Array.wrap(old) + Array.wrap(new) }
  config
end

#detect_platformObject



23
24
25
26
27
28
29
# File 'lib/kitchenplan/config.rb', line 23

def detect_platform
  #ohai = Ohai::System.new
  #ohai.require_plugin('os')
  #ohai.require_plugin('platform')
  #@platform = ohai[:platform_family]
  @platform = 'mac_os_x' # We only support osx at the moment, and it saves a large dependency
end

#parse_default_configObject



31
32
33
34
# File 'lib/kitchenplan/config.rb', line 31

def parse_default_config
  default_config_path = 'config/default.yml'
  @default_config = (YAML.load(ERB.new(File.read(default_config_path)).result) if File.exist?(default_config_path)) || {}
end

#parse_group_config(group) ⇒ Object



49
50
51
52
53
54
55
56
57
58
# File 'lib/kitchenplan/config.rb', line 49

def parse_group_config(group)
  unless @group_configs[group]
    group_config_path = "config/groups/#{group}.yml"
    @group_configs[group] = (YAML.load(ERB.new(File.read(group_config_path)).result) if File.exist?(group_config_path)) || {}
    defined_groups = @group_configs[group]['groups']
    if defined_groups
      self.parse_group_configs(defined_groups)
    end
  end
end

#parse_group_configs(group = (( @default_config['groups'] || [] ) | ( @people_config['groups'] || [] ))) ⇒ Object



41
42
43
44
45
46
47
# File 'lib/kitchenplan/config.rb', line 41

def parse_group_configs(group = (( @default_config['groups'] || [] ) | ( @people_config['groups'] || [] )))
  @group_configs = @group_configs || {}
  defined_groups = group || []
  defined_groups.each do |group|
    self.parse_group_config(group)
  end
end

#parse_people_configObject



36
37
38
39
# File 'lib/kitchenplan/config.rb', line 36

def parse_people_config
  people_config_path = "config/people/#{Etc.getlogin}.yml"
  @people_config = (YAML.load(ERB.new(File.read(people_config_path)).result) if File.exist?(people_config_path)) || {}
end