Skip to content

Commit

Permalink
add options to the lazy document header (#23)
Browse files Browse the repository at this point in the history
* add options to the lazy document header

* fix: options must not be part of doc_header

* refactoring the LazyDocumentHeader document to properly set source and options

* fixing broken specs

* refactoring lazy document header coercion

* feat: add collection_class method to repository

* fixing broken specs
  • Loading branch information
marcosgz authored Aug 7, 2024
1 parent 1efb5fe commit f3ad915
Show file tree
Hide file tree
Showing 19 changed files with 140 additions and 78 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## 0.3.6 - 2024-08-07
* Esse::LazyDocumentHeader#to_doc return `Esse::LazyDocumentHeader::Document` instance to properly separate context metadata from document source
* Add `.collection_class` method to the `Esse::Repository` class to let external plugins and extensions to access it instead of read @collection_proc variable

## 0.3.5 - 2024-08-02
* Add `update_by_query` action to transport and index APIs
* Reset index using `_reindex` api instead of the traditional collection `import` method
Expand Down
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
esse (0.3.4)
esse (0.3.6)
multi_json
thor (>= 0.19)

Expand Down
2 changes: 1 addition & 1 deletion gemfiles/Gemfile.elasticsearch-1.x.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: ..
specs:
esse (0.3.5)
esse (0.3.6)
multi_json
thor (>= 0.19)

Expand Down
2 changes: 1 addition & 1 deletion gemfiles/Gemfile.elasticsearch-2.x.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: ..
specs:
esse (0.3.5)
esse (0.3.6)
multi_json
thor (>= 0.19)

Expand Down
2 changes: 1 addition & 1 deletion gemfiles/Gemfile.elasticsearch-5.x.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: ..
specs:
esse (0.3.5)
esse (0.3.6)
multi_json
thor (>= 0.19)

Expand Down
2 changes: 1 addition & 1 deletion gemfiles/Gemfile.elasticsearch-6.x.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: ..
specs:
esse (0.3.5)
esse (0.3.6)
multi_json
thor (>= 0.19)

Expand Down
2 changes: 1 addition & 1 deletion gemfiles/Gemfile.elasticsearch-7.x.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: ..
specs:
esse (0.3.5)
esse (0.3.6)
multi_json
thor (>= 0.19)

Expand Down
2 changes: 1 addition & 1 deletion gemfiles/Gemfile.elasticsearch-8.x.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: ..
specs:
esse (0.3.5)
esse (0.3.6)
multi_json
thor (>= 0.19)

Expand Down
2 changes: 1 addition & 1 deletion gemfiles/Gemfile.opensearch-1.x.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: ..
specs:
esse (0.3.5)
esse (0.3.6)
multi_json
thor (>= 0.19)

Expand Down
2 changes: 1 addition & 1 deletion gemfiles/Gemfile.opensearch-2.x.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: ..
specs:
esse (0.3.5)
esse (0.3.6)
multi_json
thor (>= 0.19)

Expand Down
2 changes: 1 addition & 1 deletion lib/esse/document.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class Document

def initialize(object, **options)
@object = object
@options = options
@options = options.freeze
end

# @return [String, Number] the document ID
Expand Down
54 changes: 32 additions & 22 deletions lib/esse/lazy_document_header.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,57 +17,67 @@ def self.coerce(value)
if value.is_a?(Esse::LazyDocumentHeader)
value
elsif value.is_a?(Esse::Document)
new(value.doc_header)
new(id: value.id, type: value.type, routing: value.routing, **value.options)
elsif value.is_a?(Hash)
resp = value.transform_keys do |key|
case key
when :_id, :id, '_id', 'id'
:_id
:id
when :_routing, :routing, '_routing', 'routing'
:routing
when :_type, :type, '_type', 'type'
:_type
:type
else
key.to_sym
end
end
new(resp)
resp[:id] ||= nil
new(**resp)
elsif String === value || Integer === value
new(_id: value)
new(id: value)
end
end

def initialize(attributes)
@attributes = attributes
attr_reader :id, :type, :routing, :options

def initialize(id:, type: nil, routing: nil, **extra_attributes)
@id = id
@type = type
@routing = routing
@options = extra_attributes.freeze
end

def valid?
!@attributes[:_id].nil?
!id.nil?
end

def to_h
@attributes
options.merge(_id: id).tap do |hash|
hash[:_type] = type if type
hash[:routing] = routing if routing
end
end

def id
@attributes.fetch(:_id)
def to_doc(source = {})
Document.new(self, source: source)
end

def type
@attributes[:_type]
def eql?(other)
self.class == other.class && id == other.id && type == other.type && routing == other.routing
end
alias_method :==, :eql?

def routing
@attributes[:routing]
end
class Document < Esse::Document
extend Forwardable

def to_doc(source = {})
HashDocument.new(source.merge(@attributes))
end
def_delegators :object, :id, :type, :routing, :options

def eql?(other)
self.class == other.class && @attributes == other.instance_variable_get(:@attributes)
attr_reader :source

def initialize(lazy_header, source: {})
@source = source
super(lazy_header)
end
end
alias_method :==, :eql?
end
end
11 changes: 10 additions & 1 deletion lib/esse/repository/object_document_mapper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,15 @@ def collection(collection_klass = nil, **_, &block)
@collection_proc = collection_klass || block
end

# Expose the collection class to let external plugins and extensions to access it.
# @return [Class, nil] The collection class
# IDEA: When collection is defined as a block, it should setup a class with the block content.
def collection_class
return unless @collection_proc.is_a?(Class)

@collection_proc
end

# Wrap collection data into serialized batches
#
# @param [Hash] kwargs The context
Expand All @@ -89,7 +98,7 @@ def each_serialized_batch(lazy_attributes: false, **kwargs)
end
end

yield entries, **kwargs
yield entries, **collection_context
end
end

Expand Down
2 changes: 1 addition & 1 deletion lib/esse/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

module Esse
VERSION = '0.3.5'
VERSION = '0.3.6'
end
6 changes: 6 additions & 0 deletions spec/esse/document_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,12 @@ def routing
expect(document.doc_header).to eq(_id: 1, routing: 'bar')
end
end

context 'when the document includes options' do
let(:options) { { foo: 'bar' } }

it { is_expected.to eq(_id: 1, _type: 'foo', routing: 'bar') }
end
end

describe '#mutate' do
Expand Down
Loading

0 comments on commit f3ad915

Please sign in to comment.