Skip to content

Commit

Permalink
chore: only use the memoized source
Browse files Browse the repository at this point in the history
  • Loading branch information
marcosgz committed Sep 11, 2024
1 parent 8fd3022 commit 028943e
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 5 deletions.
18 changes: 13 additions & 5 deletions lib/esse/document.rb
Original file line number Diff line number Diff line change
Expand Up @@ -109,10 +109,10 @@ def document_for_partial_update(source)
end

def inspect
attributes = %i[id routing source].map do |attr|
value = send(attr)
attributes = {id: :id, routing: :routing, source: :memoized_source}.map do |attr_name, attr_src|
value = send(attr_src)
next unless value
"#{attr}: #{value.inspect}"
"#{attr_name}: #{value.inspect}"
rescue
nil
end.compact.join(', ')
Expand All @@ -131,9 +131,17 @@ def mutations
end

def mutated_source
return source unless @__mutations__
return memoized_source unless @__mutations__

@__mutated_source__ ||= source.merge(@__mutations__)
@__mutated_source__ ||= memoized_source.merge(@__mutations__)
end

protected

def memoized_source
return @__memoized_source__ if defined?(@__memoized_source__)

@__memoized_source__ = source || {}
end
end
end
17 changes: 17 additions & 0 deletions spec/esse/document_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,23 @@ def source
expect(document.to_bulk(data: true)).to eq(_id: 1, _type: 'foo', routing: 'bar', timeout: 10, data: {})
end
end

context 'when the document source is not memoized' do
before do
document_class.class_eval do
def source
{ foo: SecureRandom.hex }
end
end
end

it 'memoizes the source' do
data1 = document.to_bulk(data: true)[:data]
expect(document.to_bulk(data: true)[:data]).to eq(data1)
data2 = document.to_bulk(data: true)[:data]
expect(data1).to be(data2)
end
end
end

describe '#doc_header' do
Expand Down

0 comments on commit 028943e

Please sign in to comment.