File tree 4 files changed +77
-0
lines changed
elasticsearch-persistence
4 files changed +77
-0
lines changed Original file line number Diff line number Diff line change 6
6
7
7
require 'elasticsearch/persistence/client'
8
8
require 'elasticsearch/persistence/repository/naming'
9
+ require 'elasticsearch/persistence/repository/serialize'
9
10
require 'elasticsearch/persistence/repository'
10
11
11
12
require 'elasticsearch/persistence/repository/class'
Original file line number Diff line number Diff line change @@ -4,6 +4,7 @@ module Persistence
4
4
module Repository
5
5
include Elasticsearch ::Persistence ::Client
6
6
include Elasticsearch ::Persistence ::Repository ::Naming
7
+ include Elasticsearch ::Persistence ::Repository ::Serialize
7
8
8
9
def new ( options = { } , &block )
9
10
Elasticsearch ::Persistence ::Repository ::Class . new options , &block
Original file line number Diff line number Diff line change
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 number Diff line number Diff line change
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
You can’t perform that action at this time.
0 commit comments