lib/hashr.rb in hashr-0.0.1 vs lib/hashr.rb in hashr-0.0.2

- old
+ new

@@ -12,11 +12,12 @@ @definition ||= {} end end def initialize(data = {}) - replace(deep_includize(deep_hasherize(deep_merge(self.class.definition, data)))) + include_modules(data.delete(:_include)) if data + replace(deep_hasherize(deep_merge(self.class.definition, data))) end def []=(key, value) super(key, value.is_a?(Hash) ? self.class.new(value) : value) end @@ -36,26 +37,26 @@ end end protected + def include_modules(modules) + Array(modules).each { |mod| meta_class.send(:include, mod) } if modules + end + + def meta_class + class << self; self end + end + def deep_merge(left, right) merger = proc { |key, v1, v2| v1.is_a?(Hash) && v2.is_a?(Hash) ? self.class.new(v1.merge(v2, &merger)) : v2 } left.merge(right || {}, &merger) end def deep_hasherize(hash) hash.inject(TEMPLATE.dup) do |result, (key, value)| - result.merge(key.to_sym => value.is_a?(Hash) ? deep_hasherize(value) : value) + result[key.to_sym] = value.is_a?(Hash) ? deep_hasherize(value) : value + result end end - - def deep_includize(hash) - if modules = hash.delete(:_include) - meta_class = (class << hash; self end) - Array(modules).each { |mod| meta_class.send(:include, mod) } - end - hash.inject(hash) do |hash, (key, value)| - hash.merge(key => value.is_a?(Hash) ? deep_includize(value) : value) - end - end end +