entity_store 0.1.0 → 0.1.3
Sign up to get free protection for your applications and to get access to all the features.
@@ -12,7 +12,7 @@ module EntityStore
|
|
12
12
|
|
13
13
|
def open_store
|
14
14
|
uri = URI.parse(EntityStore.connection_profile)
|
15
|
-
connection = Connection.from_uri(EntityStore.connection_profile, :connect_timeout => connect_timeout
|
15
|
+
connection = Connection.from_uri(EntityStore.connection_profile, :connect_timeout => connect_timeout)
|
16
16
|
connection.db(uri.path.gsub(/^\//, ''))
|
17
17
|
end
|
18
18
|
|
@@ -72,13 +72,14 @@ module EntityStore
|
|
72
72
|
# Returns an object of the entity type
|
73
73
|
def get_entity(id, raise_exception=false)
|
74
74
|
if attrs = entities.find_one('_id' => BSON::ObjectId.from_string(id))
|
75
|
-
entity =
|
75
|
+
entity = EntityStore.load_type(attrs['_type']).new(attrs['snapshot'] || {'id' => id, 'version' => attrs['version']})
|
76
76
|
|
77
77
|
since_version = attrs['snapshot'] ? attrs['snapshot']['version'] : nil
|
78
78
|
|
79
79
|
get_events(id, since_version).each do |event|
|
80
80
|
begin
|
81
81
|
event.apply(entity)
|
82
|
+
logger.debug { "Applied #{event.inspect} to #{id}" }
|
82
83
|
rescue => e
|
83
84
|
logger.error "Failed to apply #{event.class.name} #{event.attributes} to #{id} with #{e.inspect}", e
|
84
85
|
end
|
@@ -106,7 +107,7 @@ module EntityStore
|
|
106
107
|
|
107
108
|
events.find(query, options).collect do |attrs|
|
108
109
|
begin
|
109
|
-
|
110
|
+
EntityStore.load_type(attrs['_type']).new(attrs)
|
110
111
|
rescue => e
|
111
112
|
logger.error "Error loading type #{attrs['_type']}", e
|
112
113
|
nil
|
@@ -114,8 +115,8 @@ module EntityStore
|
|
114
115
|
end.select { |e| !e.nil? }
|
115
116
|
end
|
116
117
|
|
117
|
-
def get_type_constant(type_name)
|
118
|
-
|
119
|
-
end
|
118
|
+
# def get_type_constant(type_name)
|
119
|
+
# type_name.split('::').inject(Object) {|obj, name| obj.const_get(name) }
|
120
|
+
# end
|
120
121
|
end
|
121
122
|
end
|
data/lib/entity_store/version.rb
CHANGED
data/lib/entity_store.rb
CHANGED
@@ -14,6 +14,7 @@ module EntityStore
|
|
14
14
|
require 'entity_store/attributes'
|
15
15
|
|
16
16
|
class << self
|
17
|
+
|
17
18
|
def setup
|
18
19
|
yield self
|
19
20
|
end
|
@@ -48,6 +49,19 @@ module EntityStore
|
|
48
49
|
@_snapshot_threshold = value
|
49
50
|
end
|
50
51
|
|
52
|
+
# Allows config to pass in a lambda or Proc to use as the type loader in place
|
53
|
+
# of the default.
|
54
|
+
# Original use case was migration of entity classes to new module namespace when
|
55
|
+
# extracting to a shared library
|
56
|
+
attr_accessor :type_loader
|
57
|
+
|
58
|
+
def load_type(type_name)
|
59
|
+
if type_loader
|
60
|
+
type_loader.call(type_name)
|
61
|
+
else
|
62
|
+
type_name.split('::').inject(Object) {|obj, name| obj.const_get(name) }
|
63
|
+
end
|
64
|
+
end
|
51
65
|
end
|
52
66
|
|
53
67
|
|
@@ -1,12 +1,5 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
|
-
module Level1
|
4
|
-
module Level2
|
5
|
-
class MyClass
|
6
|
-
end
|
7
|
-
end
|
8
|
-
end
|
9
|
-
|
10
3
|
module MongoEntityStoreSpec
|
11
4
|
class DummyEntity
|
12
5
|
include Entity
|
@@ -103,15 +96,6 @@ describe MongoEntityStore do
|
|
103
96
|
|
104
97
|
end
|
105
98
|
|
106
|
-
describe "get_type_constant" do
|
107
|
-
|
108
|
-
subject { @store.get_type_constant('Level1::Level2::MyClass') }
|
109
|
-
|
110
|
-
it "should be an Level1::Level2::MyClass" do
|
111
|
-
subject.should eq(Level1::Level2::MyClass)
|
112
|
-
end
|
113
|
-
end
|
114
|
-
|
115
99
|
describe "#snapshot_entity" do
|
116
100
|
before(:each) do
|
117
101
|
@entity = MongoEntityStoreSpec::DummyEntity.new(:id => random_object_id, :version => random_integer, :name => random_string)
|
data/spec/entity_store_spec.rb
CHANGED
@@ -1,8 +1,34 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
|
+
module Level1
|
4
|
+
module Level2
|
5
|
+
class MyClass
|
6
|
+
end
|
7
|
+
class AnotherClass
|
8
|
+
end
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
3
12
|
describe EntityStore do
|
4
|
-
describe ".setup" do
|
5
13
|
|
14
|
+
describe "load_type" do
|
15
|
+
|
16
|
+
subject { EntityStore.load_type('Level1::Level2::MyClass') }
|
17
|
+
|
18
|
+
it "should be an Level1::Level2::MyClass" do
|
19
|
+
subject.should eq(Level1::Level2::MyClass)
|
20
|
+
end
|
21
|
+
|
22
|
+
context "when type_loader set" do
|
23
|
+
before(:each) do
|
24
|
+
EntityStore.type_loader = lambda { |type_name|
|
25
|
+
Level1::Level2::AnotherClass
|
26
|
+
}
|
27
|
+
end
|
28
|
+
|
29
|
+
it "should return the result of that type loader" do
|
30
|
+
subject.should eq(Level1::Level2::AnotherClass)
|
31
|
+
end
|
32
|
+
end
|
6
33
|
end
|
7
|
-
|
8
34
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: entity_store
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.3
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-11-
|
12
|
+
date: 2012-11-29 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: mongo
|