Class: Configuratron::ConfigHash
- Inherits:
-
BlankSlate
- Object
- BlankSlate
- Configuratron::ConfigHash
- Defined in:
- lib/configuratron.rb
Instance Method Summary collapse
- #[](k) ⇒ Object
- #[]=(k, v) ⇒ Object
-
#initialize(hash = nil) ⇒ ConfigHash
constructor
A new instance of ConfigHash.
- #method_missing(name, *args, &block) ⇒ Object
Methods inherited from BlankSlate
find_hidden_method, hide, reveal
Constructor Details
#initialize(hash = nil) ⇒ ConfigHash
Returns a new instance of ConfigHash.
27 28 29 30 31 32 33 34 |
# File 'lib/configuratron.rb', line 27 def initialize(hash = nil) @hash = {} if hash hash.each do |k,v| @hash[k] = v.is_a?(Hash) ? ConfigHash.new(v) : v end end end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args, &block) ⇒ Object
44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/configuratron.rb', line 44 def method_missing(name, *args, &block) if name.to_s[-1..-1] == '=' # workaround for 1.8 returning char base_name = name.to_s[0..-2].to_sym _define_singleton_method(name) do |name| __send__ :[]=, base_name, value end __send__ :[]=, base_name, *args else _define_singleton_method(name) do __send__ :[], name.to_sym end __send__ :[], name.to_sym end end |
Instance Method Details
#[](k) ⇒ Object
40 41 42 |
# File 'lib/configuratron.rb', line 40 def [](k) @hash[k] end |
#[]=(k, v) ⇒ Object
36 37 38 |
# File 'lib/configuratron.rb', line 36 def []=(k, v) @hash[k] = v.is_a?(Hash) ? ConfigHash.new(v) : v end |