event_sourcery-rails 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 4c44446ad62fb825fb92ea6d5b2172a710b6a903de6c3c319559bdd570f44294
4
- data.tar.gz: 59f59a84434b1e5a56b28a1b5d1013003c01038083fafa490ef882f78b433255
3
+ metadata.gz: f691f46a44b4d5909efb60f1d87c2d463382c75dfed89877a26d1771162a7458
4
+ data.tar.gz: 1fca779982165b17b3aad6be86bff5e49123f5c9ec1e3d41bb4345e28bb95ded
5
5
  SHA512:
6
- metadata.gz: ae8a0001888778819f8b9636644d6d1184e47fa8953901d99beb59f2d86c935e4114ad56e7b4055f2df93ae3e8a6c7ae949129be5149f7c631104480bd82e321
7
- data.tar.gz: 9cb58b78bafec58bb05380f5723e6d270022fab2806b1f91b2667f4eba53733be44aa767ea78411e639ce9a201e2b9eabd9f153f615fd7f65e8846bc22166c50
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
- How to use my plugin.
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 a
25
- rake file with the following tasks.
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
@@ -1,5 +1,5 @@
1
1
  module EventSourcery
2
2
  module Rails
3
- VERSION = '0.1.0'
3
+ VERSION = '0.2.0'
4
4
  end
5
5
  end
@@ -2,6 +2,7 @@ require "event_sourcery/rails/railtie"
2
2
 
3
3
  module EventSourcery
4
4
  module Rails
5
- # Your code goes here...
5
+ autoload :Command, "event_sourcery/rails/command"
6
+ autoload :CommandHandler, "event_sourcery/rails/command_handler"
6
7
  end
7
8
  end
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.1.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-09 00:00:00.000000000 Z
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.4'
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.4'
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