lib/hash_mapper.rb in ismasan-hash_mapper-0.0.4 vs lib/hash_mapper.rb in ismasan-hash_mapper-0.0.5
- old
+ new
@@ -9,11 +9,11 @@
end
end
end
module HashMapper
- VERSION = '0.0.4'
+ VERSION = '0.0.5'
def maps
@maps ||= []
end
@@ -83,12 +83,13 @@
protected
def get_value_from_input(output, input, path, meth)
value = path.inject(input) do |h,e|
- throw :no_value unless h.has_key?(e)
- h[e]
+ throw :no_value unless h.has_key?(e[0].to_sym)
+ e[1].nil? ? h[e[0].to_sym] : h[e[0].to_sym][e[1].to_i]
+ #h[e[0].to_sym]
end
value = delegate_to_nested_mapper(value, meth) if delegated_mapper
value
end
@@ -101,18 +102,35 @@
end
end
def add_value_to_hash!(hash, path, value)
path.inject(hash) do |h,e|
- if h[e]
- h[e]
+ if contained?(h,e)
+ if e[1].nil?
+ h[e[0].to_sym]
+ else
+ if e == path.last
+ h[e[0].to_sym][e[1].to_i] = value
+ end
+ h[e[0].to_sym][e[1].to_i]
+ end
else
- h[e] = (e == path.last ? path.apply_filter(value) : {})
+ if e[1].nil?
+ h[e[0].to_sym] = (e == path.last ? path.apply_filter(value) : {})
+ else
+ h[e[0].to_sym] = []
+ h[e[0].to_sym][e[1].to_i] = (e == path.last ? path.apply_filter(value) : {})
+ end
end
end
end
+ def contained?(h,e)
+ e[1].nil? ? h[e[0].to_sym] : h[e[0].to_sym][e[1].to_i].nil?
+ rescue
+ false
+ end
end
# contains array of path segments
#
class PathMap
@@ -140,10 +158,15 @@
end
private
def parse(path)
- path.sub(/^\//,'').split('/').map(&:to_sym)
+ #path.sub(/^\//,'').split('/').map(&:to_sym)
+ path.sub(/^\//,'').split('/').map{ |p| key_index p }
+ end
+
+ def key_index(p)
+ p =~ /\[[0-9]+\]$/ ? p.sub(/\[([0-9]+)\]$/,' \1').split(' ') : [p,nil]
end
end
end
\ No newline at end of file