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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 02a69a4134e4ef998a3258424ed1ff35e171babf83d72665ab37464aaba1abbe
4
- data.tar.gz: 2b9af465f21d0131c71b80cd3a9e5fb08579ebfbe27e9c2595e45b9ccffba622
3
+ metadata.gz: 2d66518229bc1a1e8b67466d02c8da159937290bcb23821e7271d581e8c9ba8e
4
+ data.tar.gz: 6ad5bb806da6d9741d35171e0833b647e98dab49685b842931641020344f1a06
5
5
  SHA512:
6
- metadata.gz: 893058a17b8cef182d8a7349ad03107b00361d66af25837f44846aa8daed83cf9bbc38cef57874a753e3d79e7bf47e8c66a4612a25564a893f5188fde7e7119a
7
- data.tar.gz: 8b10d320996be80cfc718fa709339132261104b0edb7b6fe3b286757dd5ec9f3535400d1214a6d7af4ef21abc4fb612e47ef0bbdfde9c99a290c34bee226cc48
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 entity_type, event
16
+ publish_to_feed(entity_type, event)
9
17
 
10
- subscribers_to(event.receiver_name).each do |s| send_to_subscriber s, event.receiver_name, event end
11
- subscribers_to_all.each do |s| send_to_subscriber s, ALL_METHOD, event end
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
- EntityStore::Config.event_subscribers.map do |subscriber|
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)
@@ -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
@@ -1,3 +1,3 @@
1
1
  module EntityStore
2
- VERSION = "1.2.0".freeze
2
+ VERSION = "1.3.0".freeze
3
3
  end
@@ -26,7 +26,17 @@ class DummyEntitySubscriber
26
26
  end
27
27
 
28
28
  def dummy_entity_name_set(event)
29
- DummyEntitySubscriber.event_name = event.name
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.2.0
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-03-12 00:00:00.000000000 Z
11
+ date: 2018-10-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bson