class NonConfig::NonHash

hash with overriden method_missing

Public Class Methods

convert_array_subhashes(array) click to toggle source

recursively converts subhashes of ‘array` to `self.class`

# 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) click to toggle source

creates NonHash from Hash with recursive conversion of sub hashes

# 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

Public Instance Methods

[]=(key, value) click to toggle source

it allows constructions like non_hash.value1.value2

Calls superclass method
# 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
method_missing(m, *args) click to toggle source
Calls superclass method
# 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
to_hash() click to toggle source

recursive conversion

# 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() click to toggle source
# File lib/non_config/non_hash.rb, line 56
def to_yaml
  to_hash.to_yaml
end

Private Instance Methods

unconvert_array(array) click to toggle source
# File lib/non_config/non_hash.rb, line 62
def unconvert_array(array)
  array.map do |e|
    next unconvert_array(e) if e.is_a? Array
    next e.to_hash if e.is_a? self.class
    e
  end
end