simple_event_sourcing 0.2.0 → 0.3.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 8c8f96d29fae329c09e8001a2ce8ab34e725ba76095d61e7927acf42321be8c2
4
- data.tar.gz: 69fc9bde337b95d771f4ca23a15f7eedc0b40d0e39df082b39272e96c4ecf88a
3
+ metadata.gz: 666d470eb4b8fadd4728c3bd093e216b6454272d23a3f2891fef63657855a270
4
+ data.tar.gz: 828c08776a5200bd236783f0d911b25ac4283ae87d628f102e61554d0c61089e
5
5
  SHA512:
6
- metadata.gz: 48671ba2bc1b7b37e7130622f87f7d519bafa91f9e38e1f26f3b346d14f60354b92e9cabfe1c8005f309cde15204ded274095867470f3800b7298e5ee7ff7104
7
- data.tar.gz: ca37c5f86523fb2d3e28a6eedbe9f3f9ea9999940cba3d05daf616c18e7178e985e97df83f88a3860a6d019a42b778753a23252e6ac91bbc1ae3f0e0a2b37358
6
+ metadata.gz: d0b8fae3aac98197e3cbcf73f44ed374208239166cb631c952844196abdea07b81fcddbdb2cd03d604481f6d0859f4390384f359e0cc6cdf84df6c4c33df114f
7
+ data.tar.gz: 888f20ab00ebe27210b225760885cc35e8f654df4050068ad5979e33f4490e3451ed59325be12b6c64b96a774f840ac0634aa4d36dc9e9f07ce938a425bc1717
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- simple_event_sourcing (0.2.0)
4
+ simple_event_sourcing (0.3.0)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
data/README.md CHANGED
@@ -51,13 +51,13 @@ class Employee
51
51
  apply_record_event SalaryHasChangedEvent.new(@aggregate_id, new_salary)
52
52
  end
53
53
 
54
- def apply_new_employee_is_hired_event(event)
54
+ on NewEmployeeIsHiredEvent do |event|
55
55
  @name = event.name
56
56
  @title = event.title
57
57
  @salary = event.salary
58
58
  end
59
59
 
60
- def apply_salary_has_changed_event(event)
60
+ on SalaryHasChangedEvent do |event|
61
61
  @salary = event.new_salary
62
62
  end
63
63
 
@@ -69,19 +69,13 @@ class Employee
69
69
  end
70
70
  ```
71
71
 
72
- Firts you mus add behaviour including the AggregateRoot module
72
+ First, you must add behaviour including the AggregateRoot module
73
73
 
74
74
  ```ruby
75
75
  include SimpleEventSourcing::AggregateRoot
76
76
  ```
77
77
 
78
- After that all domain event must be applied and recorded
79
-
80
- ```ruby
81
- apply_record_event SalaryHasChangedEvent.new(@aggregate_id, new_salary)
82
- ```
83
-
84
- You must create your own events and a event stream
78
+ You must create your own domain events and a event stream
85
79
 
86
80
  ```ruby
87
81
  class EmployeeStreamEvents < SimpleEventSourcing::StreamEvents
@@ -101,6 +95,22 @@ class SalaryHasChangedEvent < SimpleEventSourcing::Event
101
95
  end
102
96
  ```
103
97
 
98
+ After that all domain event must be applied and recorded
99
+
100
+ ```ruby
101
+ apply_record_event SalaryHasChangedEvent.new(@aggregate_id, new_salary)
102
+ ```
103
+
104
+ SimpleEventSourcing provides a DSL to handle the applied events. You must provide a handler for each event
105
+
106
+ ```ruby
107
+ on SalaryHasChangedEvent do |event|
108
+ @salary = event.new_salary
109
+ end
110
+ ```
111
+
112
+
113
+
104
114
  Once you persist the entity you must publish all recorded events.
105
115
 
106
116
  ```ruby
data/docker-compose.yml CHANGED
@@ -5,3 +5,5 @@ services:
5
5
  volumes:
6
6
  - ./:/myapp
7
7
  command: tail -f /dev/null
