lib/esse/document.rb in esse-0.3.0 vs lib/esse/document.rb in esse-0.3.1

- old
+ new

@@ -54,11 +54,11 @@ {} end # @return [Hash] the document data def to_h - source.merge( + mutated_source.merge( _id: id, ).tap do |hash| hash[:_type] = type if type hash[:_routing] = routing if routing hash.merge!(meta) @@ -66,13 +66,13 @@ end def to_bulk(data: true, operation: nil) doc_header.tap do |h| if data && operation == :update - h[:data] = { doc: source_with_lazy_attributes } + h[:data] = { doc: mutated_source } elsif data - h[:data] = source_with_lazy_attributes + h[:data] = mutated_source end h.merge!(meta) end end @@ -103,22 +103,20 @@ "#{attr}: #{value.inspect}" if value end.compact.join(', ') "#<#{self.class.name || 'Esse::Document'} #{attributes}>" end - protected - - def source_with_lazy_attributes - return source unless @__lazy_source_data__ - - @__lazy_source_data__.merge(source) + def mutate(key) + @__mutations__ ||= {} + @__mutations__[key] = yield + instance_variable_set(:@__mutated_source__, nil) end - # api private - def __add_lazy_data_to_source__(hash) - return hash unless hash.is_a?(Hash) + protected - @__lazy_source_data__ ||= {} - @__lazy_source_data__.merge!(hash) + def mutated_source + return source unless @__mutations__ + + @__mutated_source__ ||= source.merge(@__mutations__) end end end