rails_event_store 0.1.4 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +7 -0
- data/README.md +6 -0
- data/lib/rails_event_store.rb +2 -3
- data/lib/rails_event_store/event.rb +18 -2
- data/lib/rails_event_store/repositories/event_repository.rb +9 -13
- data/lib/rails_event_store/version.rb +1 -1
- metadata +2 -5
- data/lib/rails_event_store/aggregate_root.rb +0 -28
- data/lib/rails_event_store/event_entity.rb +0 -10
- data/lib/rails_event_store/repositories/aggregate_repository.rb +0 -28
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d7cd8418290dab99d162367189c7c6fb7bd6b581
|
4
|
+
data.tar.gz: 33ef529d3cb2c33744de40c986b58d00dab9afe3
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d4f1f8817d52951672bc55d8779b2a8fce49790ea925440335c7f5f7537827bb2d0cf1679110c9bb44bb9c7648da396d691e099e77f01068c6860c5f469eb050
|
7
|
+
data.tar.gz: e88b5409772430c1c02818d1bdb9661a73d8061c4f57e7a88318044c1854e1eaaec6606c382fbfeb196b1985f147815f627db2dfd1be1fd80742a5e8f0a6bdd6
|
data/CHANGELOG.md
CHANGED
@@ -1,5 +1,12 @@
|
|
1
1
|
### HEAD
|
2
2
|
|
3
|
+
### 0.2.0 (29.01.2016)
|
4
|
+
|
5
|
+
* Removed EventEntity class
|
6
|
+
* All read & returned events are not instances of RailsEventStore::Event class
|
7
|
+
* RailsEventStore::Event class allows for easier events creation and access to data attributes
|
8
|
+
* AggregateRoot module & repository extracted to new gem (aggregate_root)
|
9
|
+
|
3
10
|
### 0.1.2 (26.05.2015)
|
4
11
|
|
5
12
|
* Moved most core features to the separate gem `ruby_event_store`. We left only rails related implementation here.
|
data/README.md
CHANGED
@@ -134,6 +134,12 @@ invoice = InvoiceReadModel.new
|
|
134
134
|
client.subscribe_to_all_events(invoice)
|
135
135
|
```
|
136
136
|
|
137
|
+
#### Building an event sourced application with RailsEventStore gem
|
138
|
+
|
139
|
+
ArrgegateRoot module & AggregateReporitory have been extracted from RailsEventStore to separate gem.
|
140
|
+
See [aggregate_root](https://github.com/arkency/aggregate_root) gem readme to find help how to start.
|
141
|
+
Also [this example](https://github.com/mpraglowski/cqrs-es-sample-with-res) might be useful.
|
142
|
+
|
137
143
|
#### Resources
|
138
144
|
|
139
145
|
There're already few blogposts about Rails EventStore. Check them out:
|
data/lib/rails_event_store.rb
CHANGED
@@ -1,13 +1,12 @@
|
|
1
|
-
require '
|
1
|
+
require 'ruby_event_store'
|
2
|
+
|
2
3
|
require 'rails_event_store/models/event'
|
3
4
|
require 'rails_event_store/generators/migrate_generator'
|
4
5
|
require 'rails_event_store/generators/templates/migration_template'
|
5
6
|
require 'rails_event_store/version'
|
6
7
|
require 'rails_event_store/event'
|
7
8
|
require 'rails_event_store/repositories/event_repository'
|
8
|
-
require 'rails_event_store/repositories/aggregate_repository'
|
9
9
|
require 'rails_event_store/event_handlers/slack_event_handler'
|
10
|
-
require 'rails_event_store/aggregate_root'
|
11
10
|
require 'rails_event_store/errors'
|
12
11
|
require 'rails_event_store/client'
|
13
12
|
require 'rails_event_store/constants'
|
@@ -1,3 +1,19 @@
|
|
1
|
-
|
1
|
+
module RailsEventStore
|
2
|
+
class Event < RubyEventStore::Event
|
3
|
+
def initialize(**args)
|
4
|
+
attributes = args.except(:event_type, :event_id, :metadata)
|
5
|
+
singleton_class = (class << self; self; end)
|
6
|
+
attributes.each do |key, value|
|
7
|
+
singleton_class.send(:define_method, key) { value }
|
8
|
+
end
|
2
9
|
|
3
|
-
|
10
|
+
event_data = {
|
11
|
+
event_type: args[:event_type] || self.class.name,
|
12
|
+
data: attributes,
|
13
|
+
}
|
14
|
+
event_data[:event_id] = args[:event_id] if args.key?(:event_id)
|
15
|
+
event_data[:metadata] = args[:metadata] if args.key?(:metadata)
|
16
|
+
super(event_data)
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -8,11 +8,11 @@ module RailsEventStore
|
|
8
8
|
attr_reader :adapter
|
9
9
|
|
10
10
|
def find(condition)
|
11
|
-
build_event_entity
|
11
|
+
build_event_entity(adapter.where(condition).first)
|
12
12
|
end
|
13
13
|
|
14
14
|
def create(data)
|
15
|
-
build_event_entity
|
15
|
+
build_event_entity(adapter.create(data))
|
16
16
|
rescue ActiveRecord::RecordNotUnique
|
17
17
|
raise EventCannotBeSaved
|
18
18
|
end
|
@@ -23,33 +23,29 @@ module RailsEventStore
|
|
23
23
|
end
|
24
24
|
|
25
25
|
def get_all_events
|
26
|
-
adapter.order('id ASC').order('stream').all.map
|
26
|
+
adapter.order('id ASC').order('stream').all.map(&method(:build_event_entity))
|
27
27
|
end
|
28
28
|
|
29
29
|
def last_stream_event(stream_name)
|
30
|
-
build_event_entity
|
30
|
+
build_event_entity(adapter.where(stream: stream_name).last)
|
31
31
|
end
|
32
32
|
|
33
33
|
def load_all_events_forward(stream_name)
|
34
|
-
adapter.where(stream: stream_name).order('id ASC').map
|
34
|
+
adapter.where(stream: stream_name).order('id ASC').map(&method(:build_event_entity))
|
35
35
|
end
|
36
36
|
|
37
37
|
def load_events_batch(stream_name, start_point, count)
|
38
|
-
adapter.where('id >= ? AND stream = ?', start_point, stream_name).limit(count).map
|
38
|
+
adapter.where('id >= ? AND stream = ?', start_point, stream_name).limit(count).map(&method(:build_event_entity))
|
39
39
|
end
|
40
40
|
|
41
41
|
private
|
42
42
|
|
43
43
|
def build_event_entity(record)
|
44
|
-
|
45
|
-
|
46
|
-
stream: record.stream,
|
44
|
+
return nil unless record
|
45
|
+
::RailsEventStore::Event.new(record.data.merge(
|
47
46
|
event_type: record.event_type,
|
48
47
|
event_id: record.event_id,
|
49
|
-
metadata: record.metadata
|
50
|
-
data: record.data,
|
51
|
-
created_at: record.created_at
|
52
|
-
)
|
48
|
+
metadata: record.metadata))
|
53
49
|
end
|
54
50
|
|
55
51
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rails_event_store
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- rybex
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: exe
|
11
11
|
cert_chain: []
|
12
|
-
date: 2016-01-
|
12
|
+
date: 2016-01-29 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: bundler
|
@@ -153,17 +153,14 @@ files:
|
|
153
153
|
- README.md
|
154
154
|
- Rakefile
|
155
155
|
- lib/rails_event_store.rb
|
156
|
-
- lib/rails_event_store/aggregate_root.rb
|
157
156
|
- lib/rails_event_store/client.rb
|
158
157
|
- lib/rails_event_store/constants.rb
|
159
158
|
- lib/rails_event_store/errors.rb
|
160
159
|
- lib/rails_event_store/event.rb
|
161
|
-
- lib/rails_event_store/event_entity.rb
|
162
160
|
- lib/rails_event_store/event_handlers/slack_event_handler.rb
|
163
161
|
- lib/rails_event_store/generators/migrate_generator.rb
|
164
162
|
- lib/rails_event_store/generators/templates/migration_template.rb
|
165
163
|
- lib/rails_event_store/models/event.rb
|
166
|
-
- lib/rails_event_store/repositories/aggregate_repository.rb
|
167
164
|
- lib/rails_event_store/repositories/event_repository.rb
|
168
165
|
- lib/rails_event_store/version.rb
|
169
166
|
- rails_event_store.gemspec
|
@@ -1,28 +0,0 @@
|
|
1
|
-
require 'active_support/inflector'
|
2
|
-
|
3
|
-
module RailsEventStore
|
4
|
-
module AggregateRoot
|
5
|
-
def apply(event)
|
6
|
-
apply_event(event)
|
7
|
-
unpublished_events << event
|
8
|
-
end
|
9
|
-
|
10
|
-
def apply_old_event(event)
|
11
|
-
apply_event(event)
|
12
|
-
end
|
13
|
-
|
14
|
-
def unpublished_events
|
15
|
-
@unpublished_events ||= []
|
16
|
-
end
|
17
|
-
|
18
|
-
private
|
19
|
-
|
20
|
-
def apply_event(event)
|
21
|
-
send("apply_#{event.event_type.underscore.gsub('/', '_')}", event)
|
22
|
-
end
|
23
|
-
|
24
|
-
def generate_uuid
|
25
|
-
SecureRandom.uuid
|
26
|
-
end
|
27
|
-
end
|
28
|
-
end
|
@@ -1,28 +0,0 @@
|
|
1
|
-
module RailsEventStore
|
2
|
-
module Repositories
|
3
|
-
class AggregateRepository
|
4
|
-
def initialize(event_store = default_event_store)
|
5
|
-
@event_store = event_store
|
6
|
-
end
|
7
|
-
|
8
|
-
def store(aggregate)
|
9
|
-
aggregate.unpublished_events.each do |event|
|
10
|
-
event_store.publish_event(event, aggregate.id)
|
11
|
-
end
|
12
|
-
end
|
13
|
-
|
14
|
-
def load(aggregate)
|
15
|
-
events = event_store.read_all_events(aggregate.id)
|
16
|
-
events.each do |event|
|
17
|
-
aggregate.apply_old_event(event)
|
18
|
-
end
|
19
|
-
end
|
20
|
-
|
21
|
-
attr_accessor :event_store
|
22
|
-
|
23
|
-
def default_event_store
|
24
|
-
Client.new
|
25
|
-
end
|
26
|
-
end
|
27
|
-
end
|
28
|
-
end
|