Class: HierarchicalConfig::OpenStruct

Inherits:
OpenStruct
  • Object
show all
Defined in:
lib/hierarchical_config.rb

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(mid, *args) ⇒ Object

:nodoc:



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/hierarchical_config.rb', line 14

def method_missing( mid, *args ) # :nodoc:
  mname = mid.id2name
  len = args.length
  if mname.chomp!('=')
    if len != 1
      raise ArgumentError, "wrong number of arguments (#{len} for 1)", caller(1)
    end
    modifiable[new_ostruct_member(mname)] = args[0]
  elsif mname =~ /\?$/
    !!send(mname.gsub("?",""))
  elsif len == 0 && @table.key?( mid )
    @table[mid]
  else
    raise NoMethodError, "undefined method `#{mname}' for #{self}", caller(1)
  end
end

Instance Method Details

#[](attribute) ⇒ Object



31
32
33
# File 'lib/hierarchical_config.rb', line 31

def [](attribute)
  send(attribute)
end

#to_hashObject



35
36
37
38
39
40
41
# File 'lib/hierarchical_config.rb', line 35

def to_hash
  @table.inject({}) do |hash, key_value|
    key, value = *key_value
    hash[key] = value.respond_to?( :to_hash ) ? value.to_hash : value
    hash
  end
end