From 528631171e31bdd135c99664d7d17f80eb31d8f4 Mon Sep 17 00:00:00 2001 From: "Marcos G. Zimmermann" Date: Fri, 23 Aug 2024 16:53:35 -0300 Subject: [PATCH] chore: rename lazy_attributes to eager_load_lazy_attributes --- lib/esse/index/documents.rb | 13 +++++++++---- lib/esse/repository/object_document_mapper.rb | 10 +++++++--- spec/esse/repository/document_spec.rb | 10 +++++----- 3 files changed, 21 insertions(+), 12 deletions(-) diff --git a/lib/esse/index/documents.rb b/lib/esse/index/documents.rb index 2ff5347..9eea726 100644 --- a/lib/esse/index/documents.rb +++ b/lib/esse/index/documents.rb @@ -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 && @@ -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) diff --git a/lib/esse/repository/object_document_mapper.rb b/lib/esse/repository/object_document_mapper.rb index 237b52d..9e5a0e0 100644 --- a/lib/esse/repository/object_document_mapper.rb +++ b/lib/esse/repository/object_document_mapper.rb @@ -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) } diff --git a/spec/esse/repository/document_spec.rb b/spec/esse/repository/document_spec.rb index 0409c8d..0476b72 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 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 @@ -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