lib/hashr.rb in hashr-0.0.14 vs lib/hashr.rb in hashr-0.0.15

- old
+ new

@@ -7,24 +7,34 @@ class << self attr_accessor :raise_missing_keys def define(definition) - @definition = definition.deep_symbolize_keys + @definition = deep_accessorize(definition.deep_symbolize_keys) end def definition @definition ||= {} end def default(defaults) - @defaults = defaults + @defaults = deep_accessorize(defaults) end def defaults @defaults ||= {} end + + def deep_accessorize(hash) + hash.each do |key, value| + next unless value.is_a?(Hash) + value[:_access] ||= [] + value[:_access] = Array(value[:_access]) + value.keys.each { |key| value[:_access] << key if value.respond_to?(key) } + deep_accessorize(value) + end + end end def initialize(data = {}, definition = self.class.definition, &block) replace((deep_hashrize(definition.deep_merge((data || {}).deep_symbolize_keys)))) deep_defaultize(self) @@ -90,12 +100,14 @@ hash.include_modules(value) when :_access hash.include_accessors(value) end end + hash.each do |key, value| deep_defaultize(value) if value.is_a?(Hash) end + hash end end