8
+ environment:
9
+ - LANG=C.UTF-8
data/example/employee.rb CHANGED
@@ -29,6 +29,18 @@ class SalaryHasChangedEvent < SimpleEventSourcing::Event
29
29
  end
30
30
  end
31
31
 
32
+ class CongratulateEmployeeSubscriber < SimpleEventSourcing::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
+
32
44
 
33
45
  class Employee
34
46
 
@@ -47,13 +59,13 @@ class Employee
47
59
  apply_record_event SalaryHasChangedEvent.new(@aggregate_id, new_salary)
48
60
  end
49
61
 
50
- def apply_new_employee_is_hired_event(event)
62
+ on NewEmployeeIsHiredEvent do |event|
51
63
  @name = event.name
52
64
  @title = event.title
53
65
  @salary = event.salary
54
66
  end
55
67
 
56
- def apply_salary_has_changed_event(event)
68
+ on SalaryHasChangedEvent do |event|
57
69
  @salary = event.new_salary
58
70
  end
59
71
 
data/example/main.rb CHANGED
@@ -1,6 +1,4 @@
1
- require_relative '../lib/simple_event_sourcing'
2
- require_relative 'employee_events'
3
- require_relative 'employee_subscribers'
1
+ require 'simple_event_sourcing'
4
2
  require_relative 'employee'
5
3
 
6
4
  SimpleEventSourcing::EventPublisher.add_subscriber(CongratulateEmployeeSubscriber.new)
@@ -1,6 +1,5 @@
1
1
  require 'simple_event_sourcing/version'
2
2
 
3
-
4
3
  require_relative 'simple_event_sourcing/aggregate_root/aggregate_root'
5
4
  require_relative 'simple_event_sourcing/events/events'
6
5
 
@@ -1,6 +1,8 @@
1
1
  require 'securerandom'
2
2
  require 'facets'
3
3
 
4
+ require_relative 'self_applier'
5
+
4
6
  module SimpleEventSourcing
5
7
  module AggregateRoot
6
8
 
@@ -24,7 +26,7 @@ module SimpleEventSourcing
24
26
  end
25
27
 
26
28
  def apply_record_event(event)
27
- apply_event event
29
+ handle_message(event)
28
30
  record_event event
29
31
  end
30
32
 
@@ -51,8 +53,9 @@ module SimpleEventSourcing
51
53
  end
52
54
 
53
55
  def apply_event(event)
54
- method = 'apply_' + event.class.name.snakecase
55
- send(method, event)
56
+ handle_message(event)
57
+ #method = 'apply_' + event.class.name.snakecase
58
+ #send(method, event)
56
59
  end
57
60
 
58
61
  end
@@ -0,0 +1,41 @@
1
+ module SimpleEventSourcing
2
+ module AggregateRoot
3
+ ##
4
+ # Creates ability to use DSL like:
5
+ # class MyEventHandler < Sequent::Core::BaseEventHandler
6
+ #
7
+ # on MyEvent do |event|
8
+ # do_some_logic
9
+ # end
10
+ # end
11
+ #
12
+ # You typically do not need to include this module in your classes. If you extend from
13
+ # Sequent::Core::AggregateRoot, Sequent::Core::Projector, Sequent::Core::Workflow or Sequent::Core::BaseCommandHandler
14
+ # you will get this functionality for free.
15
+ #
16
+
17
+ module ClassMethods
18
+ def on(*message_classes, &block)
19
+ message_classes.each { |message_class| message_mapping[message_class] = block }
20
+ end
21
+
22
+ def message_mapping
23
+ @message_mapping ||= {}
24
+ end
25
+
26
+ def handles_message?(message)
27
+ message_mapping.keys.include? message.class
28
+ end
29
+ end
30
+
31
+ def self.included(host_class)
32
+ host_class.extend(ClassMethods)
33
+ end
34
+
35
+ def handle_message(message)
36
+ handler = self.class.message_mapping[message.class]
37
+ self.instance_exec(message, &handler) if handler
38
+ end
39
+ end
40
+
41
+ end
@@ -26,6 +26,18 @@ module SimpleEventSourcing
26
26
  end
27
27
  end
28
28
 
