eventusha 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +12 -0
- data/.rspec +2 -0
- data/.travis.yml +5 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +233 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/eventusha.gemspec +41 -0
- data/lib/eventusha.rb +13 -0
- data/lib/eventusha/aggregate.rb +47 -0
- data/lib/eventusha/command.rb +31 -0
- data/lib/eventusha/command_handler.rb +20 -0
- data/lib/eventusha/event.rb +27 -0
- data/lib/eventusha/event_handler.rb +11 -0
- data/lib/eventusha/version.rb +3 -0
- data/lib/generators/eventusha/install_generator.rb +18 -0
- data/lib/generators/eventusha/migration.rb +15 -0
- data/lib/generators/eventusha/templates/install.rb +11 -0
- metadata +178 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: b1eabbf19bc5adbd1d419dd6bcd9599f7f38840f
|
4
|
+
data.tar.gz: 34523baac389f53c2d8d0fcd47e58ad5e7f03edf
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: d1f8fc690196c4437ea8a7a1962b3cdbc7334c719a58cb2d3287b551ff4e8fd31026313324f05037bd8cfca8304b2f87894031285b823dc4104b22e63b7e657e
|
7
|
+
data.tar.gz: 510d122de298d0558eb5cf801edb28923c486a6e3f1e41799b12c41d29a19ddc196201ae17103b21f7caa2f70c1eef0df29835b0fddb9ed9c106f15d56a5b6ae
|
data/.gitignore
ADDED
data/.rspec
ADDED
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 nikajukic@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 Nika Jukić
|
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,233 @@
|
|
1
|
+
# Eventusha
|
2
|
+
|
3
|
+
Eventusha is an Event Sourcing framework for Ruby.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'eventusha'
|
11
|
+
```
|
12
|
+
|
13
|
+
Execute:
|
14
|
+
|
15
|
+
$ bundle install
|
16
|
+
|
17
|
+
And then:
|
18
|
+
|
19
|
+
$ rails generate eventusha:install
|
20
|
+
$ rake db:migrate
|
21
|
+
|
22
|
+
|
23
|
+
## Usage
|
24
|
+
|
25
|
+
Event Sourcing and CQRS consist of a few different elements. Recommended folder structure inside app folder:
|
26
|
+
```
|
27
|
+
app
|
28
|
+
──cqrs
|
29
|
+
──aggregates
|
30
|
+
──command_handlers
|
31
|
+
──commands
|
32
|
+
──event_handlers
|
33
|
+
──events
|
34
|
+
```
|
35
|
+
|
36
|
+
### Controller
|
37
|
+
|
38
|
+
Inside controllers you need to execute commands.
|
39
|
+
|
40
|
+
```ruby
|
41
|
+
class BankAccountsController < ApplicationController
|
42
|
+
def new
|
43
|
+
@command = Commands::CreateBankAccount.new
|
44
|
+
end
|
45
|
+
|
46
|
+
def create
|
47
|
+
@command = Commands::CreateBankAccount.new(bank_account_params)
|
48
|
+
|
49
|
+
if @command.execute
|
50
|
+
redirect_to [:bank_accounts]
|
51
|
+
else
|
52
|
+
render :new
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
private
|
57
|
+
|
58
|
+
def bank_account_params
|
59
|
+
params.require(:bank_account)
|
60
|
+
.permit(:first_name, :last_name)
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
```
|
65
|
+
|
66
|
+
### Commands
|
67
|
+
|
68
|
+
Commands are instructions that are usually initiated by users, e.g. `CreateBankAccount`. Commands are defined inside `app/cqrs/commands` folder. User actions are validated inside commands using the `ActiveModel::Model`.
|
69
|
+
|
70
|
+
#### attributes
|
71
|
+
Use `attributes` method to define command attributes. These attributes are going to be saved as serialized event data.
|
72
|
+
|
73
|
+
```ruby
|
74
|
+
module Commands
|
75
|
+
class CreateBankAccount < Eventusha::Command
|
76
|
+
attributes :first_name, :last_name
|
77
|
+
|
78
|
+
validates :first_name, presence: true
|
79
|
+
validates :last_name, presence: true
|
80
|
+
end
|
81
|
+
end
|
82
|
+
```
|
83
|
+
|
84
|
+
### Command Handlers
|
85
|
+
|
86
|
+
Command handlers are used for handling commands. They load defined aggregate and execute corresponding action on it. For each command you must define a command handler with the same name inside `app/cqrs/command_handlers` folder
|
87
|
+
|
88
|
+
#### aggregate
|
89
|
+
Use `aggregate` method to define which aggregate is going to be used. Selected aggregate must be defined in `app/cqrs/aggregates` folder. These attributes are going to be saved as serialized event data.
|
90
|
+
|
91
|
+
`aggregate :bank_account` will use `Aggregates::BankAccount` aggregate.
|
92
|
+
|
93
|
+
```ruby
|
94
|
+
module CommandHandlers
|
95
|
+
class CreateBankAccount < Eventusha::CommandHandler
|
96
|
+
aggregate :bank_account
|
97
|
+
|
98
|
+
def execute
|
99
|
+
bank_account = aggregate.new
|
100
|
+
|
101
|
+
bank_account.create_bank_account(command.attributes)
|
102
|
+
end
|
103
|
+
end
|
104
|
+
end
|
105
|
+
```
|
106
|
+
|
107
|
+
You can create a new aggregate with `aggregate.new` or build by replaying all events with that aggregate_id using `aggregate.find(command.aggregate_id)`
|
108
|
+
|
109
|
+
```ruby
|
110
|
+
module CommandHandlers
|
111
|
+
class PerformDeposit < Eventusha::CommandHandler
|
112
|
+
aggregate :bank_account
|
113
|
+
|
114
|
+
def execute
|
115
|
+
bank_account = aggregate.find(command.aggregate_id)
|
116
|
+
|
117
|
+
bank_account.perform_deposit(command.attributes)
|
118
|
+
end
|
119
|
+
end
|
120
|
+
end
|
121
|
+
```
|
122
|
+
|
123
|
+
### Aggregates
|
124
|
+
Aggregate is a main domain model. It is build by replaying events and it represents current state of a domain object.
|
125
|
+
|
126
|
+
When an action initiated from command handler is executed on an aggregate, one or more events are created and applied on current aggregate.
|
127
|
+
|
128
|
+
Define what happens on an aggregate when you replay an event.
|
129
|
+
|
130
|
+
```ruby
|
131
|
+
on EventClass do |event|
|
132
|
+
#define what happens on aggregate when you replay this event
|
133
|
+
end
|
134
|
+
```
|
135
|
+
|
136
|
+
```ruby
|
137
|
+
module Aggregates
|
138
|
+
class BankAccount < Eventusha::Aggregate
|
139
|
+
def initialize
|
140
|
+
@aggregate_id = SecureRandom.uuid
|
141
|
+
@amount = 0
|
142
|
+
end
|
143
|
+
|
144
|
+
def create_bank_account(attributes)
|
145
|
+
apply Events::BankAccountCreated.prepare(aggregate_id, attributes)
|
146
|
+
end
|
147
|
+
|
148
|
+
def perform_deposit(attributes)
|
149
|
+
apply Events::DepositPerformed.prepare(aggregate_id, attributes)
|
150
|
+
end
|
151
|
+
|
152
|
+
private
|
153
|
+
|
154
|
+
on Events::BankAccountCreated do |event|
|
155
|
+
@aggregate_id = event.aggregate_id
|
156
|
+
@amount = 0
|
157
|
+
@first_name = event.first_name
|
158
|
+
@last_name = event.last_name
|
159
|
+
end
|
160
|
+
|
161
|
+
on Events::DepositPerformed do |event|
|
162
|
+
@amount += event.amount.to_i
|
163
|
+
end
|
164
|
+
end
|
165
|
+
end
|
166
|
+
```
|
167
|
+
|
168
|
+
### Events
|
169
|
+
Events describe what happened as a consequence of executing a command. Attributes defined on a command that was executed are saved as a `data` json object.
|
170
|
+
|
171
|
+
#### event_handler
|
172
|
+
|
173
|
+
Using event handler define which event handler is going to be used when this event is created. Selected event handler must be defined in `app/cqrs/event_handlers` folder.
|
174
|
+
|
175
|
+
`event_handler :bank_account` will use `EventHandlers::BankAccount` event handler.
|
176
|
+
|
177
|
+
#### store_accessor
|
178
|
+
|
179
|
+
You need to define accessors for data attributes saved in JSON.
|
180
|
+
|
181
|
+
```ruby
|
182
|
+
module Events
|
183
|
+
class BankAccountCreated < Eventusha::Event
|
184
|
+
event_handler :bank_account
|
185
|
+
|
186
|
+
store_accessor :data, :first_name, :last_name
|
187
|
+
end
|
188
|
+
end
|
189
|
+
```
|
190
|
+
|
191
|
+
### Event Handlers
|
192
|
+
|
193
|
+
Event handlers are usually updating view models after event is created. You need to define which event handler is going to be used for each event.
|
194
|
+
|
195
|
+
```ruby
|
196
|
+
module EventHandlers
|
197
|
+
class BankAccount < Eventusha::EventHandler
|
198
|
+
on Events::BankAccountCreated do |event|
|
199
|
+
::BankAccount.create(
|
200
|
+
aggregate_id: event.aggregate_id,
|
201
|
+
first_name: event.first_name,
|
202
|
+
last_name: event.last_name,
|
203
|
+
amount: 0
|
204
|
+
)
|
205
|
+
end
|
206
|
+
|
207
|
+
on Events::DepositPerformed do |event|
|
208
|
+
bank_account = ::BankAccount.find_by(aggregate_id: event.aggregate_id)
|
209
|
+
bank_account.amount += event.amount.to_i
|
210
|
+
bank_account.save
|
211
|
+
end
|
212
|
+
end
|
213
|
+
end
|
214
|
+
```
|
215
|
+
|
216
|
+
## Usage
|
217
|
+
|
218
|
+
|
219
|
+
|
220
|
+
## Development
|
221
|
+
|
222
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
223
|
+
|
224
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
225
|
+
|
226
|
+
## Contributing
|
227
|
+
|
228
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/eventusha. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
|
229
|
+
|
230
|
+
|
231
|
+
## License
|
232
|
+
|
233
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "eventusha"
|
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
data/eventusha.gemspec
ADDED
@@ -0,0 +1,41 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'eventusha/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "eventusha"
|
8
|
+
spec.version = Eventusha::VERSION
|
9
|
+
spec.authors = ["Nika Jukić"]
|
10
|
+
spec.email = ["nikajukic@gmail.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{Eventusha is an Event Sourcing framework for Ruby.}
|
13
|
+
spec.homepage = "https://github.com/nikajukic/eventusha"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
# Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
|
17
|
+
# to allow pushing to a single host or delete this section to allow pushing to any host.
|
18
|
+
if spec.respond_to?(:metadata)
|
19
|
+
spec.metadata['allowed_push_host'] = "https://rubygems.org"
|
20
|
+
else
|
21
|
+
raise "RubyGems 2.0 or newer is required to protect against " \
|
22
|
+
"public gem pushes."
|
23
|
+
end
|
24
|
+
|
25
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
26
|
+
f.match(%r{^(test|spec|features)/})
|
27
|
+
end
|
28
|
+
spec.bindir = "exe"
|
29
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
30
|
+
spec.require_paths = ["lib"]
|
31
|
+
|
32
|
+
spec.add_development_dependency "bundler", "~> 1.14"
|
33
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
34
|
+
spec.add_development_dependency "rspec-rails"
|
35
|
+
spec.add_development_dependency 'rails'
|
36
|
+
spec.add_development_dependency 'pg'
|
37
|
+
spec.add_development_dependency 'pry-byebug'
|
38
|
+
|
39
|
+
spec.add_dependency 'activerecord', '>= 4.0'
|
40
|
+
spec.add_dependency 'activemodel'
|
41
|
+
end
|
data/lib/eventusha.rb
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
require 'active_record'
|
2
|
+
require 'active_model'
|
3
|
+
|
4
|
+
require 'eventusha/version'
|
5
|
+
|
6
|
+
require 'eventusha/aggregate'
|
7
|
+
require 'eventusha/command_handler'
|
8
|
+
require 'eventusha/command'
|
9
|
+
require 'eventusha/event_handler'
|
10
|
+
require 'eventusha/event'
|
11
|
+
|
12
|
+
module Eventusha
|
13
|
+
end
|
@@ -0,0 +1,47 @@
|
|
1
|
+
module Eventusha
|
2
|
+
class Aggregate
|
3
|
+
attr_reader :aggregate_id
|
4
|
+
|
5
|
+
def self.on(*event_classes, &block)
|
6
|
+
event_classes.each do |event_class|
|
7
|
+
handler_name = "on_#{event_class.name.demodulize.underscore}"
|
8
|
+
define_method(handler_name, block)
|
9
|
+
private(handler_name)
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
def self.find(aggregate_id)
|
14
|
+
events = Event.where(aggregate_id: aggregate_id)
|
15
|
+
build_from(events)
|
16
|
+
end
|
17
|
+
|
18
|
+
def apply(event, published: false)
|
19
|
+
create_event(event) unless published
|
20
|
+
|
21
|
+
send(apply_event_method_name(event), event)
|
22
|
+
end
|
23
|
+
|
24
|
+
def self.build_from(events)
|
25
|
+
object = self.new
|
26
|
+
return object if events.blank?
|
27
|
+
|
28
|
+
events.each do |event|
|
29
|
+
event = event.becomes(event.name.constantize)
|
30
|
+
object.apply(event, published: true)
|
31
|
+
end
|
32
|
+
|
33
|
+
object
|
34
|
+
end
|
35
|
+
|
36
|
+
private
|
37
|
+
|
38
|
+
def apply_event_method_name(event)
|
39
|
+
"on_#{event.method_name}"
|
40
|
+
end
|
41
|
+
|
42
|
+
def create_event(event)
|
43
|
+
event.save
|
44
|
+
event.publish
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
module Eventusha
|
2
|
+
class Command
|
3
|
+
include ActiveModel::Model
|
4
|
+
|
5
|
+
attr_accessor :aggregate_id
|
6
|
+
|
7
|
+
def self.attributes(*attributes)
|
8
|
+
attr_accessor *attributes
|
9
|
+
define_method :readable_attributes do
|
10
|
+
attributes
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
def execute
|
15
|
+
return false if invalid?
|
16
|
+
|
17
|
+
command_handler = find_command_handler(self)
|
18
|
+
command_handler.execute(self)
|
19
|
+
end
|
20
|
+
|
21
|
+
def find_command_handler(command)
|
22
|
+
"CommandHandlers::#{self.class.name.demodulize}".constantize
|
23
|
+
end
|
24
|
+
|
25
|
+
def attributes
|
26
|
+
readable_attributes.each_with_object({}) do |attribute, attrs_hash|
|
27
|
+
attrs_hash[attribute] = instance_variable_get("@#{attribute}")
|
28
|
+
end.with_indifferent_access
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
module Eventusha
|
2
|
+
class CommandHandler
|
3
|
+
attr_reader :command
|
4
|
+
|
5
|
+
def initialize(command)
|
6
|
+
@command = command
|
7
|
+
end
|
8
|
+
|
9
|
+
def self.execute(command)
|
10
|
+
handler = new(command)
|
11
|
+
handler.execute
|
12
|
+
end
|
13
|
+
|
14
|
+
def self.aggregate(aggregate_name)
|
15
|
+
define_method :aggregate do
|
16
|
+
"Aggregates::#{aggregate_name.to_s.classify}".constantize
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
module Eventusha
|
2
|
+
class Event < ActiveRecord::Base
|
3
|
+
self.table_name = 'events'
|
4
|
+
|
5
|
+
def self.prepare(aggregate_id, attributes)
|
6
|
+
new(
|
7
|
+
aggregate_id: aggregate_id,
|
8
|
+
data: attributes,
|
9
|
+
name: self.name
|
10
|
+
)
|
11
|
+
end
|
12
|
+
|
13
|
+
def self.event_handler(handler_name)
|
14
|
+
define_method :event_handler_class do
|
15
|
+
"EventHandlers::#{handler_name.to_s.classify}".constantize
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
def method_name
|
20
|
+
name.demodulize.underscore
|
21
|
+
end
|
22
|
+
|
23
|
+
def publish
|
24
|
+
event_handler_class.new.send("on_#{method_name}", self)
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1,11 @@
|
|
1
|
+
module Eventusha
|
2
|
+
class EventHandler
|
3
|
+
def self.on(*event_classes, &block)
|
4
|
+
event_classes.each do |event_class|
|
5
|
+
handler_name = "on_#{event_class.name.demodulize.underscore}"
|
6
|
+
define_method(handler_name, block)
|
7
|
+
private(handler_name)
|
8
|
+
end
|
9
|
+
end
|
10
|
+
end
|
11
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
require 'rails/generators/migration'
|
2
|
+
require 'generators/eventusha/migration'
|
3
|
+
require 'eventusha'
|
4
|
+
|
5
|
+
module Eventusha
|
6
|
+
module Generators
|
7
|
+
class InstallGenerator < Rails::Generators::Base
|
8
|
+
include Rails::Generators::Migration
|
9
|
+
extend Eventusha::Generators::Migration
|
10
|
+
|
11
|
+
source_root File.expand_path('../templates', __FILE__)
|
12
|
+
|
13
|
+
def copy_migration
|
14
|
+
migration_template 'install.rb', 'db/migrate/create_events.rb'
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
module Eventusha
|
2
|
+
module Generators
|
3
|
+
module Migration
|
4
|
+
# Implement the required interface for Rails::Generators::Migration.
|
5
|
+
def next_migration_number(dirname) #:nodoc:
|
6
|
+
next_migration_number = current_migration_number(dirname) + 1
|
7
|
+
if ::ActiveRecord::Base.timestamped_migrations
|
8
|
+
[Time.now.utc.strftime("%Y%m%d%H%M%S"), "%.14d" % next_migration_number].max
|
9
|
+
else
|
10
|
+
"%.3d" % next_migration_number
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
metadata
ADDED
@@ -0,0 +1,178 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: eventusha
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Nika Jukić
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2018-05-21 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.14'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.14'
|
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-rails
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '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: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: pg
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: pry-byebug
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - ">="
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0'
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: activerecord
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - ">="
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '4.0'
|
104
|
+
type: :runtime
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - ">="
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '4.0'
|
111
|
+
- !ruby/object:Gem::Dependency
|
112
|
+
name: activemodel
|
113
|
+
requirement: !ruby/object:Gem::Requirement
|
114
|
+
requirements:
|
115
|
+
- - ">="
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: '0'
|
118
|
+
type: :runtime
|
119
|
+
prerelease: false
|
120
|
+
version_requirements: !ruby/object:Gem::Requirement
|
121
|
+
requirements:
|
122
|
+
- - ">="
|
123
|
+
- !ruby/object:Gem::Version
|
124
|
+
version: '0'
|
125
|
+
description:
|
126
|
+
email:
|
127
|
+
- nikajukic@gmail.com
|
128
|
+
executables: []
|
129
|
+
extensions: []
|
130
|
+
extra_rdoc_files: []
|
131
|
+
files:
|
132
|
+
- ".gitignore"
|
133
|
+
- ".rspec"
|
134
|
+
- ".travis.yml"
|
135
|
+
- CODE_OF_CONDUCT.md
|
136
|
+
- Gemfile
|
137
|
+
- LICENSE.txt
|
138
|
+
- README.md
|
139
|
+
- Rakefile
|
140
|
+
- bin/console
|
141
|
+
- bin/setup
|
142
|
+
- eventusha.gemspec
|
143
|
+
- lib/eventusha.rb
|
144
|
+
- lib/eventusha/aggregate.rb
|
145
|
+
- lib/eventusha/command.rb
|
146
|
+
- lib/eventusha/command_handler.rb
|
147
|
+
- lib/eventusha/event.rb
|
148
|
+
- lib/eventusha/event_handler.rb
|
149
|
+
- lib/eventusha/version.rb
|
150
|
+
- lib/generators/eventusha/install_generator.rb
|
151
|
+
- lib/generators/eventusha/migration.rb
|
152
|
+
- lib/generators/eventusha/templates/install.rb
|
153
|
+
homepage: https://github.com/nikajukic/eventusha
|
154
|
+
licenses:
|
155
|
+
- MIT
|
156
|
+
metadata:
|
157
|
+
allowed_push_host: https://rubygems.org
|
158
|
+
post_install_message:
|
159
|
+
rdoc_options: []
|
160
|
+
require_paths:
|
161
|
+
- lib
|
162
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
163
|
+
requirements:
|
164
|
+
- - ">="
|
165
|
+
- !ruby/object:Gem::Version
|
166
|
+
version: '0'
|
167
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
168
|
+
requirements:
|
169
|
+
- - ">="
|
170
|
+
- !ruby/object:Gem::Version
|
171
|
+
version: '0'
|
172
|
+
requirements: []
|
173
|
+
rubyforge_project:
|
174
|
+
rubygems_version: 2.4.5
|
175
|
+
signing_key:
|
176
|
+
specification_version: 4
|
177
|
+
summary: Eventusha is an Event Sourcing framework for Ruby.
|
178
|
+
test_files: []
|