simple_event_sourcing 0.4.0 → 0.5.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 +6 -0
- data/docker-compose.yml +14 -0
- data/lib/simple_event_sourcing/aggregate_root/base.rb +8 -10
- data/lib/simple_event_sourcing/aggregate_root/history.rb +1 -1
- data/lib/simple_event_sourcing/events/event.rb +12 -0
- data/lib/simple_event_sourcing/events/event_dispatcher.rb +24 -0
- data/lib/simple_event_sourcing/events/event_store.rb +15 -0
- data/lib/simple_event_sourcing/events/event_subscriber.rb +27 -0
- data/lib/simple_event_sourcing/events/stored_event.rb +34 -0
- data/lib/simple_event_sourcing/version.rb +1 -1
- data/lib/simple_event_sourcing.rb +6 -1
- data/simple_event_sourcing.gemspec +1 -0
- metadata +7 -3
- data/lib/simple_event_sourcing/events/events.rb +0 -56
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 5f3985c5f3cfad42937fe5eda1246cbc763812dedc3f057f9a86ae07aa534baf
|
4
|
+
data.tar.gz: aa420aff7d09f1b43bc13761f4e58f224191f01926aabf38328642bfa711df74
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b69656839246166b15c9ddc13a59b34c1877b6dd375ce68b05236174c1de441174451b8b00d19b8a9bb595598bf2a5496310636ec4da0161061efb0366371189
|
7
|
+
data.tar.gz: a1c2363347611cd259cad2e117c487c7513e9ad3ea047c1fc2d1c8c44d82eb5fbd399aaae9ef04586815e6319ee4faa37b7f84b9d1f1e6cb7a599d2fdee8a113
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -1,5 +1,11 @@
|
|
1
1
|
# EventSourcing
|
2
2
|
|
3
|
+
|
4
|
+
DISCLAIMER
|
5
|
+
|
6
|
+
This gem is under development. DO NOT USE IN PRODUCTION ENVIRONMENT!!!!
|
7
|
+
|
8
|
+
|
3
9
|
The fundamental idea of Event Sourcing is that of ensuring every change to the state of an application is captured in an event object, and that these event objects are themselves stored in the sequence they were applied for the same lifetime as the application state itself.
|
4
10
|
|
5
11
|
Martin Fowler , https://martinfowler.com/eaaDev/EventSourcing.html
|
data/docker-compose.yml
CHANGED
@@ -7,3 +7,17 @@ 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
|
@@ -22,11 +22,6 @@ module SimpleEventSourcing
|
|
22
22
|
clear_events
|
23
23
|
end
|
24
24
|
|
25
|
-
def apply_record_event(event)
|
26
|
-
handle_message(event)
|
27
|
-
record_event event
|
28
|
-
end
|
29
|
-
|
30
25
|
def handle_message(message)
|
31
26
|
handler = self.class.message_mapping[message.class]
|
32
27
|
self.instance_exec(message, &handler) if handler
|
@@ -37,11 +32,13 @@ module SimpleEventSourcing
|
|
37
32
|
end
|
38
33
|
|
39
34
|
module ClassMethods
|
35
|
+
|
40
36
|
def create_from_agrregate_id(id)
|
41
37
|
aggregate = new
|
42
38
|
aggregate.aggregate_id = id
|
43
39
|
aggregate
|
44
40
|
end
|
41
|
+
|
45
42
|
def on(*message_classes, &block)
|
46
43
|
message_classes.each { |message_class| message_mapping[message_class] = block }
|
47
44
|
end
|
@@ -57,6 +54,11 @@ module SimpleEventSourcing
|
|
57
54
|
|
58
55
|
private
|
59
56
|
|
57
|
+
def apply_record_event(event)
|
58
|
+
handle_message(event)
|
59
|
+
record_event event
|
60
|
+
end
|
61
|
+
|
60
62
|
def record_event(event)
|
61
63
|
@events << event
|
62
64
|
end
|
@@ -65,11 +67,7 @@ module SimpleEventSourcing
|
|
65
67
|
@events = []
|
66
68
|
end
|
67
69
|
|
68
|
-
|
69
|
-
handle_message(event)
|
70
|
-
#method = 'apply_' + event.class.name.snakecase
|
71
|
-
#send(method, event)
|
72
|
-
end
|
70
|
+
|
73
71
|
end
|
74
72
|
end
|
75
73
|
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
module SimpleEventSourcing
|
2
|
+
module Events
|
3
|
+
module EventDispatcher
|
4
|
+
@@subscribers = []
|
5
|
+
|
6
|
+
def self.add_subscriber(subscriber)
|
7
|
+
@@subscribers << subscriber
|
8
|
+
end
|
9
|
+
|
10
|
+
def self.delete_subscriber(subscriber)
|
11
|
+
@@subscribers.delete(subscriber)
|
12
|
+
end
|
13
|
+
|
14
|
+
def self.get_subscribers
|
15
|
+
@@subscribers
|
16
|
+
end
|
17
|
+
|
18
|
+
def self.publish(event)
|
19
|
+
@@subscribers.each { |subscriber| subscriber.handle(event) if subscriber.is_subscribet_to? event }
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
module SimpleEventSourcing
|
2
|
+
module Events
|
3
|
+
class EventStore
|
4
|
+
|
5
|
+
def commit(event)
|
6
|
+
raise StandardError "This methid must be implemented"
|
7
|
+
end
|
8
|
+
|
9
|
+
def get_history(aggregate_id)
|
10
|
+
raise StandardError "This methid must be implemented"
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
module SimpleEventSourcing
|
2
|
+
module Events
|
3
|
+
class EventSubscriber
|
4
|
+
|
5
|
+
def is_subscribet_to?(event)
|
6
|
+
raise StandardError "Method not implemented"
|
7
|
+
end
|
8
|
+
|
9
|
+
def check(event)
|
10
|
+
raise EventIsNotAllowedToBeHandled unless is_subscribet_to? event
|
11
|
+
end
|
12
|
+
|
13
|
+
def handle_event(event)
|
14
|
+
raise StandardError "Method not implemented"
|
15
|
+
end
|
16
|
+
|
17
|
+
def handle(event)
|
18
|
+
check event
|
19
|
+
handle_event event
|
20
|
+
end
|
21
|
+
|
22
|
+
end
|
23
|
+
|
24
|
+
class EventIsNotAllowedToBeHandled < StandardError; end
|
25
|
+
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
module SimpleEventSourcing
|
2
|
+
module Events
|
3
|
+
|
4
|
+
class StoredEvent
|
5
|
+
attr_reader :aggregate_id, :occurred_on, :event_type, :event_data
|
6
|
+
|
7
|
+
def initialize(args)
|
8
|
+
@aggregate_id = args[:aggregate_id]
|
9
|
+
@occurred_on = args[:occurred_on]
|
10
|
+
@event_type = args[:event_type]
|
11
|
+
@event_data = args[:event_data]
|
12
|
+
end
|
13
|
+
|
14
|
+
def to_json(*a)
|
15
|
+
{"aggregate_id" => @aggregate_id.to_s, "occurred_on" => @occurred_on.to_i, "event_type" => @event_type.to_s, "event_data" => @event_data }.to_json(*a)
|
16
|
+
end
|
17
|
+
|
18
|
+
def self.create_from_json(json)
|
19
|
+
|
20
|
+
stored_event_hash = JSON.parse(json)
|
21
|
+
|
22
|
+
self.new(
|
23
|
+
aggregate_id: stored_event_hash['aggregate_id'],
|
24
|
+
occurred_on: stored_event_hash['occurred_on'],
|
25
|
+
event_type: stored_event_hash['event_type'],
|
26
|
+
event_data: stored_event_hash['event_data']
|
27
|
+
)
|
28
|
+
|
29
|
+
end
|
30
|
+
|
31
|
+
end
|
32
|
+
|
33
|
+
end
|
34
|
+
end
|
@@ -3,4 +3,9 @@ require 'simple_event_sourcing/version'
|
|
3
3
|
require 'simple_event_sourcing/aggregate_root/id'
|
4
4
|
require 'simple_event_sourcing/aggregate_root/base'
|
5
5
|
require 'simple_event_sourcing/aggregate_root/history'
|
6
|
-
|
6
|
+
|
7
|
+
require 'simple_event_sourcing/events/event'
|
8
|
+
require 'simple_event_sourcing/events/event_dispatcher'
|
9
|
+
require 'simple_event_sourcing/events/event_subscriber'
|
10
|
+
require 'simple_event_sourcing/events/stored_event'
|
11
|
+
require 'simple_event_sourcing/events/event_store'
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
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.5.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Manuel López Torrent
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-04-
|
11
|
+
date: 2018-04-25 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -108,7 +108,11 @@ files:
|
|
108
108
|
- lib/simple_event_sourcing/aggregate_root/base.rb
|
109
109
|
- lib/simple_event_sourcing/aggregate_root/history.rb
|
110
110
|
- lib/simple_event_sourcing/aggregate_root/id.rb
|
111
|
-
- lib/simple_event_sourcing/events/
|
111
|
+
- lib/simple_event_sourcing/events/event.rb
|
112
|
+
- lib/simple_event_sourcing/events/event_dispatcher.rb
|
113
|
+
- lib/simple_event_sourcing/events/event_store.rb
|
114
|
+
- lib/simple_event_sourcing/events/event_subscriber.rb
|
115
|
+
- lib/simple_event_sourcing/events/stored_event.rb
|
112
116
|
- lib/simple_event_sourcing/version.rb
|
113
117
|
- run.sh
|
114
118
|
- simple_event_sourcing.gemspec
|
@@ -1,56 +0,0 @@
|
|
1
|
-
module SimpleEventSourcing
|
2
|
-
module Events
|
3
|
-
|
4
|
-
class Event
|
5
|
-
attr_reader :occured_on
|
6
|
-
|
7
|
-
def initialize
|
8
|
-
@occured_on ||= Time.new
|
9
|
-
end
|
10
|
-
end
|
11
|
-
|
12
|
-
class EventSubscriber
|
13
|
-
|
14
|
-
def is_subscribet_to?(event)
|
15
|
-
raise StandardError "Method not implemented"
|
16
|
-
end
|
17
|
-
|
18
|
-
def check(event)
|
19
|
-
raise EventIsNotAllowedToBeHandled unless is_subscribet_to? event
|
20
|
-
end
|
21
|
-
|
22
|
-
def handle_event(event)
|
23
|
-
raise StandardError "Method not implemented"
|
24
|
-
end
|
25
|
-
|
26
|
-
def handle(event)
|
27
|
-
check event
|
28
|
-
handle_event event
|
29
|
-
end
|
30
|
-
|
31
|
-
end
|
32
|
-
|
33
|
-
class EventIsNotAllowedToBeHandled < StandardError; end
|
34
|
-
|
35
|
-
module EventDispatcher
|
36
|
-
@@subscribers = []
|
37
|
-
|
38
|
-
def self.add_subscriber(subscriber)
|
39
|
-
@@subscribers << subscriber
|
40
|
-
end
|
41
|
-
|
42
|
-
def self.delete_subscriber(subscriber)
|
43
|
-
@@subscribers.delete(subscriber)
|
44
|
-
end
|
45
|
-
|
46
|
-
def self.get_subscribers
|
47
|
-
@@subscribers
|
48
|
-
end
|
49
|
-
|
50
|
-
def self.publish(event)
|
51
|
-
@@subscribers.each { |subscriber| subscriber.handle(event) if subscriber.is_subscribet_to? event }
|
52
|
-
end
|
53
|
-
end
|
54
|
-
|
55
|
-
end
|
56
|
-
end
|