29
+ class EventSubscriber
30
+
31
+ def is_subscribet_to?(event)
32
+ raise StandardError "Method not implemented"
33
+ end
34
+
35
+ def handle(event)
36
+ raise StandardError "Method not implemented"
37
+ end
38
+ end
39
+
40
+
29
41
  module EventPublisher
30
42
  @@subscribers = []
31
43
 
@@ -1,3 +1,3 @@
1
1
  module SimpleEventSourcing
2
- VERSION = "0.2.0"
2
+ VERSION = "0.3.0"
3
3
  end
@@ -6,12 +6,12 @@ require "simple_event_sourcing/version"
6
6
  Gem::Specification.new do |spec|
7
7
  spec.name = "simple_event_sourcing"
8
8
  spec.version = SimpleEventSourcing::VERSION
9
- spec.authors = ["Manel"]
9
+ spec.authors = ["Manuel López Torrent"]
10
10
  spec.email = ["malotor@gmail.com"]
11
11
 
12
- spec.summary = %q{Write a short summary, because RubyGems requires one.}
13
- spec.description = %q{Write a longer description or delete this line.}
14
- spec.homepage = "https://github.com/malotor"
12
+ spec.summary = %q{This gem provides a simple way for add events sourcing related behaviour to your models class}
13
+ spec.description = %q{This gem provides a simple way for add events sourcing related behaviour to your models class}
14
+ spec.homepage = "https://github.com/malotor/ruby_event_sourcing"
15
15
  spec.license = "MIT"
16
16
 
17
17
  # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
metadata CHANGED
@@ -1,10 +1,10 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: simple_event_sourcing
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
- - Manel
7
+ - Manuel López Torrent
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
@@ -80,7 +80,8 @@ dependencies:
80
80
  - - ">="
81
81
  - !ruby/object:Gem::Version
82
82
  version: '0'
83
- description: Write a longer description or delete this line.
83
+ description: This gem provides a simple way for add events sourcing related behaviour
84
+ to your models class
84
85
  email:
85
86
  - malotor@gmail.com
86
87
  executables: []
@@ -101,17 +102,16 @@ files:
101
102
  - bin/console
102
103
  - bin/setup
103
104
  - docker-compose.yml
104
- - example/Gemfile
105
105
  - example/employee.rb
106
- - example/employee_subscribers.rb
107
106
  - example/main.rb
108
107
  - lib/simple_event_sourcing.rb
109
108
  - lib/simple_event_sourcing/aggregate_root/aggregate_root.rb
109
+ - lib/simple_event_sourcing/aggregate_root/self_applier.rb
110
110
  - lib/simple_event_sourcing/events/events.rb
111
111
  - lib/simple_event_sourcing/version.rb
112
112
  - run.sh
113
113
  - simple_event_sourcing.gemspec
114
- homepage: https://github.com/malotor
114
+ homepage: https://github.com/malotor/ruby_event_sourcing
115
115
  licenses:
116
116
  - MIT
117
117
  metadata: {}
@@ -134,5 +134,6 @@ rubyforge_project:
134
134
  rubygems_version: 2.7.2
135
135
  signing_key:
136
136
  specification_version: 4
137
- summary: Write a short summary, because RubyGems requires one.
137
+ summary: This gem provides a simple way for add events sourcing related behaviour
138
+ to your models class
138
139
  test_files: []
data/example/Gemfile DELETED
@@ -1 +0,0 @@
1
- gem "simple_event_sourcing", :git => "https://github.com/malotor/ruby_event_sourcing.git"
@@ -1,22 +0,0 @@
1
- class EmployeeSubscriber
2
-
3
- def (event)
4
- raise StandardError "Method not implemented"
5
- end
6
-
7
- def handle(event)
8
- raise StandardError "Method not implemented"
9
- end
10
- end
11
-
12
- class CongratulateEmployeeSubscriber < EmployeeSubscriber
13
-
14
- def is_subscribet_to?(event)
15
- event.class == SalaryHasChangedEvent
16
- end
17
-
18
- def handle(event)
19
- puts "Cogratulations for your new salary => #{event.new_salary}!!!!"
20
- end
21
-
22
- end