sandthorn 0.9.2 → 0.10.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +29 -1
- data/lib/sandthorn/aggregate_root.rb +4 -2
- data/lib/sandthorn/aggregate_root_base.rb +35 -21
- data/lib/sandthorn/aggregate_root_marshal.rb +0 -3
- data/lib/sandthorn/version.rb +1 -1
- data/spec/aggregate_events_spec.rb +88 -0
- data/spec/spec_helper.rb +4 -0
- metadata +5 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e447ae84df794de89797c576b53a8e958bf5836f
|
4
|
+
data.tar.gz: 175d7f6a64b7ec05ae870e1cf838be1d6ff90ce6
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 77c020e0af49d6ecc03314e879c22b76e361659648e6163dde7b090b897250b163ce7aeaaa440714eeba6ebd619b0e64b3ca20aafd501184ca9e795bd6c90476
|
7
|
+
data.tar.gz: 1cd42f78aa82b503676e22d469cd373edd6cda4f819716c7b0787392c8dfe9404aa950b201ec4aab036c2c6c852c298ebc3097891e59b0895e0923b1b8bab693
|
data/README.md
CHANGED
@@ -64,7 +64,7 @@ class Ship
|
|
64
64
|
end
|
65
65
|
end
|
66
66
|
|
67
|
-
# Configure
|
67
|
+
# Configure one driver
|
68
68
|
url = "sqlite://spec/db/sequel_driver.sqlite3"
|
69
69
|
sql_event_store = SandthornDriverSequel.driver_from_url(url: url)
|
70
70
|
Sandthorn.configure do |c|
|
@@ -72,6 +72,10 @@ Sandthorn.configure do |c|
|
|
72
72
|
end
|
73
73
|
|
74
74
|
# Or configure many drivers
|
75
|
+
url = "sqlite://spec/db/sequel_driver.sqlite3"
|
76
|
+
sql_event_store = SandthornDriverSequel.driver_from_url(url: url)
|
77
|
+
url_two = "sqlite://spec/db/sequel_driver_two.sqlite3"
|
78
|
+
other_store = SandthornDriverSequel.driver_from_url(url: url_two)
|
75
79
|
|
76
80
|
Sandthorn.configure do |c|
|
77
81
|
c.event_stores = {
|
@@ -208,6 +212,28 @@ end
|
|
208
212
|
|
209
213
|
All objects that include `Sandthorn::AggregateRoot` is provided with an `aggregate_id` which is a [UUID](http://en.wikipedia.org/wiki/Universally_unique_identifier).
|
210
214
|
|
215
|
+
### `Sandthorn::AggregateRoot::events`
|
216
|
+
|
217
|
+
An abstraction over `commit` that creates events methods that can be used from within a command method.
|
218
|
+
|
219
|
+
In this exampel the `events` method will generate a method called `marked`, this method take *args as input that will result in the method argument on the event. It also take a block that will be executed before the event is commited and is used to groups the state changes to the event (but is only optional right now).
|
220
|
+
|
221
|
+
```ruby
|
222
|
+
class Board
|
223
|
+
include Sandthorn::AggregateRoot
|
224
|
+
|
225
|
+
events :marked
|
226
|
+
|
227
|
+
def mark player, pos_x, pos_y
|
228
|
+
# change some state
|
229
|
+
marked(player) do
|
230
|
+
@pos_x = pos_x
|
231
|
+
@pos_y = pos_y
|
232
|
+
end
|
233
|
+
end
|
234
|
+
end
|
235
|
+
```
|
236
|
+
|
211
237
|
### `Sandthorn::AggregateRoot.commit`
|
212
238
|
|
213
239
|
It is required that an event is commited to the aggregate to be stored as an event. `commit` extracts the object's delta and locally caches the state changes that has been applied to the aggregate. Commonly, commit is called when an event is applied. In [CQRS](http://martinfowler.com/bliki/CQRS.html), events are named using past tense.
|
@@ -225,6 +251,8 @@ end
|
|
225
251
|
|
226
252
|
`commit` determines the state changes by monitoring the object's readable fields.
|
227
253
|
|
254
|
+
Since version 0.10.0 of Sandthorn the concept `events` have been introduced to abstract away the usage of `commit`. Commit still works as before but we think that the `events` abstraction makes the aggregate more readable.
|
255
|
+
|
228
256
|
### `Sandthorn::AggregateRoot.save`
|
229
257
|
|
230
258
|
Once one or more commits have been applied to an aggregate it should be saved. This means all commited events will be persisted by the specific Sandthorn driver. `save` is called by the owning object.
|
@@ -1,11 +1,13 @@
|
|
1
|
+
require 'sandthorn/aggregate_root_base'
|
1
2
|
require 'sandthorn/aggregate_root_marshal'
|
2
3
|
|
3
4
|
module Sandthorn
|
4
5
|
module AggregateRoot
|
5
|
-
include
|
6
|
+
include Base
|
7
|
+
include Marshal
|
6
8
|
|
7
9
|
def self.included(base)
|
8
|
-
base.extend(
|
10
|
+
base.extend(Base::ClassMethods)
|
9
11
|
end
|
10
12
|
end
|
11
13
|
end
|
@@ -48,27 +48,8 @@ module Sandthorn
|
|
48
48
|
end
|
49
49
|
|
50
50
|
def commit *args
|
51
|
-
|
52
|
-
|
53
|
-
method_name = caller_locations(1,1)[0].label.gsub(/block ?(.*) in /, "")
|
54
|
-
increase_current_aggregate_version!
|
55
|
-
data = {
|
56
|
-
method_name: method_name,
|
57
|
-
method_args: args,
|
58
|
-
attribute_deltas: aggregate_attribute_deltas
|
59
|
-
}
|
60
|
-
trace_information = @aggregate_trace_information
|
61
|
-
unless trace_information.nil? || trace_information.empty?
|
62
|
-
data.merge!({ trace: trace_information })
|
63
|
-
end
|
64
|
-
|
65
|
-
@aggregate_events << ({
|
66
|
-
aggregate_version: @aggregate_current_event_version,
|
67
|
-
event_name: method_name,
|
68
|
-
event_args: data
|
69
|
-
})
|
70
|
-
|
71
|
-
self
|
51
|
+
event_name = caller_locations(1,1)[0].label.gsub(/block ?(.*) in /, "")
|
52
|
+
commit_with_event_name(event_name, args)
|
72
53
|
end
|
73
54
|
|
74
55
|
alias :record_event :commit
|
@@ -150,6 +131,16 @@ module Sandthorn
|
|
150
131
|
aggregate
|
151
132
|
end
|
152
133
|
|
134
|
+
def events(*event_names)
|
135
|
+
event_names.each do |name|
|
136
|
+
define_method(name) do |*args, &block|
|
137
|
+
block.call() if block
|
138
|
+
commit_with_event_name(name, args)
|
139
|
+
end
|
140
|
+
private name.to_s
|
141
|
+
end
|
142
|
+
end
|
143
|
+
|
153
144
|
private
|
154
145
|
|
155
146
|
def build_instance_vars_from_events events
|
@@ -220,6 +211,29 @@ module Sandthorn
|
|
220
211
|
@aggregate_id = aggregate_id
|
221
212
|
end
|
222
213
|
|
214
|
+
def commit_with_event_name(event_name, *args)
|
215
|
+
aggregate_attribute_deltas = get_delta
|
216
|
+
|
217
|
+
increase_current_aggregate_version!
|
218
|
+
data = {
|
219
|
+
method_name: event_name,
|
220
|
+
method_args: args,
|
221
|
+
attribute_deltas: aggregate_attribute_deltas
|
222
|
+
}
|
223
|
+
trace_information = @aggregate_trace_information
|
224
|
+
unless trace_information.nil? || trace_information.empty?
|
225
|
+
data.merge!({ trace: trace_information })
|
226
|
+
end
|
227
|
+
|
228
|
+
@aggregate_events << ({
|
229
|
+
aggregate_version: @aggregate_current_event_version,
|
230
|
+
event_name: event_name,
|
231
|
+
event_args: data
|
232
|
+
})
|
233
|
+
|
234
|
+
self
|
235
|
+
end
|
236
|
+
|
223
237
|
end
|
224
238
|
end
|
225
239
|
end
|
data/lib/sandthorn/version.rb
CHANGED
@@ -0,0 +1,88 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
module Sandthorn
|
4
|
+
class EventsSpec
|
5
|
+
include AggregateRoot
|
6
|
+
|
7
|
+
events :name_changed, :some_other_event, :third_event
|
8
|
+
attr_reader :name
|
9
|
+
|
10
|
+
def change_name(name)
|
11
|
+
if @name != name
|
12
|
+
name_changed(name) { @name = name }
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
def some_other one, two
|
17
|
+
some_other_event one, two
|
18
|
+
end
|
19
|
+
|
20
|
+
def old_way_event event_params
|
21
|
+
commit event_params
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
describe "::events" do
|
26
|
+
|
27
|
+
let(:subject) do
|
28
|
+
EventsSpec.new.extend EventInspector
|
29
|
+
end
|
30
|
+
|
31
|
+
it "should not expose events methods" do
|
32
|
+
expect(subject).not_to respond_to(:name_changed)
|
33
|
+
end
|
34
|
+
|
35
|
+
it "should make the events methods private" do
|
36
|
+
expect(subject.private_methods).to include(:name_changed)
|
37
|
+
end
|
38
|
+
|
39
|
+
describe ".change_name" do
|
40
|
+
|
41
|
+
before do
|
42
|
+
subject.change_name "new name"
|
43
|
+
end
|
44
|
+
|
45
|
+
it "should set the name instance variable" do
|
46
|
+
expect(subject.name).to eql "new name"
|
47
|
+
end
|
48
|
+
|
49
|
+
it "should store the event params as methods args" do
|
50
|
+
expect(subject.has_event?(:name_changed)).to be_truthy
|
51
|
+
end
|
52
|
+
|
53
|
+
it "should store the args to the event" do
|
54
|
+
expect(subject.aggregate_events[1][:event_args][:method_args][0][0]).to eql("new name")
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
describe ".some_other" do
|
59
|
+
|
60
|
+
before do
|
61
|
+
subject.some_other 1, 2
|
62
|
+
end
|
63
|
+
|
64
|
+
it "should store the event" do
|
65
|
+
expect(subject.has_event?(:some_other_event)).to be_truthy
|
66
|
+
end
|
67
|
+
|
68
|
+
it "should store the args to the event" do
|
69
|
+
expect(subject.aggregate_events[1][:event_args][:method_args][0]).to eql([1,2])
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
describe ".old_way_event" do
|
74
|
+
|
75
|
+
before do
|
76
|
+
subject.old_way_event "hej"
|
77
|
+
end
|
78
|
+
|
79
|
+
it "should store the event the old way" do
|
80
|
+
expect(subject.has_event?(:old_way_event)).to be_truthy
|
81
|
+
end
|
82
|
+
|
83
|
+
it "should store the args to the event" do
|
84
|
+
expect(subject.aggregate_events[1][:event_args][:method_args][0][0]).to eql("hej")
|
85
|
+
end
|
86
|
+
end
|
87
|
+
end
|
88
|
+
end
|
data/spec/spec_helper.rb
CHANGED
@@ -30,6 +30,10 @@ RSpec.configure do |config|
|
|
30
30
|
# --seed 1234
|
31
31
|
config.order = 'random'
|
32
32
|
config.before(:each) { sqlite_store_setup }
|
33
|
+
|
34
|
+
config.after(:each) do
|
35
|
+
Sandthorn.event_stores.default_store.driver.instance_variable_get(:@db).disconnect
|
36
|
+
end
|
33
37
|
end
|
34
38
|
|
35
39
|
def spec_db
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: sandthorn
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.10.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Lars Krantz
|
@@ -10,7 +10,7 @@ authors:
|
|
10
10
|
autorequire:
|
11
11
|
bindir: bin
|
12
12
|
cert_chain: []
|
13
|
-
date:
|
13
|
+
date: 2016-01-25 00:00:00.000000000 Z
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
16
|
name: bundler
|
@@ -200,6 +200,7 @@ files:
|
|
200
200
|
- lib/sandthorn/version.rb
|
201
201
|
- sandthorn.gemspec
|
202
202
|
- spec/aggregate_delta_spec.rb
|
203
|
+
- spec/aggregate_events_spec.rb
|
203
204
|
- spec/aggregate_root_spec.rb
|
204
205
|
- spec/aggregate_snapshot_spec.rb
|
205
206
|
- spec/benchmark_spec.rb
|
@@ -236,12 +237,13 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
236
237
|
version: '0'
|
237
238
|
requirements: []
|
238
239
|
rubyforge_project:
|
239
|
-
rubygems_version: 2.4.
|
240
|
+
rubygems_version: 2.4.8
|
240
241
|
signing_key:
|
241
242
|
specification_version: 4
|
242
243
|
summary: Event sourcing gem
|
243
244
|
test_files:
|
244
245
|
- spec/aggregate_delta_spec.rb
|
246
|
+
- spec/aggregate_events_spec.rb
|
245
247
|
- spec/aggregate_root_spec.rb
|
246
248
|
- spec/aggregate_snapshot_spec.rb
|
247
249
|
- spec/benchmark_spec.rb
|