rails_event_store 0.1.1 → 0.1.2

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
  SHA1:
3
- metadata.gz: 55f134f7a328395f2aaaee363409e3174c781ced
4
- data.tar.gz: c0a7f0ac41fbe5688cc0407cd3ed07a62437d9f1
3
+ metadata.gz: b92227a5b22816861dab6e283090912863e3c60f
4
+ data.tar.gz: 460ea96f8e15e7a79a406c1e12dcb43293c2f518
5
5
  SHA512:
6
- metadata.gz: fae4e8a9c0f3d414720795c557e7a6c2647b6d5b28f2197eba9715d54579b4028b063d6ed8a357055a158c1d694c71a1e0238f03fa8687d6251061101671019a
7
- data.tar.gz: d4336f83e8c4a6e366ccb9e9e2f0a2c2deb545e766f21446354460fb92b288ec7e4129d8e03063cad436b64c78a18586c75c42536e8618fd2fe54d180cd1e2c8
6
+ metadata.gz: 0f9162341516cb547bd46adf076ceeb48c36309beb5947be4a7f89bc52a2eeba48a47e9fed59e35c31478ad05b91f5c3604754a51b03c9f2cedeefd5f155a8c5
7
+ data.tar.gz: 273c27809b1c4695b4c1c99e6b3e17319c3575706fba7cb426fa46cd8208400d43d6357082bffa16085ccb5eaf15943d2b78e069ff34c79a62380591d0151628
@@ -1,10 +1,10 @@
1
1
  language: ruby
2
2
  rvm:
3
- - 2.1.5
4
-
3
+ - 2.1.5
5
4
  before_install: gem install bundler
6
-
7
- gemfile:
8
- Gemfile
9
-
10
- script: bundle exec rspec
5
+ gemfile: Gemfile
6
+ notifications:
7
+ slack:
8
+ secure: i/GM4u6oCfgdt9ablmOx4ov3Y9bHJjHabXxWXt+GLRlfW5MDdzwEH0QOx2mveVbit+00rKKqZzBAbEQ1yLo4MLLovHyJvCzoluC10u0MRIR7e8ZlCdlww+IL3JmD8E7G1a/fKEMoKwfLrYUXa5K0oAeKpJ3RwP+tTD+g3KxQfIA=
9
+ env:
10
+ - CODECLIMATE_REPO_TOKEN=24ac54c50ae022eb008ebd1f1e5da043d6419759136d4027d0266426c6198ef2
@@ -0,0 +1,17 @@
1
+ ### HEAD
2
+
3
+ ### 0.1.2 (26.05.2015)
4
+
5
+ * Moved most core features to the separate gem `ruby_event_store`. We left only rails related implementation here.
6
+ * It's now assumed that `event_id` has a unique index in the database (the same is applied to the initial migration generator)
7
+ * Model is no longer validating uniqueness of `event_id` via ActiveRecord
8
+ * Event does not need any data now, it can be created just like `OrderCreated.new` (without any arguments)
9
+ * Migration generator is no more generating the `updated_at` field in the `event_store_events` table. We also advise to remove this column, since events shouldn't be *ever* modified.
10
+ * In the event's metadata there's a new field `published_at`
11
+ * Added the `subscribe_to_all_events` method to `RailsEventStore::Client`
12
+ * Subscribing to only one event via `client.subscribe(subscriber, 'OrderCreated')` no longer works. You should use `client.subscribe(subscriber, ['OrderCreated'])` instead.
13
+ * Default event's name is no longer `OrderCreated` for the `OrderCreated` event in the `Orders` namespace, now it's `Orders::OrderCreated`
14
+
15
+ ### 0.1.1 (22.04.2015)
16
+
17
+ Initial release.
data/Gemfile CHANGED
@@ -1,2 +1,3 @@
1
1
  source 'https://rubygems.org'
2
2
  gemspec
