lib/looksist/hashed.rb in looksist-0.3.0 vs lib/looksist/hashed.rb in looksist-0.3.1
- old
+ new
@@ -16,11 +16,11 @@
unless @rules[after].length > 1
define_method("#{after}_with_inject") do |*args|
hash = send("#{after}_without_inject".to_sym, *args)
self.class.instance_variable_get(:@rules)[after].each do |opts|
- if opts[:at].is_a? String
+ if opts[:at].nil? or opts[:at].is_a? String
hash = self.class.update_using_json_path(hash, opts)
else
self.class.inject_attributes_at(hash[opts[:at]], opts)
end
end
@@ -39,11 +39,11 @@
unless @rules[after].length > 1
define_singleton_method("#{after}_with_inject") do |*args|
hash = send("#{after}_without_inject".to_sym, *args)
@rules[after].each do |opts|
- if opts[:at].is_a? String
+ if opts[:at].nil? or opts[:at].is_a? String
hash = update_using_json_path(hash, opts)
else
inject_attributes_at(hash[opts[:at]], opts)
end
end
@@ -63,13 +63,17 @@
hash_offset
end
def update_using_json_path(hash, opts)
if hash.is_a?(Hash)
+ if opts[:at].present?
+ JsonPath.for(hash.with_indifferent_access).gsub!(opts[:at]) do |i|
+ i.is_a?(Array) ? inject_attributes_for(i, opts) : inject_attributes_at(i, opts) unless (i.nil? or i.empty?)
+ i
+ end
+ else
+ inject_attributes_at(hash, opts)
- JsonPath.for(hash.with_indifferent_access).gsub!(opts[:at]) do |i|
- i.is_a?(Array) ? inject_attributes_for(i, opts) : inject_attributes_at(i, opts) unless (i.nil? or i.empty?)
- i
end.to_hash.deep_symbolize_keys
else
inject_attributes_for(hash, opts)
end
end