From b30a35c6d08caefdbdb1f20292a721a9cd6aa6ff Mon Sep 17 00:00:00 2001 From: "Marcos G. Zimmermann" Date: Fri, 12 Jul 2024 13:55:57 -0300 Subject: [PATCH] chore: rename attributes kwarg to lazy_attributes --- lib/esse/index/documents.rb | 2 +- lib/esse/repository/object_document_mapper.rb | 8 ++++---- spec/esse/repository/document_spec.rb | 10 +++++----- 3 files changed, 10 insertions(+), 10 deletions(-) diff --git a/lib/esse/index/documents.rb b/lib/esse/index/documents.rb index 4552afc..fe61f7f 100644 --- a/lib/esse/index/documents.rb +++ b/lib/esse/index/documents.rb @@ -210,7 +210,7 @@ def import(*repo_types, context: {}, eager_include_document_attributes: false, l doc_attrs[:lazy] -= doc_attrs[:eager] context ||= {} - context[:attributes] = doc_attrs[:eager] if doc_attrs[:eager].any? + context[:lazy_attributes] = doc_attrs[:eager] if doc_attrs[:eager].any? repo.each_serialized_batch(**context) do |batch| # Elasticsearch 6.x and older have multiple types per index. # This gem supports multiple types per index for backward compatibility, but we recommend to update diff --git a/lib/esse/repository/object_document_mapper.rb b/lib/esse/repository/object_document_mapper.rb index 064495f..452a517 100644 --- a/lib/esse/repository/object_document_mapper.rb +++ b/lib/esse/repository/object_document_mapper.rb @@ -74,14 +74,14 @@ def collection(collection_klass = nil, **_, &block) # @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(attributes: false, **kwargs) + def each_serialized_batch(lazy_attributes: false, **kwargs) each_batch(**kwargs) do |*args| batch, collection_context = args collection_context ||= {} entries = [*batch].map { |entry| serialize(entry, **collection_context) }.compact - if attributes - attributes = lazy_document_attribute_names(attributes) unless attributes.is_a?(Array) - attributes.each do |attr_name| + if lazy_attributes + attrs = lazy_attributes.is_a?(Array) ? lazy_attributes : lazy_document_attribute_names(lazy_attributes) + attrs.each do |attr_name| retrieve_lazy_attribute_values(attr_name, entries).each do |doc_header, value| doc = entries.find { |d| doc_header.id.to_s == d.id.to_s && doc_header.type == d.type && doc_header.routing == d.routing } doc&.mutate(attr_name) { value } diff --git a/spec/esse/repository/document_spec.rb b/spec/esse/repository/document_spec.rb index 3305eed..f68881f 100644 --- a/spec/esse/repository/document_spec.rb +++ b/spec/esse/repository/document_spec.rb @@ -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 attributes: true' do + it 'yields serialized objects with lazy attributes when passing lazy_attributes: true' do expected_data = [] expect { - StoriesIndex::Story.each_serialized_batch(attributes: true) do |batch| + StoriesIndex::Story.each_serialized_batch(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 attributes: false' do + it 'yields serialized objects without lazy attributes when passing lazy_attributes: false' do expected_data = [] expect { - StoriesIndex::Story.each_serialized_batch(attributes: false) do |batch| + StoriesIndex::Story.each_serialized_batch(lazy_attributes: false) do |batch| expected_data.push(*batch) end }.not_to raise_error @@ -180,7 +180,7 @@ it 'yields serialized objects with lazy attributes when passing specific attributes' do expected_data = [] expect { - StoriesIndex::Story.each_serialized_batch(attributes: %i[tags]) do |batch| + StoriesIndex::Story.each_serialized_batch(lazy_attributes: %i[tags]) do |batch| expected_data.push(*batch) end }.not_to raise_error