Skip to content

Commit

Permalink
add options to the lazy document header
Browse files Browse the repository at this point in the history
  • Loading branch information
marcosgz committed Aug 7, 2024
1 parent 1efb5fe commit de7120d
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 2 deletions.
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.5)
multi_json
thor (>= 0.19)

Expand Down
1 change: 1 addition & 0 deletions lib/esse/document.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
4 changes: 4 additions & 0 deletions lib/esse/lazy_document_header.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
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', foo: 'bar') }
end
end

describe '#mutate' do
Expand Down
18 changes: 17 additions & 1 deletion spec/esse/lazzy_document_header_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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) { {} }

Expand Down Expand Up @@ -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

0 comments on commit de7120d

Please sign in to comment.