event_sourcery-rails 0.1.0 → 0.2.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/README.md +66 -8
- data/lib/event_sourcery/rails/command.rb +30 -0
- data/lib/event_sourcery/rails/command_handler.rb +27 -0
- data/lib/event_sourcery/rails/version.rb +1 -1
- data/lib/event_sourcery/rails.rb +2 -1
- metadata +6 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f691f46a44b4d5909efb60f1d87c2d463382c75dfed89877a26d1771162a7458
|
4
|
+
data.tar.gz: 1fca779982165b17b3aad6be86bff5e49123f5c9ec1e3d41bb4345e28bb95ded
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5fa9870de94fb1d99733c1b7bb5c69b7a566454eb28279328ea7cc4eb7d9c326682008eb55ec843aff75d4075880be6ecb6de86e23554e74e7022468bac419c8
|
7
|
+
data.tar.gz: 43c35bc1f3efda9b5be3ea4aa1f5ad944f1c09aa4a0dfc383e5c485698332f9ee0f641bc9f8cbe96e8e743f292ae5dce6cbf375901c72b272e6951f4e3373a3a
|
data/README.md
CHANGED
@@ -2,7 +2,65 @@
|
|
2
2
|
Short description and motivation.
|
3
3
|
|
4
4
|
## Usage
|
5
|
-
|
5
|
+
|
6
|
+
### Commands
|
7
|
+
|
8
|
+
EventSourcery::Rails adds an optional base class for commands to enforce
|
9
|
+
instantiating commands with an `aggregate_id` and required parameters as keyword
|
10
|
+
arguments. Defined attributes are available with an `attr_reader`.
|
11
|
+
|
12
|
+
```ruby
|
13
|
+
class AddUser < EventSourcery::Rails::Command
|
14
|
+
attributes :name, :email
|
15
|
+
end
|
16
|
+
|
17
|
+
AddUser.new # => raises ArgumentError.new("missing keywords: aggregate_id, name, email")
|
18
|
+
|
19
|
+
command = AddUser.new(aggregate_id: 'aggregate-id',
|
20
|
+
name: 'name',
|
21
|
+
email: 'email')
|
22
|
+
command.aggregate_id # => "aggregate-id"
|
23
|
+
command.name # => "name"
|
24
|
+
command.email # => "email"
|
25
|
+
```
|
26
|
+
|
27
|
+
### Command Handlers
|
28
|
+
|
29
|
+
You can also optionally include `EventSourcery::Rails::CommandHandler` to use
|
30
|
+
use a callback DSL for binding commands. This DSL allows your application code
|
31
|
+
to use all command handlers with `#call`.
|
32
|
+
|
33
|
+
**Todo**
|
34
|
+
|
35
|
+
- [ ] Consider switching to a base class with common initializer and
|
36
|
+
`with_aggregate`
|
37
|
+
- [ ] Introduce API for invoking all known command handlers with an array of
|
38
|
+
commands.
|
39
|
+
|
40
|
+
```ruby
|
41
|
+
class UserCommandHandler
|
42
|
+
include EventSourcery::Rails::CommandHandler
|
43
|
+
|
44
|
+
attr_reader :repository
|
45
|
+
|
46
|
+
def initialize(repository: EventSourceryRails.repository)
|
47
|
+
@repository = repository
|
48
|
+
end
|
49
|
+
|
50
|
+
on AddUser do |command|
|
51
|
+
aggregate = repository.load(UserAggregate, aggregate_id)
|
52
|
+
aggregate.add(name: command.name,
|
53
|
+
email: command.email)
|
54
|
+
repository.save(aggregate)
|
55
|
+
end
|
56
|
+
|
57
|
+
on UpdateUserEmail do |command|
|
58
|
+
aggregate = repository.load(UserAggregate, aggregate_id)
|
59
|
+
aggregate.update_email(email: command.email)
|
60
|
+
repository.save(aggregate)
|
61
|
+
end
|
62
|
+
end
|
63
|
+
```
|
6
64
|
|
7
65
|
## Installation
|
8
66
|
Add the following line to your Gemfile.
|
@@ -21,14 +79,14 @@ Next, your need to run the generator:
|
|
21
79
|
$ rails generate event_sourcery_rails:install
|
22
80
|
```
|
23
81
|
|
24
|
-
At this point you will have an initializer to configure EventSourcery and
|
25
|
-
|
82
|
+
At this point you will have an initializer to configure EventSourcery and the
|
83
|
+
following Rake tasks.
|
26
84
|
|
27
|
-
```
|
28
|
-
rails event_sourcery:db:migrate # create the event sourcery schema
|
29
|
-
rails event_sourcery:processors:setup # create projector schemas
|
30
|
-
rails event_sourcery:processors:reset # drop and recreate projector schemas and data
|
31
|
-
rails event_sourcery:processors:run # start event stream processors
|
85
|
+
```bash
|
86
|
+
$ rails event_sourcery:db:migrate # create the event sourcery schema
|
87
|
+
$ rails event_sourcery:processors:setup # create projector schemas
|
88
|
+
$ rails event_sourcery:processors:reset # drop and recreate projector schemas and data
|
89
|
+
$ rails event_sourcery:processors:run # start event stream processors
|
32
90
|
```
|
33
91
|
|
34
92
|
Typically you'll have the following in your Procfile.
|
@@ -0,0 +1,30 @@
|
|
1
|
+
module EventSourcery
|
2
|
+
module Rails
|
3
|
+
class Command
|
4
|
+
attr_reader :aggregate_id
|
5
|
+
|
6
|
+
def initialize(aggregate_id:)
|
7
|
+
@aggregate_id = aggregate_id
|
8
|
+
end
|
9
|
+
|
10
|
+
def self.attributes(*attributes)
|
11
|
+
attr_reader *attributes
|
12
|
+
|
13
|
+
method_arguments = attributes.map { |arg| "#{arg}:" }.join(', ')
|
14
|
+
method_assignments = attributes.map { |arg| "@#{arg} = #{arg}" }.join(';')
|
15
|
+
method_attrs_to_hash = attributes.map { |arg| "#{arg}: #{arg}" }.join(',')
|
16
|
+
|
17
|
+
class_eval <<~CODE
|
18
|
+
def initialize(aggregate_id:, #{method_arguments})
|
19
|
+
@aggregate_id = aggregate_id
|
20
|
+
#{method_assignments}
|
21
|
+
end
|
22
|
+
|
23
|
+
def to_hash
|
24
|
+
{#{method_attrs_to_hash}}
|
25
|
+
end
|
26
|
+
CODE
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
module EventSourcery
|
2
|
+
module Rails
|
3
|
+
module CommandHandler
|
4
|
+
|
5
|
+
def self.included(klass)
|
6
|
+
klass.extend ClassMethods
|
7
|
+
end
|
8
|
+
|
9
|
+
def call(command)
|
10
|
+
return unless self.class.command_events.has_key?(command.class)
|
11
|
+
instance_exec command, &self.class.command_events[command.class]
|
12
|
+
end
|
13
|
+
|
14
|
+
private
|
15
|
+
|
16
|
+
module ClassMethods
|
17
|
+
def command_events
|
18
|
+
@command_events ||= {}
|
19
|
+
end
|
20
|
+
|
21
|
+
def on(command, &block)
|
22
|
+
self.command_events[command] = block
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
data/lib/event_sourcery/rails.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: event_sourcery-rails
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Baylor Rae'
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2019-02-
|
11
|
+
date: 2019-02-10 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|
@@ -30,14 +30,14 @@ dependencies:
|
|
30
30
|
requirements:
|
31
31
|
- - "~>"
|
32
32
|
- !ruby/object:Gem::Version
|
33
|
-
version: '1.
|
33
|
+
version: '1.3'
|
34
34
|
type: :development
|
35
35
|
prerelease: false
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
37
37
|
requirements:
|
38
38
|
- - "~>"
|
39
39
|
- !ruby/object:Gem::Version
|
40
|
-
version: '1.
|
40
|
+
version: '1.3'
|
41
41
|
- !ruby/object:Gem::Dependency
|
42
42
|
name: rspec-rails
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|
@@ -78,6 +78,8 @@ files:
|
|
78
78
|
- README.md
|
79
79
|
- Rakefile
|
80
80
|
- lib/event_sourcery/rails.rb
|
81
|
+
- lib/event_sourcery/rails/command.rb
|
82
|
+
- lib/event_sourcery/rails/command_handler.rb
|
81
83
|
- lib/event_sourcery/rails/generators/install_generator.rb
|
82
84
|
- lib/event_sourcery/rails/generators/templates/event_sourcery.rake
|
83
85
|
- lib/event_sourcery/rails/generators/templates/initializer.rb
|