diff --git a/Gemfile.lock b/Gemfile.lock index beeef02..6fd2d2b 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,7 +1,7 @@ PATH remote: . specs: - esse (0.3.4) + esse (0.3.5) multi_json thor (>= 0.19) diff --git a/lib/esse/document.rb b/lib/esse/document.rb index 8236ba3..be94e44 100644 --- a/lib/esse/document.rb +++ b/lib/esse/document.rb @@ -94,6 +94,7 @@ def doc_header { _id: id }.tap do |h| h[:_type] = type if type h[:routing] = routing if routing? + h.merge!(options) end end diff --git a/lib/esse/lazy_document_header.rb b/lib/esse/lazy_document_header.rb index c3039bd..50cd80b 100644 --- a/lib/esse/lazy_document_header.rb +++ b/lib/esse/lazy_document_header.rb @@ -61,6 +61,10 @@ def routing @attributes[:routing] end + def options + @attributes.reject { |key, _| %i[_id _type routing].include?(key) } + end + def to_doc(source = {}) HashDocument.new(source.merge(@attributes)) end diff --git a/spec/esse/document_spec.rb b/spec/esse/document_spec.rb index 82a99ee..2633565 100644 --- a/spec/esse/document_spec.rb +++ b/spec/esse/document_spec.rb @@ -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', foo: 'bar') } + end end describe '#mutate' do diff --git a/spec/esse/lazzy_document_header_spec.rb b/spec/esse/lazzy_document_header_spec.rb index 6e18484..93e0211 100644 --- a/spec/esse/lazzy_document_header_spec.rb +++ b/spec/esse/lazzy_document_header_spec.rb @@ -3,7 +3,7 @@ require 'spec_helper' RSpec.describe Esse::LazyDocumentHeader do - let(:doc) { described_class.new(object) } + let(:doc) { described_class.new(object.merge(options)) } let(:object) { {} } let(:options) { {} } @@ -190,4 +190,20 @@ expect(doc.to_doc(foo: 'bar').source).to eq(object.merge(foo: 'bar')) end end + + describe '#options' do + it { expect(doc).to respond_to :options } + + it 'returns an empty hash' do + expect(doc.options).to eq({}) + end + + context 'when options are present' do + let(:options) { { foo: 'bar' } } + + it 'returns the options' do + expect(doc.options).to eq(options) + end + end + end end