Class: NonConfig::NonHash
- Inherits:
-
Hash
- Object
- Hash
- NonConfig::NonHash
- Defined in:
- lib/non_config/non_hash.rb
Overview
hash with overriden #method_missing
Direct Known Subclasses
Class Method Summary collapse
-
.convert_array_subhashes(array) ⇒ Object
recursively converts subhashes of
arraytoself.class. -
.from_hash(hash) ⇒ Object
creates NonHash from Hash with recursive conversion of sub hashes.
Instance Method Summary collapse
-
#[]=(key, value) ⇒ Object
it allows constructions like non_hash.value1.value2.
- #method_missing(m, *args) ⇒ Object
- #to_hash ⇒ Object
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(m, *args) ⇒ Object
31 32 33 34 |
# File 'lib/non_config/non_hash.rb', line 31 def method_missing(m, *args) super unless args.empty? self[m.to_s] || self[m.to_sym] end |
Class Method Details
.convert_array_subhashes(array) ⇒ Object
recursively converts subhashes of array to self.class
17 18 19 20 21 22 23 |
# File 'lib/non_config/non_hash.rb', line 17 def self.convert_array_subhashes(array) array.map do |e| next convert_array_subhashes(e) if e.is_a? Array next from_hash(e) if e.is_a? Hash e end end |
.from_hash(hash) ⇒ Object
creates NonHash from Hash with recursive conversion of sub hashes
7 8 9 10 11 12 13 14 |
# File 'lib/non_config/non_hash.rb', line 7 def self.from_hash(hash) hash.each do |k, v| hash[k] = from_hash(v) if v.is_a? Hash hash[k] = convert_array_subhashes(v) if v.is_a? Array end new.merge(hash) end |
Instance Method Details
#[]=(key, value) ⇒ Object
it allows constructions like non_hash.value1.value2
26 27 28 29 |
# File 'lib/non_config/non_hash.rb', line 26 def []=(key, value) value = self.class.from_hash(value) if value.is_a? Hash super(key, value) end |
#to_hash ⇒ Object
36 37 38 39 40 41 42 43 |
# File 'lib/non_config/non_hash.rb', line 36 def to_hash each do |k, v| case v.class when self.class then self[k] = v.to_hash when Array then self[k] = reconvert_array(v) end end end |