Skip to content

Persistence implementation: Repository pattern #71

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 32 commits into from
May 27, 2014
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
6e199e4
[STORE] Added the blank skeleton of the "elasticsearch-persistence" gem
karmi Mar 25, 2014
4488ea5
[STORE] Added the Persistence::Repository module and client integration
karmi Mar 25, 2014
48d7132
[STORE] Added the default `Repository::Class` for convenience
karmi Mar 29, 2014
e5d9ffb
[STORE] Added the `Naming` module
karmi Mar 29, 2014
7165f3d
[STORE] Added the `Serialize` module
karmi Mar 29, 2014
5938ef0
[STORE] Added the `Store` module
karmi Mar 29, 2014
41a3695
[STORE] Added the `Find` module
karmi Mar 31, 2014
7bcd91d
[STORE] Added the `Search` module
karmi Mar 31, 2014
485a4f9
[STORE] Added the DSL variant of `klass` setter method
karmi Mar 31, 2014
c4b1d41
[STORE] Added the methods from the "elasticsearch-model" gem
karmi Mar 31, 2014
5160424
[STORE] Refactored the `:index` paramter to use repository `index_name`
karmi Mar 31, 2014
0989784
[STORE] Changed that `document_type` method returns `nil` when no `kl…
karmi Apr 1, 2014
caecfa9
[STORE] Changed that the `Store` methods reflect that `klass` returns…
karmi Apr 1, 2014
2fbc3aa
[STORE] Added the `index` and `type` aliases for `index_name` and `do…
karmi Apr 1, 2014
6d739dd
[STORE] Added, that `document_type` can set the document type for rep…
karmi Apr 1, 2014
e4e6ff2
[STORE] Changed, that repository methods respect `document_type` when…
karmi Apr 1, 2014
c391a3b
[STORE] Implemented the gateway pattern for the repository integration
karmi Apr 2, 2014
63da64a
[STORE] Added, that `index_name` is inferred from the including class
karmi Apr 2, 2014
1299c0b
[STORE] Added, that the repository class reflects the `:index` option
karmi Apr 2, 2014
a929413
[STORE] Added that the `client` can be set in a DSL-like way
karmi Apr 3, 2014
cbbff17
[STORE] Added `respond_to_missing?` to the proxy objects
karmi Apr 3, 2014
44c9952
[STORE] Added the `method_added` hook to allow defining gateway metho…
karmi Apr 4, 2014
ca1270d
[STORE] Added code annotation, documentation and examples
karmi Apr 4, 2014
94a19de
[STORE] Added a comprehensive usage information / tutorial to the README
karmi Apr 4, 2014
e853785
[STORE] Added an example Sinatra web application for the repository p…
karmi Apr 7, 2014
4881db8
[STORE] Added development dependency on "oj"
karmi Apr 17, 2014
afafa12
[STORE] Fixed, that `search` respects `document_type` set in class
pepe Apr 15, 2014
0e66341
[STORE] Added a unit test for searching in type based on `document_type`
karmi Apr 17, 2014
44f6bca
[STORE] Added integration tests for the Repository pattern
karmi Apr 17, 2014
c44c343
[STORE] Added the `__extract_id_from_document` method to Naming module
karmi Apr 19, 2014
b97491a
[STORE] Added a `update` method for the repository Store module
karmi Apr 19, 2014
91814b3
[STORE] Improved the documentation across the persistence gem
karmi May 27, 2014
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
[STORE] Refactored the :index paramter to use repository index_name
  • Loading branch information
karmi committed May 27, 2014
commit 5160424a5694cfcba5823f71c49ca5bddde1a002
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ def find(*args)

def exists?(id, options={})
type = (klass ? __get_type_from_class(klass) : '_all')
client.exists( { index: 'test', type: type, id: id }.merge(options) )
client.exists( { index: index_name, type: type, id: id }.merge(options) )
end

def __find_one(id, options={})
type = (klass ? __get_type_from_class(klass) : '_all')
document = client.get( { index: 'test', type: type, id: id }.merge(options) )
document = client.get( { index: index_name, type: type, id: id }.merge(options) )

deserialize(document)
rescue Elasticsearch::Transport::Transport::Errors::NotFound => e
Expand All @@ -32,7 +32,7 @@ def __find_one(id, options={})

def __find_many(ids, options={})
type = (klass ? __get_type_from_class(klass) : '_all')
documents = client.mget( { index: 'test', type: type, body: { ids: ids } }.merge(options) )
documents = client.mget( { index: index_name, type: type, body: { ids: ids } }.merge(options) )

documents['docs'].map { |document| document['found'] ? deserialize(document) : nil }
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ def search(query_or_definition, options={})
type = (klass ? __get_type_from_class(klass) : nil )
case
when query_or_definition.respond_to?(:to_hash)
response = client.search( { index: 'test', type: type, body: query_or_definition.to_hash }.merge(options) )
response = client.search( { index: index_name, type: type, body: query_or_definition.to_hash }.merge(options) )
when query_or_definition.is_a?(String)
response = client.search( { index: 'test', type: type, q: query_or_definition }.merge(options) )
response = client.search( { index: index_name, type: type, q: query_or_definition }.merge(options) )
else
raise ArgumentError, "[!] Pass the search definition as a Hash-like object or pass the query as a String" +
" -- #{query_or_definition.class} given."
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ def save(document, options={})
serialized = serialize(document)
id = __get_id_from_document(serialized)
type = klass || __get_type_from_class(document.class)
client.index( { index: 'test', type: type, id: id, body: serialized }.merge(options) )
client.index( { index: index_name, type: type, id: id, body: serialized }.merge(options) )
end

