Skip to content

Commit 7165f3d

Browse files
committed
[STORE] Added the Serialize module
The repository uses two symmetric methods: `serialize` and `deserialize` to convert documents when: 1. passing them to the storage, 2. initializing Ruby objects when retrieving documents from storage Every repository can easily customize (overload) these methods, to provide (de)serialization for complex use-cases, such as storing PDF files or images in the storage. See: * https://www.braintreepayments.com/braintrust/untangle-domain-and-persistence-logic-with-curator * https://github.com/braintree/curator/blob/master/lib/curator/repository.rb
1 parent e5d9ffb commit 7165f3d

File tree

4 files changed

+77
-0
lines changed

4 files changed

+77
-0
lines changed

Diff for: elasticsearch-persistence/lib/elasticsearch/persistence.rb

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
require 'elasticsearch/persistence/client'
88
require 'elasticsearch/persistence/repository/naming'
9+
require 'elasticsearch/persistence/repository/serialize'
910
require 'elasticsearch/persistence/repository'
1011

1112
require 'elasticsearch/persistence/repository/class'

Diff for: elasticsearch-persistence/lib/elasticsearch/persistence/repository.rb

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ module Persistence
44
module Repository
55
include Elasticsearch::Persistence::Client
66
include Elasticsearch::Persistence::Repository::Naming
7+
include Elasticsearch::Persistence::Repository::Serialize
78

89
def new(options={}, &block)
910
Elasticsearch::Persistence::Repository::Class.new options, &block
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
module Elasticsearch
2+
module Persistence
3+
module Repository
4+
5+
module Serialize
6+
def serialize(document)
7+
document.to_hash
8+
end
9+
10+
def deserialize(document)
11+
_klass = klass || __get_klass_from_type(document['_type'])
12+
_klass.new document['_source']
13+
end
14+
end
15+
16+
end
17+
end
18+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
require 'test_helper'
2+
3+
class Elasticsearch::Persistence::RepositorySerializeTest < Test::Unit::TestCase
4+
context "The repository serialization" do
5+
class DummyDocument
6+
def to_hash
7+
{ foo: 'bar' }
8+
end
9+
end
10+
11+
class MyDocument; end
12+
13+
setup do
14+
@shoulda_subject = Class.new() { include Elasticsearch::Persistence::Repository::Serialize }.new
15+
end
16+
17+
context "serialize" do
18+
should "call #to_hash on passed object" do
19+
document = DummyDocument.new
20+
assert_equal( { foo: 'bar' }, subject.serialize(document))
21+
end
22+
end
23+
24+
context "deserialize" do
25+
should "get the class name from #klass" do
26+
subject.expects(:klass)
27+
.returns(MyDocument)
28+
29+
MyDocument.expects(:new)
30+
31+
subject.deserialize( {} )
32+
end
33+
34+
should "get the class name from Elasticsearch _type" do
35+
subject.expects(:klass)
36+
.returns(nil)
37+
38+
subject.expects(:__get_klass_from_type)
39+
.returns(MyDocument)
40+
41+
MyDocument.expects(:new)
42+
43+
subject.deserialize( {} )
44+
end
45+
46+
should "create the class instance with _source attributes" do
47+
subject.expects(:klass).returns(nil)
48+
49+
subject.expects(:__get_klass_from_type).returns(MyDocument)
50+
51+
MyDocument.expects(:new).with({ 'foo' => 'bar' })
52+
53+
subject.deserialize( {'_source' => { 'foo' => 'bar' } } )
54+
end
55+
end
56+
end
57+
end

0 commit comments

Comments
 (0)