Class: Cheffish::MergedConfig

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/cheffish/merged_config.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*configs) ⇒ MergedConfig

Returns a new instance of MergedConfig.



5
6
7
8
# File 'lib/cheffish/merged_config.rb', line 5

def initialize(*configs)
  @configs = configs.map { |config| Mash.from_hash config.to_hash }
  @merge_arrays = Mash.new
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object



50
51
52
53
54
55
56
57
# File 'lib/cheffish/merged_config.rb', line 50

def method_missing(name, *args)
  $stderr.puts "WARN: deprecated use of method_missing on a Cheffish::MergedConfig object at #{caller[0]}"
  if args.count > 0
    raise NoMethodError, "Unexpected method #{name} for MergedConfig with arguments #{args}"
  else
    self[name]
  end
end

Instance Attribute Details

#configsObject (readonly)

Returns the value of attribute configs.



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

def configs
  @configs
end

Instance Method Details

#[](name) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/cheffish/merged_config.rb', line 23

def [](name)
  if @merge_arrays[name]
    configs.select { |c| !c[name].nil? }.collect_concat { |c| c[name] }
  else
    result_configs = []
    configs.each do |config|
      value = config[name]
      unless value.nil?
        if value.respond_to?(:keys)
          result_configs << value
        elsif result_configs.size > 0
          return result_configs[0]
        else
          return value
        end
      end
    end
    if result_configs.size > 1
      MergedConfig.new(*result_configs)
    elsif result_configs.size == 1
      result_configs[0]
    else
      nil
    end
  end
end

#eachObject



81
82
83
84
85
86
87
# File 'lib/cheffish/merged_config.rb', line 81

def each
  keys.each do |key|
    if block_given?
      yield key, self[key]
    end
  end
end

#each_pair(&block) ⇒ Object



77
78
79
# File 'lib/cheffish/merged_config.rb', line 77

def each_pair(&block)
  each(&block)
end

#empty?Boolean

Returns:

  • (Boolean)


73
74
75
# File 'lib/cheffish/merged_config.rb', line 73

def empty?
  configs.empty?
end

#key?(name) ⇒ Boolean Also known as: has_key?

Returns:

  • (Boolean)


59
60
61
# File 'lib/cheffish/merged_config.rb', line 59

def key?(name)
  configs.any? { |config| config.key?(name) }
end

#keysObject



65
66
67
# File 'lib/cheffish/merged_config.rb', line 65

def keys
  configs.flat_map(&:keys).uniq
end

#merge_arrays(*symbols) ⇒ Object



13
14
15
16
17
18
19
20
21
# File 'lib/cheffish/merged_config.rb', line 13

def merge_arrays(*symbols)
  if symbols.size > 0
    symbols.each do |symbol|
      @merge_arrays[symbol] = true
    end
  else
    @merge_arrays
  end
end

#to_hObject



97
98
99
# File 'lib/cheffish/merged_config.rb', line 97

def to_h
  to_hash
end

#to_hashObject



89
90
91
92
93
94
95
# File 'lib/cheffish/merged_config.rb', line 89

def to_hash
  result = {}
  each_pair do |key, value|
    result[key] = value
  end
  result
end

#to_sObject



101
102
103
# File 'lib/cheffish/merged_config.rb', line 101

def to_s
  to_hash.to_s
end

#valuesObject



69
70
71
# File 'lib/cheffish/merged_config.rb', line 69

def values
  keys.map { |key| self[key] }
end