lib/light_mapper.rb in light_mapper-1.0.5 vs lib/light_mapper.rb in light_mapper-1.0.6
- old
+ new
@@ -74,26 +74,26 @@
result = result[key]
end
end
end
- def self.push(hash, key, value, keys: :string, build_structure: true)
+ def self.push(hash, key, value, keys: :string, build_structure: true, override: false)
return hash[key] = value if key.is_a?(Symbol)
path = key.to_s.split('.')
path = path.map(&:to_sym) if keys == :symbol
context = hash
context[path.first] if build_structure && path.size == 2
path.each_with_index do |k, idx|
last_idx = idx == path.size - 1
- raise AlreadyAssignedValue, "Key #{k} already assigned in #{path} for #{hash.inspect} structure" if last_idx && context.key?(k) && !context[k].nil?
- next context[k] = value if last_idx
+ raise AlreadyAssignedValue, "Key #{k} already assigned in #{path} for #{hash.inspect} structure" if !override && last_idx && context.key?(k) && !context[k].nil?
+ next context[k] = value if last_idx && context.is_a?(Hash)
context.send(:[]=,k, {}) if build_structure && !context.key?(k)
- context = context.send(:[], k) if context.is_a?(Hash)
+ context.is_a?(Hash) ? context = context.send(:[], k) : break
end
end
def self.compact(hash)
hash.each_with_object({}) do |(key, value), result|
@@ -106,9 +106,14 @@
raise InvalidStructure, "Invalid key: #{key} for #{hash.inspect} structure"
end
def mapping(mappings, opts = {})
LightMapper.mapping(clone, mappings, opts)
+ end
+
+ def push(key, value, keys: :string, build_structure: true, override: false)
+ LightMapper::Helper.push(self, key, value, keys: keys, build_structure: build_structure, override: override)
+ self
end
def self.mapping(hash, mappings, opts = {})
strict, any_keys, keys = opts.values_at(:strict, :any_keys, :keys)