lib/hash_mapper.rb in ismasan-hash_mapper-0.0.6 vs lib/hash_mapper.rb in ismasan-hash_mapper-0.0.7
- old
+ new
@@ -28,11 +28,11 @@
end
end
end
module HashMapper
- VERSION = '0.0.6'
+ VERSION = '0.0.7'
# we need this for inheritable mappers, which is annoying because it needs ActiveSupport, kinda overkill.
#
def self.extended(base)
base.class_eval do
@@ -89,30 +89,20 @@
output = {}
# Before filter
before_filter = instance_eval "@before_#{meth}"
a_hash = before_filter.call(a_hash, output) if before_filter
# Do the mapping
- a_hash = symbolize_keys(a_hash)
maps.each do |m|
m.process_into(output, a_hash, meth)
end
# After filter
after_filter = instance_eval "@after_#{meth}"
output = after_filter.call(a_hash, output) if after_filter
# Return
output
end
- # from http://www.geekmade.co.uk/2008/09/ruby-tip-normalizing-hash-keys-as-symbols/
- #
- def symbolize_keys(hash)
- hash.inject({}) do |options, (key, value)|
- options[(key.to_sym rescue key) || key] = value
- options
- end
- end
-
# Contains PathMaps
# Makes them interact
#
class Map
@@ -132,12 +122,13 @@
protected
def get_value_from_input(output, input, path, meth)
value = path.inject(input) do |h,e|
- throw :no_value if h[e].nil?#.has_key?(e)
- h[e]
+ v = h.with_indifferent_access[e] # this does it, but uses ActiveSupport
+ throw :no_value if v.nil?#.has_key?(e)
+ v
end
delegated_mapper ? delegate_to_nested_mapper(value, meth) : value
end
@@ -152,10 +143,10 @@
end
end
def add_value_to_hash!(hash, path, value)
path.inject_with_index(hash) do |h,e,i|
- if h[e]
+ if !h[e].nil? # it can be FALSE
h[e]
else
h[e] = (i == path.size-1 ? path.apply_filter(value) : {})
end
end