event-sourcing 0.2.0.pre
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 +7 -0
- data/.gitignore +15 -0
- data/.rspec +3 -0
- data/.ruby-gemset +1 -0
- data/.ruby-version +1 -0
- data/.travis.yml +10 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/Gemfile +6 -0
- data/LICENSE.txt +21 -0
- data/README.md +102 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/event-sourcing.gemspec +28 -0
- data/lib/event_sourcing.rb +8 -0
- data/lib/event_sourcing/command.rb +119 -0
- data/lib/event_sourcing/event.rb +143 -0
- data/lib/event_sourcing/event_dispatcher.rb +71 -0
- data/lib/event_sourcing/rails.rb +2 -0
- data/lib/event_sourcing/rails/command_record.rb +11 -0
- data/lib/event_sourcing/rails/event_record.rb +85 -0
- data/lib/event_sourcing/version.rb +3 -0
- metadata +134 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 1887675acd16105bd77fb6d7cf83281590a965e0352817d55c7c83a0ea56bf50
|
4
|
+
data.tar.gz: 00cce85ada5a577a081c18d60fc59d66bf007878b52d57b286c835f2898d7bec
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 5279909deeb3b0f88b008180eb810a03139552de6374859b617707366a4dac3ba17380aa245a83e8e8b9b3ff6f1a7c6c383a1b66d54e36bf01f5aa2f9d96d5b6
|
7
|
+
data.tar.gz: 75b7e45e8479284f550d42b66bdde7e61d388fb990f55c0963d050133513fbc14d04ab10bd067ce4a11ee37181e03f3ce3f01d130da5999ddb3c221ccd39537b
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.ruby-gemset
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
tc-event-sourcing
|
data/.ruby-version
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
2.5.1
|
data/.travis.yml
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
2
|
+
|
3
|
+
## Our Pledge
|
4
|
+
|
5
|
+
In the interest of fostering an open and welcoming environment, we as
|
6
|
+
contributors and maintainers pledge to making participation in our project and
|
7
|
+
our community a harassment-free experience for everyone, regardless of age, body
|
8
|
+
size, disability, ethnicity, gender identity and expression, level of experience,
|
9
|
+
nationality, personal appearance, race, religion, or sexual identity and
|
10
|
+
orientation.
|
11
|
+
|
12
|
+
## Our Standards
|
13
|
+
|
14
|
+
Examples of behavior that contributes to creating a positive environment
|
15
|
+
include:
|
16
|
+
|
17
|
+
* Using welcoming and inclusive language
|
18
|
+
* Being respectful of differing viewpoints and experiences
|
19
|
+
* Gracefully accepting constructive criticism
|
20
|
+
* Focusing on what is best for the community
|
21
|
+
* Showing empathy towards other community members
|
22
|
+
|
23
|
+
Examples of unacceptable behavior by participants include:
|
24
|
+
|
25
|
+
* The use of sexualized language or imagery and unwelcome sexual attention or
|
26
|
+
advances
|
27
|
+
* Trolling, insulting/derogatory comments, and personal or political attacks
|
28
|
+
* Public or private harassment
|
29
|
+
* Publishing others' private information, such as a physical or electronic
|
30
|
+
address, without explicit permission
|
31
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
32
|
+
professional setting
|
33
|
+
|
34
|
+
## Our Responsibilities
|
35
|
+
|
36
|
+
Project maintainers are responsible for clarifying the standards of acceptable
|
37
|
+
behavior and are expected to take appropriate and fair corrective action in
|
38
|
+
response to any instances of unacceptable behavior.
|
39
|
+
|
40
|
+
Project maintainers have the right and responsibility to remove, edit, or
|
41
|
+
reject comments, commits, code, wiki edits, issues, and other contributions
|
42
|
+
that are not aligned to this Code of Conduct, or to ban temporarily or
|
43
|
+
permanently any contributor for other behaviors that they deem inappropriate,
|
44
|
+
threatening, offensive, or harmful.
|
45
|
+
|
46
|
+
## Scope
|
47
|
+
|
48
|
+
This Code of Conduct applies both within project spaces and in public spaces
|
49
|
+
when an individual is representing the project or its community. Examples of
|
50
|
+
representing a project or community include using an official project e-mail
|
51
|
+
address, posting via an official social media account, or acting as an appointed
|
52
|
+
representative at an online or offline event. Representation of a project may be
|
53
|
+
further defined and clarified by project maintainers.
|
54
|
+
|
55
|
+
## Enforcement
|
56
|
+
|
57
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
58
|
+
reported by contacting the project team at tiagopadrela@gmail.com. All
|
59
|
+
complaints will be reviewed and investigated and will result in a response that
|
60
|
+
is deemed necessary and appropriate to the circumstances. The project team is
|
61
|
+
obligated to maintain confidentiality with regard to the reporter of an incident.
|
62
|
+
Further details of specific enforcement policies may be posted separately.
|
63
|
+
|
64
|
+
Project maintainers who do not follow or enforce the Code of Conduct in good
|
65
|
+
faith may face temporary or permanent repercussions as determined by other
|
66
|
+
members of the project's leadership.
|
67
|
+
|
68
|
+
## Attribution
|
69
|
+
|
70
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
|
71
|
+
available at [http://contributor-covenant.org/version/1/4][version]
|
72
|
+
|
73
|
+
[homepage]: http://contributor-covenant.org
|
74
|
+
[version]: http://contributor-covenant.org/version/1/4/
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2018 The Conversation Media Group
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,102 @@
|
|
1
|
+
# EventSourcing
|
2
|
+
|
3
|
+
Simple Ruby Event Sourcing framework, with Rails support. Inspired by [Kickstarter](https://www.kickstarter.com/)'s
|
4
|
+
["Event Sourcing made Simple"](https://kickstarter.engineering/event-sourcing-made-simple-4a2625113224) blog post.
|
5
|
+
|
6
|
+
`EventSourcing` gives you a toolset to configure and store sequences of events on any Ruby application, making it
|
7
|
+
possible to query an object state at any given point of time.
|
8
|
+
|
9
|
+
As Martin Fowler describes the Event Sourcing concept:
|
10
|
+
|
11
|
+
> Event Sourcing ensures that all changes to application state are stored as a sequence of events. Not just can we query
|
12
|
+
these events, we can also use the event log to reconstruct past states, and as a foundation to automatically adjust the
|
13
|
+
state to cope with retroactive changes.
|
14
|
+
> - Source: https://martinfowler.com/eaaDev/EventSourcing.html
|
15
|
+
|
16
|
+
## Installation
|
17
|
+
|
18
|
+
Add this line to your application's Gemfile:
|
19
|
+
|
20
|
+
```ruby
|
21
|
+
gem 'event-sourcing'
|
22
|
+
```
|
23
|
+
|
24
|
+
And then execute:
|
25
|
+
|
26
|
+
$ bundle
|
27
|
+
|
28
|
+
Or install it yourself as:
|
29
|
+
|
30
|
+
$ gem install event-sourcing
|
31
|
+
|
32
|
+
## Concepts
|
33
|
+
|
34
|
+
This frameworks introduces artifacts and rules to keep track of object state changes.
|
35
|
+
|
36
|
+
The core artifacts of this framework are:
|
37
|
+
|
38
|
+
- **Aggregates**: represent the current state of the application
|
39
|
+
- Usually they are persistence objects, such as "models"
|
40
|
+
- **Events**: provide history of an aggregate, applying changes to an aggregate
|
41
|
+
- "Applying" an event is also known as "Calculator" on other event sourcing literature
|
42
|
+
- **Commands**: validates and persists events
|
43
|
+
- **Reactors**: trigger side effects as events happen
|
44
|
+
- **Event Dispatcher**: manage which reactors will be executed after an event happens
|
45
|
+
|
46
|
+
While state changing actions obey the rationale:
|
47
|
+
|
48
|
+
- Aggregate state changes should be executed through a Command
|
49
|
+
- Events are immutable, as they represent series of changes to an Aggregate
|
50
|
+
- Reactors may be triggered after an Event occurs
|
51
|
+
|
52
|
+
## Usage
|
53
|
+
|
54
|
+
This framework can be used on any Ruby project, as it allows you to customize how you can handle Event persistence and
|
55
|
+
event dispatching control flow.
|
56
|
+
|
57
|
+
You can find an implementation sample at the [PORO app's README file](examples/poro_app/README.md).
|
58
|
+
|
59
|
+
## Usage with Rails
|
60
|
+
|
61
|
+
You can also use this framework with Rails, as it includes an `event_sourcing/rails` module to ease up configuration.
|
62
|
+
|
63
|
+
**Note:** this Rails implementation requires a database that implements serializable attributes by default,
|
64
|
+
such as PostgreSQL JSON/Binary JSON columns.
|
65
|
+
|
66
|
+
To include the Rails module, add this line to your application's Gemfile:
|
67
|
+
|
68
|
+
```ruby
|
69
|
+
gem "event-sourcing", require: "event_sourcing/rails"
|
70
|
+
```
|
71
|
+
|
72
|
+
Further installation steps and a sample implementation can be found at the [Rails' README file](examples/rails_app/README.md).
|
73
|
+
|
74
|
+
## Contributing
|
75
|
+
|
76
|
+
- System requirements
|
77
|
+
- Postgres 8.4+
|
78
|
+
- Ruby 2.3+
|
79
|
+
|
80
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests.
|
81
|
+
|
82
|
+
You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
83
|
+
|
84
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/conversation/event-sourcing.
|
85
|
+
|
86
|
+
This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere
|
87
|
+
|
88
|
+
to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
|
89
|
+
|
90
|
+
## License
|
91
|
+
|
92
|
+
The gem is available as open source under the terms of the [MIT License](LICENSE.txt).
|
93
|
+
|
94
|
+
## Code of Conduct
|
95
|
+
|
96
|
+
Everyone interacting in the The Conversation project’s codebases, issue trackers, chat rooms and mailing lists is
|
97
|
+
expected to follow the [code of conduct](https://github.com/conversation/event-sourcing/blob/master/CODE_OF_CONDUCT.md).
|
98
|
+
|
99
|
+
## Related Projects
|
100
|
+
|
101
|
+
Thank you very much [Kickstarter](https://www.kickstarter.com/) for making the ["Event Sourcing made Simple"](https://kickstarter.engineering/event-sourcing-made-simple-4a2625113224)
|
102
|
+
blog post. This project is heavily inspired on concepts described on it and its [Rails demo app](https://github.com/pcreux/event-sourcing-rails-todo-app-demo).
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "event_sourcing"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
lib = File.expand_path("../lib", __FILE__)
|
2
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
3
|
+
|
4
|
+
require "event_sourcing/version"
|
5
|
+
require "event_sourcing"
|
6
|
+
|
7
|
+
Gem::Specification.new do |spec|
|
8
|
+
spec.name = "event-sourcing"
|
9
|
+
spec.version = EventSourcing::VERSION
|
10
|
+
spec.authors = ["The Conversation Dev Team"]
|
11
|
+
|
12
|
+
spec.summary = "TC Event sourcing library"
|
13
|
+
spec.description = "Event sourcing implementation for TC projects"
|
14
|
+
spec.homepage = "https://github.com/conversation/tc-event-sourcing"
|
15
|
+
spec.license = "MIT"
|
16
|
+
|
17
|
+
# Specify which files should be added to the gem when it is released.
|
18
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
19
|
+
spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
|
20
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features|examples)/}) }
|
21
|
+
end
|
22
|
+
|
23
|
+
spec.add_development_dependency "bundler", "~> 1.16"
|
24
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
25
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
26
|
+
spec.add_development_dependency "rails", ">= 5"
|
27
|
+
spec.add_development_dependency "pg", ">= 1"
|
28
|
+
end
|
@@ -0,0 +1,119 @@
|
|
1
|
+
# The Base command mixin that commands include.
|
2
|
+
#
|
3
|
+
# A Command has the following public api.
|
4
|
+
#
|
5
|
+
# ```
|
6
|
+
# MyCommand.call(user: ..., post: ...) # shorthand to initialize, validate and execute the command
|
7
|
+
# command = MyCommand.new(user: ..., post: ...)
|
8
|
+
# command.valid? # true or false
|
9
|
+
# command.call # validate and execute the command
|
10
|
+
# ```
|
11
|
+
#
|
12
|
+
# Commands including the `EventSourcing::Command` mixin must:
|
13
|
+
# * list the attributes the command takes
|
14
|
+
# * implement `build_event` which returns a non-persisted event or nil for noop.
|
15
|
+
#
|
16
|
+
# Example:
|
17
|
+
#
|
18
|
+
# ```
|
19
|
+
# class MyCommand
|
20
|
+
# include EventSourcing::Command
|
21
|
+
#
|
22
|
+
# attributes :user, :post
|
23
|
+
#
|
24
|
+
# def build_event
|
25
|
+
# Event.new(...)
|
26
|
+
# end
|
27
|
+
# end
|
28
|
+
# ```
|
29
|
+
#
|
30
|
+
# Subclasses must define the following methods:
|
31
|
+
#
|
32
|
+
# - `build_event`
|
33
|
+
# - `validate!`
|
34
|
+
#
|
35
|
+
# You can also implement an optional `after_initialize` method, which can be used to set commands' default values
|
36
|
+
# after its initialization. Example:
|
37
|
+
#
|
38
|
+
# ```
|
39
|
+
# class MyCommand
|
40
|
+
# include EventSourcing::Command
|
41
|
+
#
|
42
|
+
# attributes :active
|
43
|
+
#
|
44
|
+
# def after_initialize
|
45
|
+
# self.active = true
|
46
|
+
# end
|
47
|
+
# end
|
48
|
+
# ```
|
49
|
+
#
|
50
|
+
module EventSourcing
|
51
|
+
module Command
|
52
|
+
def self.included(base)
|
53
|
+
base.extend ClassMethods
|
54
|
+
end
|
55
|
+
|
56
|
+
module ClassMethods
|
57
|
+
# Run validations and persist the event.
|
58
|
+
#
|
59
|
+
# On success: returns the event
|
60
|
+
# On noop: returns nil
|
61
|
+
# On failure: raise an ActiveRecord::RecordInvalid error
|
62
|
+
def call(args = {})
|
63
|
+
new(args).call
|
64
|
+
end
|
65
|
+
|
66
|
+
# Define the attributes.
|
67
|
+
# They are set when initializing the command as a hash and
|
68
|
+
# are all accessible as getter methods.
|
69
|
+
#
|
70
|
+
# ex: `attributes :post, :user, :ability`
|
71
|
+
def attributes(*args)
|
72
|
+
attr_accessor(*args)
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
# @param [Hash<Symbol, Object>] args
|
77
|
+
def initialize(args = {})
|
78
|
+
args.each { |key, value| instance_variable_set("@#{key}", value) }
|
79
|
+
after_initialize
|
80
|
+
end
|
81
|
+
|
82
|
+
def call
|
83
|
+
return :noop if event.nil?
|
84
|
+
raise "The event should not be persisted at this stage!" if event.already_persisted?
|
85
|
+
|
86
|
+
validate!
|
87
|
+
execute!
|
88
|
+
|
89
|
+
event
|
90
|
+
end
|
91
|
+
|
92
|
+
def validate!
|
93
|
+
raise NotImplementedError
|
94
|
+
end
|
95
|
+
|
96
|
+
# A new event or nil if noop
|
97
|
+
def event
|
98
|
+
@event ||= build_event
|
99
|
+
end
|
100
|
+
|
101
|
+
private
|
102
|
+
|
103
|
+
# Save the event. Should not be overwritten by the command as side effects
|
104
|
+
# should be implemented via Reactors triggering other Events.
|
105
|
+
def execute!
|
106
|
+
event.persist_and_dispatch
|
107
|
+
end
|
108
|
+
|
109
|
+
# Returns a new event record or nil if noop
|
110
|
+
def build_event
|
111
|
+
raise NotImplementedError
|
112
|
+
end
|
113
|
+
|
114
|
+
# Hook to set default values
|
115
|
+
def after_initialize
|
116
|
+
# noop
|
117
|
+
end
|
118
|
+
end
|
119
|
+
end
|
@@ -0,0 +1,143 @@
|
|
1
|
+
# This is the base `Event` class that all Events inherit from.
|
2
|
+
# It takes care of serializing `data` and `metadata`
|
3
|
+
# It defines setters and accessors for the defined `data_attributes`
|
4
|
+
# After create, it calls `apply` to apply changes.
|
5
|
+
#
|
6
|
+
# Subclasses must define the following methods:
|
7
|
+
#
|
8
|
+
# - `already_persisted?`
|
9
|
+
# - `apply`
|
10
|
+
# - `build_aggregate`
|
11
|
+
# - `dispatch`
|
12
|
+
# - `persist_aggregate`
|
13
|
+
# - `persist_event`
|
14
|
+
#
|
15
|
+
# Example:
|
16
|
+
#
|
17
|
+
# ```
|
18
|
+
# class MyEvent
|
19
|
+
# include EventSourcing::Event
|
20
|
+
#
|
21
|
+
# def persist_aggregate
|
22
|
+
# # Define your aggregate persistence method here
|
23
|
+
# end
|
24
|
+
#
|
25
|
+
# def persist_event
|
26
|
+
# # Define your event persistence method here
|
27
|
+
# end
|
28
|
+
#
|
29
|
+
# def dispatch
|
30
|
+
# # Define your custom dispatcher here
|
31
|
+
# end
|
32
|
+
#
|
33
|
+
# def build_aggregate
|
34
|
+
# self.aggregate ||= MyAggregate.new
|
35
|
+
# end
|
36
|
+
# end
|
37
|
+
# ```
|
38
|
+
#
|
39
|
+
module EventSourcing
|
40
|
+
module Event
|
41
|
+
def self.included(base)
|
42
|
+
base.extend ClassMethods
|
43
|
+
end
|
44
|
+
|
45
|
+
module ClassMethods
|
46
|
+
# @param [Hash] attrs
|
47
|
+
def assign(attrs = {})
|
48
|
+
metadata = attrs[:metadata] || {}
|
49
|
+
|
50
|
+
self.new.tap do |instance|
|
51
|
+
instance.instance_variable_set(:@data, attrs)
|
52
|
+
instance.instance_variable_set(:@metadata, metadata.merge(klass: klass_name))
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
# Define attributes to be serialized in the `data` column.
|
57
|
+
# It generates setters and getters for those.
|
58
|
+
#
|
59
|
+
# Example:
|
60
|
+
#
|
61
|
+
# class MyEvent < Lib::BaseEvent
|
62
|
+
# data_attributes :title, :description, :drop_id
|
63
|
+
# end
|
64
|
+
#
|
65
|
+
# MyEvent.create!(
|
66
|
+
def data_attributes(*attrs)
|
67
|
+
attrs.map(&:to_s).each do |attr|
|
68
|
+
define_method attr do
|
69
|
+
@data ||= {}
|
70
|
+
@data[attr.to_sym]
|
71
|
+
end
|
72
|
+
|
73
|
+
define_method "#{attr}=" do |arg|
|
74
|
+
@data ||= {}
|
75
|
+
@data[attr.to_sym] = arg
|
76
|
+
end
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
def klass_name
|
81
|
+
self.name
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
85
|
+
attr_reader :data, :metadata
|
86
|
+
attr_accessor :aggregate
|
87
|
+
|
88
|
+
# Apply the event to the aggregate passed in.
|
89
|
+
# Must return the aggregate.
|
90
|
+
def apply(_aggregate)
|
91
|
+
raise NotImplementedError
|
92
|
+
end
|
93
|
+
|
94
|
+
# Redundant name to avoid clashing with ActiveRecord's `#persisted?` method
|
95
|
+
# @return [TrueClass, FalseClass]
|
96
|
+
def already_persisted?
|
97
|
+
false
|
98
|
+
end
|
99
|
+
|
100
|
+
# Replays the event on any given aggregate
|
101
|
+
# @param [Object] aggregate
|
102
|
+
def replay(aggregate)
|
103
|
+
event_class = Object.const_get(metadata[:klass])
|
104
|
+
event_class.assign(data).apply(aggregate)
|
105
|
+
end
|
106
|
+
|
107
|
+
def persist_and_dispatch
|
108
|
+
self.aggregate = build_aggregate
|
109
|
+
|
110
|
+
# Apply
|
111
|
+
self.aggregate = apply(aggregate)
|
112
|
+
|
113
|
+
# Persist aggregate
|
114
|
+
persist_aggregate
|
115
|
+
persist_event
|
116
|
+
|
117
|
+
dispatch
|
118
|
+
end
|
119
|
+
|
120
|
+
private
|
121
|
+
|
122
|
+
def build_aggregate
|
123
|
+
raise NotImplementedError
|
124
|
+
end
|
125
|
+
|
126
|
+
# The base event class should dispatch the dispatcher implementation
|
127
|
+
# Otherwise, event triggers won't be fired
|
128
|
+
# Example: `MyApp::EventDispatcher.dispatch(self)`
|
129
|
+
def dispatch
|
130
|
+
raise NotImplementedError
|
131
|
+
end
|
132
|
+
|
133
|
+
# Persists the aggregate. Must be implemented.
|
134
|
+
def persist_aggregate
|
135
|
+
raise NotImplementedError
|
136
|
+
end
|
137
|
+
|
138
|
+
# Persists the transformation event. Must be implemented.
|
139
|
+
def persist_event
|
140
|
+
raise NotImplementedError
|
141
|
+
end
|
142
|
+
end
|
143
|
+
end
|
@@ -0,0 +1,71 @@
|
|
1
|
+
require "set"
|
2
|
+
|
3
|
+
module EventSourcing
|
4
|
+
# Dispatcher implementation.
|
5
|
+
class EventDispatcher
|
6
|
+
# Register Reactors to Events, which will be synchronously triggered
|
7
|
+
#
|
8
|
+
# Example:
|
9
|
+
#
|
10
|
+
# on BaseEvent, trigger: LogEvent
|
11
|
+
# on PledgeCancelled, PaymentFailed, trigger: [NotifyAdmin, CreateTask]
|
12
|
+
# on [PledgeCancelled, PaymentFailed], trigger: [NotifyAdmin, CreateTask]
|
13
|
+
#
|
14
|
+
def self.on(*events, trigger: [])
|
15
|
+
rules.register(events: events.flatten, sync: Array(trigger))
|
16
|
+
end
|
17
|
+
|
18
|
+
# Dispatches events to matching Reactors once.
|
19
|
+
# Called by all events after they are created.
|
20
|
+
def self.dispatch(event)
|
21
|
+
reactors = rules.for(event)
|
22
|
+
reactors.sync.each { |reactor| reactor.call(event) }
|
23
|
+
end
|
24
|
+
|
25
|
+
def self.rules
|
26
|
+
@rules ||= RuleSet.new
|
27
|
+
end
|
28
|
+
|
29
|
+
class RuleSet
|
30
|
+
def initialize
|
31
|
+
@rules ||= Hash.new { |h, k| h[k] = ReactorSet.new }
|
32
|
+
end
|
33
|
+
|
34
|
+
# Register events with their sync Reactors
|
35
|
+
def register(events:, sync:)
|
36
|
+
events.each do |event|
|
37
|
+
@rules[event].add_sync sync
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
# Return a ReactorSet containing all Reactors matching an Event
|
42
|
+
def for(event)
|
43
|
+
reactors = ReactorSet.new
|
44
|
+
|
45
|
+
@rules.each do |event_class, rule|
|
46
|
+
# Match event by class including ancestors. e.g. All events match a role for BaseEvent.
|
47
|
+
if event.is_a?(event_class)
|
48
|
+
reactors.add_sync rule.sync
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
reactors
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
# Contains sync reactors. Used to:
|
57
|
+
# * store reactors via Rules#register
|
58
|
+
# * return a set of matching reactors with Rules#for
|
59
|
+
class ReactorSet
|
60
|
+
attr_reader :sync
|
61
|
+
|
62
|
+
def initialize
|
63
|
+
@sync = Set.new
|
64
|
+
end
|
65
|
+
|
66
|
+
def add_sync(reactors)
|
67
|
+
@sync += reactors
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
@@ -0,0 +1,85 @@
|
|
1
|
+
module EventSourcing
|
2
|
+
module Rails
|
3
|
+
module EventRecord
|
4
|
+
extend ActiveSupport::Concern
|
5
|
+
|
6
|
+
class_methods do
|
7
|
+
def klass_name
|
8
|
+
self.base_class.name
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
# An event's aggregate reflects the belonging model of a one-to-many association (an aggregate has many events).
|
13
|
+
# This association should map to an `ActiveRecord.belongs_to` association. The methods below maps the belonging
|
14
|
+
# model getters and setters. Example:
|
15
|
+
#
|
16
|
+
# The base event:
|
17
|
+
#
|
18
|
+
# ```
|
19
|
+
# class Users::Events::Base < ApplicationRecord
|
20
|
+
# include EventSourcing::Event
|
21
|
+
# include EventSourcing::Rails::EventRecord
|
22
|
+
#
|
23
|
+
# belongs_to :user
|
24
|
+
# end
|
25
|
+
# ```
|
26
|
+
#
|
27
|
+
# Will have the following method delegations:
|
28
|
+
# - `Users::Events::Base#user=` -> `Users::Events::Base#aggregate=`
|
29
|
+
# - `Users::Events::Base#user` -> `Users::Events::Base#aggregate`
|
30
|
+
# - `Users::Events::Base#user_id=` -> `Users::Events::Base#aggregate_id=`
|
31
|
+
# - `Users::Events::Base#user_id` -> `Users::Events::Base#aggregate_id`
|
32
|
+
#
|
33
|
+
def aggregate_name
|
34
|
+
inferred_aggregate = self.class.reflect_on_all_associations(:belongs_to).first
|
35
|
+
raise "Events must belong to an aggregate" if inferred_aggregate.nil?
|
36
|
+
inferred_aggregate.name
|
37
|
+
end
|
38
|
+
|
39
|
+
def aggregate=(object)
|
40
|
+
public_send("#{aggregate_name}=", object)
|
41
|
+
end
|
42
|
+
|
43
|
+
def aggregate
|
44
|
+
public_send(aggregate_name)
|
45
|
+
end
|
46
|
+
|
47
|
+
def aggregate_id=(id)
|
48
|
+
public_send("#{aggregate_name}_id=", id)
|
49
|
+
end
|
50
|
+
|
51
|
+
def aggregate_id
|
52
|
+
public_send("#{aggregate_name}_id")
|
53
|
+
end
|
54
|
+
|
55
|
+
# Replaces `EventSourcing::Event#data` and `EventSourcing::Event#metadata` so it fetches
|
56
|
+
# ActiveRecord's database column. This always returns a hash with `Symbol`s as keys
|
57
|
+
def data
|
58
|
+
self[:data].transform_keys(&:to_sym)
|
59
|
+
end
|
60
|
+
|
61
|
+
def metadata
|
62
|
+
self[:metadata].transform_keys(&:to_sym)
|
63
|
+
end
|
64
|
+
|
65
|
+
# Delegates to `ActiveRecord#persisted?`
|
66
|
+
def already_persisted?
|
67
|
+
persisted?
|
68
|
+
end
|
69
|
+
|
70
|
+
def persist_aggregate
|
71
|
+
aggregate.save!
|
72
|
+
end
|
73
|
+
|
74
|
+
def persist_event
|
75
|
+
raise "Event must have an aggregate!" if aggregate.nil?
|
76
|
+
|
77
|
+
self[:data] = @data
|
78
|
+
self[:metadata] = @metadata
|
79
|
+
self.aggregate_id = aggregate.id
|
80
|
+
|
81
|
+
save!
|
82
|
+
end
|
83
|
+
end
|
84
|
+
end
|
85
|
+
end
|
metadata
ADDED
@@ -0,0 +1,134 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: event-sourcing
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.2.0.pre
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- The Conversation Dev Team
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2018-10-27 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.16'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.16'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '3.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '3.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rails
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '5'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '5'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: pg
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '1'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '1'
|
83
|
+
description: Event sourcing implementation for TC projects
|
84
|
+
email:
|
85
|
+
executables: []
|
86
|
+
extensions: []
|
87
|
+
extra_rdoc_files: []
|
88
|
+
files:
|
89
|
+
- ".gitignore"
|
90
|
+
- ".rspec"
|
91
|
+
- ".ruby-gemset"
|
92
|
+
- ".ruby-version"
|
93
|
+
- ".travis.yml"
|
94
|
+
- CODE_OF_CONDUCT.md
|
95
|
+
- Gemfile
|
96
|
+
- LICENSE.txt
|
97
|
+
- README.md
|
98
|
+
- Rakefile
|
99
|
+
- bin/console
|
100
|
+
- bin/setup
|
101
|
+
- event-sourcing.gemspec
|
102
|
+
- lib/event_sourcing.rb
|
103
|
+
- lib/event_sourcing/command.rb
|
104
|
+
- lib/event_sourcing/event.rb
|
105
|
+
- lib/event_sourcing/event_dispatcher.rb
|
106
|
+
- lib/event_sourcing/rails.rb
|
107
|
+
- lib/event_sourcing/rails/command_record.rb
|
108
|
+
- lib/event_sourcing/rails/event_record.rb
|
109
|
+
- lib/event_sourcing/version.rb
|
110
|
+
homepage: https://github.com/conversation/tc-event-sourcing
|
111
|
+
licenses:
|
112
|
+
- MIT
|
113
|
+
metadata: {}
|
114
|
+
post_install_message:
|
115
|
+
rdoc_options: []
|
116
|
+
require_paths:
|
117
|
+
- lib
|
118
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
119
|
+
requirements:
|
120
|
+
- - ">="
|
121
|
+
- !ruby/object:Gem::Version
|
122
|
+
version: '0'
|
123
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
124
|
+
requirements:
|
125
|
+
- - ">"
|
126
|
+
- !ruby/object:Gem::Version
|
127
|
+
version: 1.3.1
|
128
|
+
requirements: []
|
129
|
+
rubyforge_project:
|
130
|
+
rubygems_version: 2.7.7
|
131
|
+
signing_key:
|
132
|
+
specification_version: 4
|
133
|
+
summary: TC Event sourcing library
|
134
|
+
test_files: []
|