Skip to content

Commit

Permalink
chore: rename lazy_attributes to eager_load_lazy_attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
marcosgz committed Aug 23, 2024
1 parent dcc4479 commit 5286311
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 12 deletions.
13 changes: 9 additions & 4 deletions lib/esse/index/documents.rb
Original file line number Diff line number Diff line change
Expand Up @@ -249,9 +249,14 @@ def import(*repo_types, context: {}, eager_load_lazy_attributes: false, update_l
repo_types = repo_hash.keys if repo_types.empty?
count = 0

# Backward compatibility while I change plugins using it
update_lazy_attributes = options.delete(:lazy_update_document_attributes) if options.key?(:lazy_update_document_attributes)
eager_load_lazy_attributes = options.delete(:eager_include_document_attributes) if options.key?(:eager_include_document_attributes)
if options.key?(:eager_include_document_attributes)
warn 'The `eager_include_document_attributes` option is deprecated. Use `eager_load_lazy_attributes` instead.'
eager_load_lazy_attributes = options.delete(:eager_include_document_attributes)
end
if options.key?(:lazy_update_document_attributes)
warn 'The `lazy_update_document_attributes` option is deprecated. Use `update_lazy_attributes` instead.'
update_lazy_attributes = options.delete(:lazy_update_document_attributes)
end

doc_header_check = ->(doc, (id, routing, type)) do
id && id.to_s == doc.id.to_s &&
Expand All @@ -276,7 +281,7 @@ def import(*repo_types, context: {}, eager_load_lazy_attributes: false, update_l
lazy_attrs_to_search_preload -= lazy_attrs_to_eager_load

# @TODO Refactor this by combining the upcoming code again with repo.each_serialized_batch as it was before:
# context[:lazy_attributes] = lazy_attrs_to_eager_load if lazy_attrs_to_eager_load.any?
# context[:eager_load_lazy_attributes] = lazy_attrs_to_eager_load if lazy_attrs_to_eager_load.any?
# repo.each_serialized_batch(**context) do |batch|
# bulk(**bulk_kwargs, index: batch)

Expand Down
10 changes: 7 additions & 3 deletions lib/esse/repository/object_document_mapper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -83,13 +83,17 @@ def collection_class
# @param [Hash] kwargs The context
# @return [Enumerator] The enumerator
# @yield [Array, **context] serialized collection and the optional context from the collection
def each_serialized_batch(lazy_attributes: false, **kwargs)
def each_serialized_batch(eager_load_lazy_attributes: false, **kwargs)
if kwargs.key?(:lazy_attributes)
warn 'The `lazy_attributes` option is deprecated. Use `eager_load_lazy_attributes` instead.'
eager_load_lazy_attributes = kwargs.delete(:lazy_attributes)
end
each_batch(**kwargs) do |*args|
batch, collection_context = args
collection_context ||= {}
entries = [*batch].map { |entry| serialize(entry, **collection_context) }.compact
if lazy_attributes
attrs = lazy_attributes.is_a?(Array) ? lazy_attributes : lazy_document_attribute_names(lazy_attributes)
if eager_load_lazy_attributes
attrs = eager_load_lazy_attributes.is_a?(Array) ? eager_load_lazy_attributes : lazy_document_attribute_names(eager_load_lazy_attributes)
attrs.each do |attr_name|
retrieve_lazy_attribute_values(attr_name, entries).each do |doc_header, value|
doc = entries.find { |d| d.eql?(doc_header, match_lazy_doc_header: true) }
Expand Down
10 changes: 5 additions & 5 deletions spec/esse/repository/document_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -157,20 +157,20 @@
context 'with lazy_load_attributes' do
include_context 'with stories index definition'

it 'yields serialized objects with lazy attributes when passing lazy_attributes: true' do
it 'yields serialized objects with lazy attributes when passing eager_load_lazy_attributes: true' do
expected_data = []
expect {
StoriesIndex::Story.each_serialized_batch(lazy_attributes: true) do |batch|
StoriesIndex::Story.each_serialized_batch(eager_load_lazy_attributes: true) do |batch|
expected_data.push(*batch)
end
}.not_to raise_error
expect(expected_data.select { |doc| doc.to_h.key?(:tags) && doc.to_h.key?(:tags_count) }).not_to be_empty
end

it 'yields serialized objects without lazy attributes when passing lazy_attributes: false' do
it 'yields serialized objects without lazy attributes when passing eager_load_lazy_attributes: false' do
expected_data = []
expect {
StoriesIndex::Story.each_serialized_batch(lazy_attributes: false) do |batch|
StoriesIndex::Story.each_serialized_batch(eager_load_lazy_attributes: false) do |batch|
expected_data.push(*batch)
end
}.not_to raise_error
Expand All @@ -180,7 +180,7 @@
it 'yields serialized objects with lazy attributes when passing specific attributes' do
expected_data = []
expect {
StoriesIndex::Story.each_serialized_batch(lazy_attributes: %i[tags]) do |batch|
StoriesIndex::Story.each_serialized_batch(eager_load_lazy_attributes: %i[tags]) do |batch|
expected_data.push(*batch)
end
}.not_to raise_error
Expand Down

0 comments on commit 5286311

Please sign in to comment.