simple_event_sourcing 0.5.0 → 0.6.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/Gemfile.lock +1 -1
- data/README.md +25 -11
- data/docker-compose.yml +0 -14
- data/lib/simple_event_sourcing/aggregate_root/base.rb +4 -2
- data/lib/simple_event_sourcing/events/event.rb +5 -2
- data/lib/simple_event_sourcing/version.rb +1 -1
- metadata +1 -3
- data/example/employee.rb +0 -77
- data/example/main.rb +0 -16
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e192e7a999101b1dfa36ad18d04e20d9eab59216d9411244dbe62b7fdb98e3c8
|
4
|
+
data.tar.gz: e638c2abce2661ae35ffa228bf22482a9ce76c9d96ed45baa18e5288f67cadf2
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b20c72225d5a1db39aea56c3e590de1270a2d2f789a913a05148fd1763dada7a4c9b6da74a9253c9f111f9bd1cedb20ba7140c19bb41a22154e8308b59a01dff
|
7
|
+
data.tar.gz: 81e98f3fe9cec6dbf5e8a9907ae41535d13abf3b4f793f32becb692f8aecf1aa3279f38f86dfe0b8a3f25f54afaacfa12ec0ada46d7f5882389d91e59f7260b9
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -43,19 +43,19 @@ Here an example of use:
|
|
43
43
|
```ruby
|
44
44
|
class Employee
|
45
45
|
|
46
|
-
include SimpleEventSourcing::AggregateRoot
|
46
|
+
include SimpleEventSourcing::AggregateRoot::Base
|
47
47
|
|
48
48
|
attr_reader :name, :title, :salary
|
49
49
|
|
50
50
|
def initialize(args = nil )
|
51
51
|
super
|
52
52
|
unless args.nil?
|
53
|
-
apply_record_event NewEmployeeIsHiredEvent
|
53
|
+
apply_record_event NewEmployeeIsHiredEvent , name: args[:name], title: args[:title], salary: args[:salary]
|
54
54
|
end
|
55
55
|
end
|
56
56
|
|
57
57
|
def salary=(new_salary)
|
58
|
-
apply_record_event SalaryHasChangedEvent
|
58
|
+
apply_record_event SalaryHasChangedEvent , new_salary: new_salary
|
59
59
|
end
|
60
60
|
|
61
61
|
on NewEmployeeIsHiredEvent do |event|
|
@@ -74,12 +74,13 @@ class Employee
|
|
74
74
|
end
|
75
75
|
|
76
76
|
end
|
77
|
+
|
77
78
|
```
|
78
79
|
|
79
80
|
First, you must add behaviour including the AggregateRoot module
|
80
81
|
|
81
82
|
```ruby
|
82
|
-
include SimpleEventSourcing::AggregateRoot
|
83
|
+
include SimpleEventSourcing::AggregateRoot::Base
|
83
84
|
```
|
84
85
|
|
85
86
|
You must create your own domain events and a event stream
|
@@ -91,13 +92,24 @@ class EmployeeStreamEvents < SimpleEventSourcing::AggregateRoot::History
|
|
91
92
|
end
|
92
93
|
end
|
93
94
|
|
94
|
-
class SalaryHasChangedEvent < SimpleEventSourcing::Event
|
95
|
-
attr_reader :aggregate_id, :new_salary
|
96
95
|
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
96
|
+
class NewEmployeeIsHiredEvent < SimpleEventSourcing::Events::Event
|
97
|
+
attr_reader :name, :title,:salary
|
98
|
+
|
99
|
+
def initialize(args)
|
100
|
+
@name = args[:name]
|
101
|
+
@title = args[:title]
|
102
|
+
@salary = args[:salary]
|
103
|
+
super(args)
|
104
|
+
end
|
105
|
+
end
|
106
|
+
|
107
|
+
class SalaryHasChangedEvent < SimpleEventSourcing::Events::Event
|
108
|
+
attr_reader :new_salary
|
109
|
+
|
110
|
+
def initialize(args)
|
111
|
+
@new_salary = args[:new_salary]
|
112
|
+
super(args)
|
101
113
|
end
|
102
114
|
end
|
103
115
|
```
|
@@ -105,7 +117,7 @@ end
|
|
105
117
|
After that all domain event must be applied and recorded
|
106
118
|
|
107
119
|
```ruby
|
108
|
-
apply_record_event SalaryHasChangedEvent
|
120
|
+
apply_record_event SalaryHasChangedEvent , new_salary: new_salary
|
109
121
|
```
|
110
122
|
|
111
123
|
SimpleEventSourcing provides a DSL to handle the applied events. You must provide a handler for each event
|
@@ -125,6 +137,8 @@ Once you persist the entity you must publish all recorded events.
|
|
125
137
|
end
|
126
138
|
```
|
127
139
|
|
140
|
+
You could see this example in https://github.com/malotor/simple_event_sourcing_example
|
141
|
+
|
128
142
|
Happy coding!
|
129
143
|
|
130
144
|
## Contributing
|
data/docker-compose.yml
CHANGED
@@ -7,17 +7,3 @@ services:
|
|
7
7
|
command: tail -f /dev/null
|
8
8
|
environment:
|
9
9
|
- LANG=C.UTF-8
|
10
|
-
redis:
|
11
|
-
container_name: redis
|
12
|
-
image: redis:3.2-alpine
|
13
|
-
hostname: redis
|
14
|
-
redis-gui:
|
15
|
-
# image: emmenko/redis-commander:0.3.2
|
16
|
-
image: tenstartups/redis-commander
|
17
|
-
container_name: redis-gui
|
18
|
-
command: --redis-host redis
|
19
|
-
restart: unless-stopped
|
20
|
-
ports:
|
21
|
-
- "50000:8081"
|
22
|
-
depends_on:
|
23
|
-
- redis
|
@@ -54,7 +54,9 @@ module SimpleEventSourcing
|
|
54
54
|
|
55
55
|
private
|
56
56
|
|
57
|
-
def apply_record_event(
|
57
|
+
def apply_record_event(event_class, args = {})
|
58
|
+
args[:aggregate_id] ||= aggregate_id
|
59
|
+
event = event_class.new(args)
|
58
60
|
handle_message(event)
|
59
61
|
record_event event
|
60
62
|
end
|
@@ -67,7 +69,7 @@ module SimpleEventSourcing
|
|
67
69
|
@events = []
|
68
70
|
end
|
69
71
|
|
70
|
-
|
72
|
+
|
71
73
|
end
|
72
74
|
end
|
73
75
|
end
|
@@ -2,9 +2,12 @@ module SimpleEventSourcing
|
|
2
2
|
module Events
|
3
3
|
|
4
4
|
class Event
|
5
|
-
attr_reader :occured_on
|
5
|
+
attr_reader :aggregate_id, :occured_on
|
6
6
|
|
7
|
-
def initialize
|
7
|
+
def initialize(args)
|
8
|
+
puts args.inspect
|
9
|
+
|
10
|
+
@aggregate_id = args[:aggregate_id]
|
8
11
|
@occured_on ||= Time.new
|
9
12
|
end
|
10
13
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: simple_event_sourcing
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.6.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Manuel López Torrent
|
@@ -102,8 +102,6 @@ files:
|
|
102
102
|
- bin/console
|
103
103
|
- bin/setup
|
104
104
|
- docker-compose.yml
|
105
|
-
- example/employee.rb
|
106
|
-
- example/main.rb
|
107
105
|
- lib/simple_event_sourcing.rb
|
108
106
|
- lib/simple_event_sourcing/aggregate_root/base.rb
|
109
107
|
- lib/simple_event_sourcing/aggregate_root/history.rb
|
data/example/employee.rb
DELETED
@@ -1,77 +0,0 @@
|
|
1
|
-
require_relative '../lib/simple_event_sourcing'
|
2
|
-
|
3
|
-
class EmployeeStreamEvents < SimpleEventSourcing::AggregateRoot::History
|
4
|
-
def get_aggregate_class
|
5
|
-
Employee
|
6
|
-
end
|
7
|
-
end
|
8
|
-
|
9
|
-
|
10
|
-
class NewEmployeeIsHiredEvent < SimpleEventSourcing::Events::Event
|
11
|
-
attr_reader :name, :title,:salary
|
12
|
-
|
13
|
-
def initialize(aggregate_id, name, title, salary)
|
14
|
-
@aggregate_id = aggregate_id
|
15
|
-
@name = name
|
16
|
-
@title = title
|
17
|
-
@salary = salary
|
18
|
-
super()
|
19
|
-
end
|
20
|
-
end
|
21
|
-
|
22
|
-
class SalaryHasChangedEvent < SimpleEventSourcing::Events::Event
|
23
|
-
attr_reader :aggregate_id, :new_salary
|
24
|
-
|
25
|
-
def initialize(aggregate_id, new_salary)
|
26
|
-
@aggregate_id = aggregate_id
|
27
|
-
@new_salary = new_salary
|
28
|
-
super()
|
29
|
-
end
|
30
|
-
end
|
31
|
-
|
32
|
-
class CongratulateEmployeeSubscriber < SimpleEventSourcing::Events::EventSubscriber
|
33
|
-
|
34
|
-
def is_subscribet_to?(event)
|
35
|
-
event.class == SalaryHasChangedEvent
|
36
|
-
end
|
37
|
-
|
38
|
-
def handle(event)
|
39
|
-
puts "Cogratulations for your new salary => #{event.new_salary}!!!!"
|
40
|
-
end
|
41
|
-
|
42
|
-
end
|
43
|
-
|
44
|
-
|
45
|
-
class Employee
|
46
|
-
|
47
|
-
include SimpleEventSourcing::AggregateRoot::Base
|
48
|
-
|
49
|
-
attr_reader :name, :title, :salary
|
50
|
-
|
51
|
-
def initialize(args = nil )
|
52
|
-
super
|
53
|
-
unless args.nil?
|
54
|
-
apply_record_event NewEmployeeIsHiredEvent.new(@aggregate_id, args[:name], args[:title], args[:salary] )
|
55
|
-
end
|
56
|
-
end
|
57
|
-
|
58
|
-
def salary=(new_salary)
|
59
|
-
apply_record_event SalaryHasChangedEvent.new(@aggregate_id, new_salary)
|
60
|
-
end
|
61
|
-
|
62
|
-
on NewEmployeeIsHiredEvent do |event|
|
63
|
-
@name = event.name
|
64
|
-
@title = event.title
|
65
|
-
@salary = event.salary
|
66
|
-
end
|
67
|
-
|
68
|
-
on SalaryHasChangedEvent do |event|
|
69
|
-
@salary = event.new_salary
|
70
|
-
end
|
71
|
-
|
72
|
-
def save
|
73
|
-
# Persist the entity
|
74
|
-
publish_events { |event| SimpleEventSourcing::Events::EventDispatcher.publish(event) }
|
75
|
-
end
|
76
|
-
|
77
|
-
end
|
data/example/main.rb
DELETED
@@ -1,16 +0,0 @@
|
|
1
|
-
|
2
|
-
unless $LOAD_PATH.include?(File.expand_path(File.dirname(__FILE__)+'/../lib'))
|
3
|
-
$LOAD_PATH.unshift(File.expand_path(File.dirname(__FILE__)+'/../lib'))
|
4
|
-
end
|
5
|
-
|
6
|
-
require 'simple_event_sourcing'
|
7
|
-
require_relative './employee'
|
8
|
-
|
9
|
-
SimpleEventSourcing::Events::EventDispatcher.add_subscriber(CongratulateEmployeeSubscriber.new)
|
10
|
-
|
11
|
-
fred = Employee.new(name: "Fred Flintstone", title: "Crane Operator", salary: 30000.0)
|
12
|
-
fred.salary=35000.0
|
13
|
-
fred.save
|
14
|
-
|
15
|
-
barney = Employee.new(name:"Barney Rubble", title: "Crane Operator", salary: 10000.0)
|
16
|
-
barney.save
|