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 ‘array` to `self.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
recursive conversion.
- #to_yaml ⇒ Object
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(m, *args) ⇒ Object
35 36 37 38 |
# File 'lib/non_config/non_hash.rb', line 35 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`
21 22 23 24 25 26 27 |
# File 'lib/non_config/non_hash.rb', line 21 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 15 16 17 18 |
# File 'lib/non_config/non_hash.rb', line 7 def self.from_hash(hash) new_hash = {} hash.each do |k, v| new_hash[k] = case v when Hash then from_hash(v) when Array then convert_array_subhashes(v) else v end end new.merge(new_hash) end |
Instance Method Details
#[]=(key, value) ⇒ Object
it allows constructions like non_hash.value1.value2
30 31 32 33 |
# File 'lib/non_config/non_hash.rb', line 30 def []=(key, value) value = self.class.from_hash(value) if value.is_a? Hash super(key, value) end |
#to_hash ⇒ Object
recursive conversion
41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/non_config/non_hash.rb', line 41 def to_hash result = {} each do |k, v| result[k] = case v when self.class then v.to_hash when Array then unconvert_array(v) else v end end result end |
#to_yaml ⇒ Object
56 57 58 |
# File 'lib/non_config/non_hash.rb', line 56 def to_yaml to_hash.to_yaml end |