3
+ gem 'codeclimate-test-reporter', group: :test, require: nil
data/LICENSE ADDED
@@ -0,0 +1,5 @@
1
+ Copyright (c) Arkency
2
+
3
+ Rails EventStore is an Open Source project licensed under the terms of
4
+ the LGPLv3 license. Please see <http://www.gnu.org/licenses/lgpl-3.0.html>
5
+ for license text.
data/README.md CHANGED
@@ -1,3 +1,9 @@
1
+ [![Build Status](https://travis-ci.org/arkency/rails_event_store.svg?branch=master)](https://travis-ci.org/arkency/rails_event_store)
2
+ [![Gem Version](https://badge.fury.io/rb/rails_event_store.svg)](http://badge.fury.io/rb/rails_event_store)
3
+ [![Code Climate](https://codeclimate.com/github/arkency/rails_event_store/badges/gpa.svg)](https://codeclimate.com/github/arkency/rails_event_store)
4
+ [![Test Coverage](https://codeclimate.com/github/arkency/rails_event_store/badges/coverage.svg)](https://codeclimate.com/github/arkency/rails_event_store)
5
+ [![Join the chat at https://gitter.im/arkency/rails_event_store](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/arkency/rails_event_store?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
6
+
1
7
  # EventStore
2
8
 
3
9
  A Ruby implementation of an EventStore based on Active Record.
@@ -71,7 +77,7 @@ client.publish_event(event, stream_name, expected_version)
71
77
 
72
78
  ```ruby
73
79
  stream_name = "order_1"
74
- start = 1
80
+ start = "850c347f-423a-4158-a5ce-b885396c5b73"
75
81
  count = 40
76
82
  client.read_all_events(stream_name, start, count)
77
83
  ```
@@ -93,19 +99,46 @@ This method allows us to load all stored events ascending.
93
99
  client.read_all_streams
94
100
  ```
95
101
 
102
+ #### Deleting stream
103
+
104
+ You can permanently delete all events from a specific stream. Use this wisely.
105
+
106
+ ```ruby
107
+ stream_name = "product_1"
108
+ client.delete_stream(stream_name)
109
+ ```
110
+
96
111
  #### Subscribing to events
97
112
 
98
113
  To listen on specific events synchronously you have to create subscriber reprezentation. The only requirement is that subscriber class has to implement the 'handle_event(event)' method.
99
114
 
100
115
  ```ruby
101
116
  class InvoiceReadModel
102
- def handle_event(event)
103
- #we deal here with event's data
104
- end
117
+ def handle_event(event)
118
+ #we deal here with event's data
119
+ end
105
120
  end
106
121
  ```
107
122
 
123
+ * You can subscribe on specific set of events
124
+
108
125
  ```ruby
109
126
  invoice = InvoiceReadModel.new
110
127
  client.subscribe(invoice, ['PriceChanged', 'ProductAdded'])
111
- ```
128
+ ```
129
+
130
+ * You can also listen on all incoming events
131
+
132
+ ```ruby
133
+ invoice = InvoiceReadModel.new
134
+ client.subscribe(invoice)
135
+ ```
136
+
137
+ #### Resources
138
+
139
+ There're already few blogposts about Rails EventStore. Check them out:
140
+
141
+ * [Why use Event Sourcing](http://blog.arkency.com/2015/03/why-use-event-sourcing/)
142
+ * [The Event Store for Rails developers](http://blog.arkency.com/2015/04/the-event-store-for-rails-developers/)
143
+ * [Fast introduction to Event Sourcing for Ruby programmers](http://blog.arkency.com/2015/03/fast-introduction-to-event-sourcing-for-ruby-programmers/)
144
+ * [Why I want to introduce mutation testing to the rails_event_store gem](http://blog.arkency.com/2015/04/why-i-want-to-introduce-mutation-testing-to-the-rails-event-store-gem/)
data/Rakefile CHANGED
@@ -1,3 +1,6 @@
1
1
  require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
+ RSpec::Core::RakeTask.new(:spec)
4
+ task default: :spec
2
5
 
3
6
 
@@ -4,11 +4,10 @@ require 'rails_event_store/generators/templates/migration_template'
4
4
  require 'rails_event_store/version'
5
5
  require 'rails_event_store/event'
6
6
  require 'rails_event_store/repositories/event_repository'
7
- require 'rails_event_store/actions/append_event_to_stream'
8
- require 'rails_event_store/actions/read_all_events'
9
- require 'rails_event_store/actions/read_events_batch'
10
- require 'rails_event_store/actions/delete_stream_events'
11
- require 'rails_event_store/actions/read_all_streams'
12
- require 'rails_event_store/pub_sub/broker'
7
+ require 'rails_event_store/repositories/aggregate_repository'
8
+ require 'rails_event_store/event_handlers/slack_event_handler'
9
+ require 'rails_event_store/aggregate_root'
13
10
  require 'rails_event_store/errors'
14
11
  require 'rails_event_store/client'
12
+ require 'rails_event_store/constants'
13
+ require 'rails_event_store/utils/string_utils'
@@ -0,0 +1,26 @@
1
+ module RailsEventStore
2
+ module AggregateRoot
3
+ def apply(event)
4
+ apply_event(event)
5
+ unpublished_events << event
6
+ end
7
+
8
+ def apply_old_event(event)
9
+ apply_event(event)
10
+ end
11
+
12
+ def unpublished_events
13
+ @unpublished_events ||= []
14
+ end
15
+
16
+ private
17
+
18
+ def apply_event(event)
19
+ public_send("apply_#{StringUtils.underscore(event.event_type)}")
20
+ end
21
+
22
+ def generate_uuid
23
+ SecureRandom.uuid
24
+ end
25
+ end
26
+ end
@@ -4,36 +4,40 @@ module RailsEventStore
4
4
  def initialize(repository = Repositories::EventRepository.new)
5
5
  @repository = repository
6
6
  end
7
+ attr_reader :repository
7
8
 
8
- def publish_event(event_data, stream_name = 'all', expected_version = nil)
9
- event = Actions::AppendEventToStream.new(@repository).call(stream_name, event_data, expected_version)
10
- event_broker.notify_subscribers(event)
9
+ def publish_event(event_data, stream_name = GLOBAL_STREAM, expected_version = nil)
10
+ event_store.publish_event(event_data, stream_name, expected_version)
11
11
  end
12
12
 
13
13
  def delete_stream(stream_name)
14
- Actions::DeleteStreamEvents.new(@repository).call(stream_name)
14
+ event_store.delete_stream(stream_name)
15
15
  end
16
16
 
17
17
  def read_events(stream_name, start, count)
18
- Actions::ReadEventsBatch.new(@repository).call(stream_name, start, count)
18
+ event_store.read_events(stream_name, start, count)
19
19
  end
20
20
 
21
21
  def read_all_events(stream_name)
22
- Actions::ReadAllEvents.new(@repository).call(stream_name)
22
+ event_store.read_all_events(stream_name)
23
23
  end
24
24
 
25
25
  def read_all_streams
26
- Actions::ReadAllStreams.new(@repository).call
26
+ event_store.read_all_streams
27
27
  end
28
28
 
29
- def subscribe(subscriber, event_types = [])
30
- event_broker.add_subscriber(subscriber, event_types)
29
+ def subscribe(subscriber, event_types)
30
+ event_store.subscribe(subscriber, event_types)
31
+ end
32
+
33
+ def subscribe_to_all_events(subscriber)
34
+ event_store.subscribe_to_all_events(subscriber)
31
35
  end
32
36
 
33
37
  private
34
38
 
35
- def event_broker
36
- @event_broker ||= PubSub::Broker.new
39
+ def event_store
40
+ @event_store ||= RubyEventStore::Facade.new(repository)
37
41
  end
38
42
  end
39
43
  end
@@ -0,0 +1,3 @@
1
+ module RailsEventStore
2
+ GLOBAL_STREAM = 'all'.freeze
3
+ end
@@ -1,8 +1,3 @@
1
1
  module RailsEventStore
2
- WrongExpectedEventVersion = Class.new(StandardError)
3
- IncorrectStreamData = Class.new(StandardError)
4
2
  EventCannotBeSaved = Class.new(StandardError)
5
- EventNotFound = Class.new(StandardError)
6
- SubscriberNotExist = Class.new(StandardError)
7
- MethodNotDefined = Class.new(StandardError)
8
3
  end
@@ -1,44 +1,3 @@
1
- require 'securerandom'
1
+ require 'ruby_event_store'
2
2
 
3
- module RailsEventStore
4
- class Event
5
-
6
- def initialize(event_data)
7
- @event_type = event_data.fetch(:event_type, event_name)
8
- @event_id = event_data.fetch(:event_id, generate_id).to_s
9
- @metadata = event_data.fetch(:metadata, nil)
10
- @data = event_data.fetch(:data, nil)
11
- end
12
-
13
- attr_reader :event_type, :event_id, :metadata, :data
14
-
15
- def validate!
16
- [event_type, event_id, data].each do |attribute|
17
- raise IncorrectStreamData if is_invalid?(attribute)
18
- end
19
- end
20
-
21
- def to_h
22
- {
23
- event_type: event_type,
24
- event_id: event_id,
25
- metadata: metadata,
26
- data: data
27
- }
28
- end
29
-
30
- private
31
-
32
- def is_invalid?(attribute)
33
- attribute.nil? || attribute.empty?
34
- end
35
-
36
- def generate_id
37
- SecureRandom.uuid
38
- end
39
-
40
- def event_name
41
- self.class.name
42
- end
43
- end
44
- end
3
+ RailsEventStore::Event = RubyEventStore::Event
@@ -6,6 +6,5 @@ module RailsEventStore
6
6
  self.table_name = 'event_store_events'
7
7
  serialize :metadata
8
8
  serialize :data
9
- validates_uniqueness_of :event_id
10
9
  end
11
10
  end
@@ -0,0 +1,41 @@
1
+ require 'net/http'
2
+ require 'json'
3
+
4
+ module RailsEventStore
5
+ module EventHandlers
6
+ class SlackEventHandler
7
+ class HTTPClient
8
+ def post(url, params)
9
+ uri = URI.parse(url)
10
+
11
+ req = Net::HTTP::Post.new(uri)
12
+ req.set_form_data(params)
13
+
14
+ http = Net::HTTP.new(uri.host, uri.port)
15
+ http.use_ssl = true
16
+
17
+ http.request(req)
18
+ end
19
+ end
20
+
21
+ def initialize(webhook_url)
22
+ @webhook_url = webhook_url
23
+ @http_client = HTTPClient.new
24
+ end
25
+
26
+ attr_reader :webhook_url, :http_client
27
+
28
+ def handle_event(event)
29
+ event_name = event.event_type
30
+ payload = {
31
+ text: "Event #{event_name} raised.",
32
+ username: "Rails EventStore Bot",
33
+ }
34
+
35
+ @http_client.post(@webhook_url, {
36
+ payload: payload.to_json
37
+ })
38
+ end
39
+ end
40
+ end
41
+ end
@@ -7,10 +7,10 @@ class CreateEventsTable < ActiveRecord::Migration
7
7
  t.string :event_id, null: false
8
8
  t.text :metadata
9
9
  t.text :data, null: false
10
- t.timestamps
10
+ t.datetime :created_at, null: false
11
11
  end
12
12
  add_index :event_store_events, :stream
13
- add_index :event_store_events, :event_id
13
+ add_index :event_store_events, :event_id, unique: true
14
14
  end
15
15
 
16
16
  def self.down
@@ -0,0 +1,28 @@
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
@@ -2,8 +2,8 @@ module RailsEventStore
2
2
  module Repositories
3
3
  class EventRepository
4
4
 
5
- def initialize(adapter = EventEntity)
6
- @adapter = adapter
5
+ def initialize
6
+ @adapter = EventEntity
7
7
  end
8
8
  attr_reader :adapter
9
9
 
@@ -12,9 +12,11 @@ module RailsEventStore
12
12
  end
13
13
 
14
14
  def create(data)
15
- model = adapter.new(data)
16
- raise EventCannotBeSaved unless model.valid?
17
- model.save
15
+ begin
16
+ adapter.create(data)
17
+ rescue ActiveRecord::RecordNotUnique
18
+ raise EventCannotBeSaved
19
+ end
18
20
  end
19
21
 
20
22
  def delete(condition)
@@ -0,0 +1,14 @@
1
+ module StringUtils
2
+ def self.underscore(camel_cased_word)
3
+ acronym_regex = /(?=a)b/
4
+ return camel_cased_word unless camel_cased_word =~ /[A-Z-]|::/
5
+ word = camel_cased_word.to_s.gsub(/::/, '/')
6
+ word.gsub!(/(?:(?<=([A-Za-z\d]))|\b)(#{acronym_regex})(?=\b|[^a-z])/) { "#{$1 && '_'}#{$2.downcase}" }
7
+ word.gsub!(/([A-Z\d]+)([A-Z][a-z])/,'\1_\2')
8
+ word.gsub!(/([a-z\d])([A-Z])/,'\1_\2')
9
+ word.tr!("-", "_")
10
+ word.downcase!
11
+ word.gsub!(/[^A-Za-z0-9_]/, '_')
12
+ word
13
+ end
14
+ end
@@ -1,3 +1,3 @@
1
1
  module RailsEventStore
2
- VERSION = '0.1.1'
2
+ VERSION = '0.1.2'
3
3
  end
@@ -23,4 +23,7 @@ Gem::Specification.new do |spec|
23
23
  spec.add_development_dependency 'pry'
24
24
  spec.add_development_dependency 'rspec'
25
25
  spec.add_development_dependency 'rails', '~> 4.2.1'
26
+ spec.add_development_dependency 'webmock', '~> 1.21.0'
27
+ spec.add_dependency 'ruby_event_store'
28
+
26
29
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rails_event_store
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - rybex
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2015-04-22 00:00:00.000000000 Z
11
+ date: 2015-05-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -80,6 +80,34 @@ dependencies:
80
80
  - - "~>"
81
81
  - !ruby/object:Gem::Version
82
82
  version: 4.2.1
83
+ - !ruby/object:Gem::Dependency
84
+ name: webmock
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: 1.21.0
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: 1.21.0
97
+ - !ruby/object:Gem::Dependency
98
+ name: ruby_event_store
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :runtime
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
83
111
  description: Implementation of Event Store in Ruby
84
112
  email:
85
113
  - tomek.rybka@gmail.com
@@ -89,23 +117,24 @@ extra_rdoc_files: []
89
117
  files:
90
118
  - ".gitignore"
91
119
  - ".travis.yml"
120
+ - CHANGELOG.md
92
121
  - Gemfile
122
+ - LICENSE
93
123
  - README.md
94
124
  - Rakefile
95
125
  - lib/rails_event_store.rb
96
- - lib/rails_event_store/actions/append_event_to_stream.rb
97
- - lib/rails_event_store/actions/delete_stream_events.rb
98
- - lib/rails_event_store/actions/read_all_events.rb
99
- - lib/rails_event_store/actions/read_all_streams.rb
100
- - lib/rails_event_store/actions/read_events_batch.rb
126
+ - lib/rails_event_store/aggregate_root.rb
101
127
  - lib/rails_event_store/client.rb
128
+ - lib/rails_event_store/constants.rb
102
129
  - lib/rails_event_store/errors.rb
103
130
  - lib/rails_event_store/event.rb
104
131
  - lib/rails_event_store/event_entity.rb
132
+ - lib/rails_event_store/event_handlers/slack_event_handler.rb
105
133
  - lib/rails_event_store/generators/migrate_generator.rb
106
134
  - lib/rails_event_store/generators/templates/migration_template.rb
107
- - lib/rails_event_store/pub_sub/broker.rb
135
+ - lib/rails_event_store/repositories/aggregate_repository.rb
108
136
  - lib/rails_event_store/repositories/event_repository.rb
137
+ - lib/rails_event_store/utils/string_utils.rb
109
138
  - lib/rails_event_store/version.rb
110
139
  - rails_event_store.gemspec
111
140
  homepage: ''
@@ -1,34 +0,0 @@
1
- module RailsEventStore
2
- module Actions
3
- class AppendEventToStream
4
-
5
- def initialize(repository)
6
- @repository = repository
7
- end
8
-
9
- def call(stream_name, event, expected_version)
10
- raise WrongExpectedEventVersion if version_incorrect?(stream_name, expected_version)
11
- event.validate!
12
- save_event(event, stream_name)
13
- return event
14
- end
15
-
16
- private
17
- attr_reader :repository
18
-
19
- def version_incorrect?(stream_name, expected_version)
20
- unless expected_version.nil?
21
- find_last_event_version(stream_name) != expected_version
22
- end
23
- end
24
-
25
- def find_last_event_version(stream_name)
26
- repository.last_stream_event(stream_name).event_id
27
- end
28
-
29
- def save_event(event, stream_name)
30
- repository.create(event.to_h.merge!(stream: stream_name))
31
- end
32
- end
33
- end
34
- end
@@ -1,23 +0,0 @@
1
- module RailsEventStore
2
- module Actions
3
- class DeleteStreamEvents
4
-
5
- def initialize(repository)
6
- @repository = repository
7
- end
8
-
9
- def call(stream_name)
10
- raise IncorrectStreamData if stream_name.nil? || stream_name.empty?
11
- delete_stream(stream_name)
12
- end
13
-
14
- private
15
- attr_reader :repository
16
-
17
- def delete_stream(stream_name)
18
- repository.delete({stream: stream_name})
19
- end
20
- end
21
- end
22
- end
23
-
@@ -1,22 +0,0 @@
1
- module RailsEventStore
2
- module Actions
3
- class ReadAllEvents
4
-
5
- def initialize(repository)
6
- @repository = repository
7
- end
8
-
9
- def call(stream_name)
10
- raise IncorrectStreamData if stream_name.nil? || stream_name.empty?
11
- get_all_events(stream_name)
12
- end
13
-
14
- private
15
- attr_reader :repository
16
-
17
- def get_all_events(stream_name)
18
- repository.load_all_events_forward(stream_name)
19
- end
20
- end
21
- end
22
- end
@@ -1,21 +0,0 @@
1
- module RailsEventStore
2
- module Actions
3
- class ReadAllStreams
4
-
5
- def initialize(repository)
6
- @repository = repository
7
- end
8
-
9
- def call
10
- get_all_events.group_by { |event| event.stream }
11
- end
12
-
13
- private
14
- attr_reader :repository
15
-
16
- def get_all_events
17
- repository.get_all_events
18
- end
19
- end
20
- end
21
- end
@@ -1,29 +0,0 @@
1
- module RailsEventStore
2
- module Actions
3
- class ReadEventsBatch
4
-
5
- def initialize(repository)
6
- @repository = repository
7
- end
8
-
9
- def call(stream_name, start, count)
10
- raise IncorrectStreamData if stream_name.nil? || stream_name.empty?
11
- event = find_event(start)
12
- get_events_batch(stream_name, event.id, count)
13
- end
14
-
15
- private
16
- attr_reader :repository
17
-
18
- def get_events_batch(stream_name, start, count)
19
- repository.load_events_batch(stream_name, start, count)
20
- end
21
-
22
- def find_event(start)
23
- event = repository.find({event_id: start})
24
- raise EventNotFound if event.nil?
25
- event
26
- end
27
- end
28
- end
29
- end
@@ -1,33 +0,0 @@
1
- module RailsEventStore
2
- module PubSub
3
- class Broker
4
-
5
- def initialize
6
- @subscribers = {}
7
- end
8
-
9
- def add_subscriber(subscriber, event_types)
10
- raise SubscriberNotExist if subscriber.nil?
11
- raise MethodNotDefined unless subscriber.methods.include? :handle_event
12
- subscribe(subscriber, [*event_types])
13
- end
14
-
15
- def notify_subscribers(event)
16
- if subscribers.key? event.event_type
17
- subscribers[event.event_type].each do |subscriber|
18
- subscriber.handle_event(event)
19
- end
20
- end
21
- end
22
-
23
- private
24
- attr_reader :subscribers
25
-
26
- def subscribe(subscriber, event_types)
27
- event_types.each do |type|
28
- (subscribers[type] ||= []) << subscriber
29
- end
30
- end
31
- end
32
- end
33
- end