def delete(document, options={})
Expand All @@ -19,7 +19,7 @@ def delete(document, options={})
id = __get_id_from_document(serialized)
type = klass || __get_type_from_class(document.class)
end
client.delete( { index: 'test', type: type, id: id }.merge(options) )
client.delete( { index: index_name, type: type, id: id }.merge(options) )
end
end

Expand Down
26 changes: 15 additions & 11 deletions elasticsearch-persistence/test/unit/repository_find_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ class MyDocument; end

@client = mock
@shoulda_subject.stubs(:klass).returns(nil)
@shoulda_subject.stubs(:index_name).returns('my_index')
@shoulda_subject.stubs(:client).returns(@client)
end

Expand Down Expand Up @@ -78,10 +79,11 @@ class MyDocument; end

should "pass options to the client" do
@client.expects(:exists).with do |arguments|
assert_equal 'bambam', arguments[:routing]
assert_equal 'foobarbam', arguments[:index]
assert_equal 'bambam', arguments[:routing]
end

subject.exists? '1', routing: 'bambam'
subject.exists? '1', index: 'foobarbam', routing: 'bambam'
end
end

Expand Down Expand Up @@ -155,25 +157,26 @@ class MyDocument; end
@client
.expects(:get)
.with do |arguments|
assert_equal 'bambam', arguments[:routing]
assert_equal 'foobarbam', arguments[:index]
assert_equal 'bambam', arguments[:routing]
end
.returns({'_source' => { 'foo' => 'bar' }})

subject.__find_one '1', routing: 'bambam'
subject.__find_one '1', index: 'foobarbam', routing: 'bambam'
end
end

context "'__find_many' method" do
setup do
@response = {"docs"=>
[ {"_index"=>"test",
[ {"_index"=>"my_index",
"_type"=>"note",
"_id"=>"1",
"_version"=>1,
"found"=>true,
"_source"=>{"id"=>"1", "title"=>"Test 1"}},

{"_index"=>"test",
{"_index"=>"my_index",
"_type"=>"note",
"_id"=>"2",
"_version"=>1,
Expand Down Expand Up @@ -241,20 +244,20 @@ class MyDocument; end

should "find keep missing documents in the result as nil" do
@response = {"docs"=>
[ {"_index"=>"test",
[ {"_index"=>"my_index",
"_type"=>"note",
"_id"=>"1",
"_version"=>1,
"found"=>true,
"_source"=>{"id"=>"1", "title"=>"Test 1"}},

{"_index"=>"test",
{"_index"=>"my_index",
"_type"=>"note",
"_id"=>"3",
"_version"=>1,
"found"=>false},

{"_index"=>"test",
{"_index"=>"my_index",
"_type"=>"note",
"_id"=>"2",
"_version"=>1,
Expand Down Expand Up @@ -299,11 +302,12 @@ class MyDocument; end
@client
.expects(:mget)
.with do |arguments|
assert_equal 'bambam', arguments[:routing]
assert_equal 'foobarbam', arguments[:index]
assert_equal 'bambam', arguments[:routing]
end
.returns(@response)

subject.__find_many ['1', '2'], routing: 'bambam'
subject.__find_many ['1', '2'], index: 'foobarbam', routing: 'bambam'
end
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ class MyDocument; end
{ "total" => 2,
"max_score" => 0.19,
"hits" =>
[{"_index" => "test",
[{"_index" => "my_index",
"_type" => "note",
"_id" => "1",
"_score" => 0.19,
"_source" => {"id" => 1, "title" => "Test 1"}},

{"_index" => "test",
{"_index" => "my_index",
"_type" => "note",
"_id" => "2",
"_score" => 0.19,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ class MyDocument; end

@client = mock
@shoulda_subject.stubs(:klass).returns(nil)
@shoulda_subject.stubs(:index_name).returns('test')
@shoulda_subject.stubs(:client).returns(@client)
end

Expand Down
7 changes: 5 additions & 2 deletions elasticsearch-persistence/test/unit/repository_store_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ class MyDocument; end

setup do
@shoulda_subject = Class.new() { include Elasticsearch::Persistence::Repository::Store }.new
@shoulda_subject.stubs(:index_name).returns('test')
end

context "save" do
Expand Down Expand Up @@ -49,11 +50,12 @@ class MyDocument; end

client = mock
client.expects(:index).with do |arguments|
assert_equal 'foobarbam', arguments[:index]
assert_equal 'bambam', arguments[:routing]
end
subject.expects(:client).returns(client)

subject.save({foo: 'bar'}, routing: 'bambam')
subject.save({foo: 'bar'}, { index: 'foobarbam', routing: 'bambam' })
end
end

Expand Down Expand Up @@ -109,11 +111,12 @@ class MyDocument; end

client = mock
client.expects(:delete).with do |arguments|
assert_equal 'foobarbam', arguments[:index]
assert_equal 'bambam', arguments[:routing]
end
subject.expects(:client).returns(client)

subject.delete('1', routing: 'bambam')
subject.delete('1', index: 'foobarbam', routing: 'bambam')
end
end
end
Expand Down