entity_store 1.2.0 → 1.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/entity_store/event_bus.rb +16 -8
- data/lib/entity_store/store.rb +10 -5
- data/lib/entity_store/version.rb +1 -1
- data/spec/entity_store_spec.rb +54 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 2d66518229bc1a1e8b67466d02c8da159937290bcb23821e7271d581e8c9ba8e
|
4
|
+
data.tar.gz: 6ad5bb806da6d9741d35171e0833b647e98dab49685b842931641020344f1a06
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: bb6bbac0680c5b46ebce882cd171306585dd622cf6fcdaae2dda1e18ca789e072d781654a22ed52f19a90bad8c28bddb1a94987cca9141c904d6d79ef685b5d8
|
7
|
+
data.tar.gz: 7efcc35670e23d51147fa8128fed71dda6308ebe9f5cd2bf934ca928a1006705bea03c80cd621a3ccaed79065ac0393f38963de2c387e45650b03f6d60321695
|
@@ -4,11 +4,19 @@ module EntityStore
|
|
4
4
|
|
5
5
|
ALL_METHOD = :all_events
|
6
6
|
|
7
|
+
def initialize(event_subscribers = nil)
|
8
|
+
@_event_subscribers = event_subscribers if event_subscribers
|
9
|
+
end
|
10
|
+
|
11
|
+
def event_subscribers
|
12
|
+
@_event_subscribers || EntityStore::Config.event_subscribers
|
13
|
+
end
|
14
|
+
|
7
15
|
def publish(entity_type, event)
|
8
|
-
publish_to_feed
|
16
|
+
publish_to_feed(entity_type, event)
|
9
17
|
|
10
|
-
subscribers_to(event.receiver_name).each
|
11
|
-
subscribers_to_all.each
|
18
|
+
subscribers_to(event.receiver_name).each { |s| send_to_subscriber(s, event.receiver_name, event) }
|
19
|
+
subscribers_to_all.each { |s| send_to_subscriber(s, ALL_METHOD, event) }
|
12
20
|
end
|
13
21
|
|
14
22
|
def send_to_subscriber subscriber, receiver_name, event
|
@@ -27,7 +35,7 @@ module EntityStore
|
|
27
35
|
end
|
28
36
|
|
29
37
|
def subscribers
|
30
|
-
|
38
|
+
event_subscribers.map do |subscriber|
|
31
39
|
case subscriber
|
32
40
|
when String
|
33
41
|
Utils.get_type_constant(subscriber)
|
@@ -46,17 +54,17 @@ module EntityStore
|
|
46
54
|
end
|
47
55
|
|
48
56
|
# Public - replay events of a given type to a given subscriber
|
49
|
-
#
|
57
|
+
#
|
50
58
|
# since - Time reference point
|
51
59
|
# type - String type name of event
|
52
|
-
# subscriber - Class of the subscriber to replay events to
|
53
|
-
#
|
60
|
+
# subscriber - Class of the subscriber to replay events to
|
61
|
+
#
|
54
62
|
# Returns nothing
|
55
63
|
def replay(since, type, subscriber)
|
56
64
|
max_items = 100
|
57
65
|
event_data_objects = feed_store.get_events(since, type, max_items)
|
58
66
|
|
59
|
-
while event_data_objects.count > 0 do
|
67
|
+
while event_data_objects.count > 0 do
|
60
68
|
event_data_objects.each do |event_data_object|
|
61
69
|
begin
|
62
70
|
event = EntityStore::Config.load_type(event_data_object.type).new(event_data_object.attrs)
|
data/lib/entity_store/store.rb
CHANGED
@@ -2,10 +2,19 @@ module EntityStore
|
|
2
2
|
class Store
|
3
3
|
include Logging
|
4
4
|
|
5
|
+
def initialize(storage_client = nil, event_bus = nil)
|
6
|
+
@_storage_client = storage_client if storage_client
|
7
|
+
@_event_bus = event_bus if event_bus
|
8
|
+
end
|
9
|
+
|
5
10
|
def storage_client
|
6
11
|
@_storage_client ||= EntityStore::Config.store
|
7
12
|
end
|
8
13
|
|
14
|
+
def event_bus
|
15
|
+
@_event_bus ||= EventBus.new
|
16
|
+
end
|
17
|
+
|
9
18
|
def add(entity)
|
10
19
|
entity.id = storage_client.add_entity(entity)
|
11
20
|
add_events(entity)
|
@@ -71,7 +80,7 @@ module EntityStore
|
|
71
80
|
|
72
81
|
yield if block_given?
|
73
82
|
|
74
|
-
items.each {|e| event_bus.publish(entity.type, e) }
|
83
|
+
items.each { |e| event_bus.publish(entity.type, e) }
|
75
84
|
|
76
85
|
entity.clear_pending_events
|
77
86
|
end
|
@@ -146,10 +155,6 @@ module EntityStore
|
|
146
155
|
@_storage_client = nil
|
147
156
|
end
|
148
157
|
|
149
|
-
def event_bus
|
150
|
-
@_event_bus ||= EventBus.new
|
151
|
-
end
|
152
|
-
|
153
158
|
# Public: returns an array representing a full audit trail for the entity.
|
154
159
|
# After each event is applied the state of the entity is rendered.
|
155
160
|
# Optionally accepts a block which should return true or false to indicate
|
data/lib/entity_store/version.rb
CHANGED
data/spec/entity_store_spec.rb
CHANGED
@@ -26,7 +26,17 @@ class DummyEntitySubscriber
|
|
26
26
|
end
|
27
27
|
|
28
28
|
def dummy_entity_name_set(event)
|
29
|
-
|
29
|
+
self.class.event_name = event.name
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
class AnotherEntitySubscriber
|
34
|
+
class << self
|
35
|
+
attr_accessor :event_name
|
36
|
+
end
|
37
|
+
|
38
|
+
def dummy_entity_name_set(event)
|
39
|
+
self.class.event_name = event.name
|
30
40
|
end
|
31
41
|
end
|
32
42
|
|
@@ -80,6 +90,49 @@ class DummyStore
|
|
80
90
|
end
|
81
91
|
end
|
82
92
|
|
93
|
+
describe "creation without static instances" do
|
94
|
+
let(:store) do
|
95
|
+
storage_client = DummyStore.new
|
96
|
+
Store.new(storage_client, event_bus)
|
97
|
+
end
|
98
|
+
|
99
|
+
let(:event_bus) do
|
100
|
+
event_subscribers = []
|
101
|
+
event_subscribers << AnotherEntitySubscriber
|
102
|
+
|
103
|
+
EventBus.new(event_subscribers)
|
104
|
+
end
|
105
|
+
|
106
|
+
before do
|
107
|
+
EntityStore::Config.setup do |config|
|
108
|
+
config.store = DummyStore.new
|
109
|
+
config.event_subscribers << DummyEntitySubscriber
|
110
|
+
end
|
111
|
+
end
|
112
|
+
|
113
|
+
context "when save entity" do
|
114
|
+
let(:name) { random_string }
|
115
|
+
before(:each) do
|
116
|
+
@entity = DummyEntity.new
|
117
|
+
@entity.set_name name
|
118
|
+
@id = store.save @entity
|
119
|
+
end
|
120
|
+
|
121
|
+
it "does not publish event to the non configured subscriber" do
|
122
|
+
DummyEntitySubscriber.event_name.should_not eq(name)
|
123
|
+
end
|
124
|
+
it "publishes event to the subscriber" do
|
125
|
+
AnotherEntitySubscriber.event_name.should eq(name)
|
126
|
+
end
|
127
|
+
it "is retrievable with the events applied" do
|
128
|
+
store.get(@entity.id).name.should eq(name)
|
129
|
+
end
|
130
|
+
it "is not retrievable from the static store instance" do
|
131
|
+
EntityStore::Store.new.get(@entity.id).should eq(nil)
|
132
|
+
end
|
133
|
+
end
|
134
|
+
end
|
135
|
+
|
83
136
|
describe "end to end" do
|
84
137
|
before(:each) do
|
85
138
|
EntityStore::Config.setup do |config|
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: entity_store
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Adam Bird
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-
|
11
|
+
date: 2018-10-24 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bson
|