ruby_event_store 0.43.0 → 1.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: 5dec78069a189ba48a3eacb8c3ff06629e9c9067ea21f23480dbb521394fbaeb
4
- data.tar.gz: d4d466bf64f7402b5f6341228628cebbd73c1a42aecab7e51362c8a6b4f38bb8
3
+ metadata.gz: 6b30f1a52e6c803267914de53968b2885b7262c96f86e8399749ffcf189ad967
4
+ data.tar.gz: 9aa5d056ace06dec409e292e5562b3c6b177cdc4fceec2b98f1f936807b403cf
5
5
  SHA512:
6
- metadata.gz: 80b4dfbda828a29dfe5cdc0f2606eddc10588822869f6bbd4a880f4407c8d422341ed7e57f639d04c417c15e1ba163343d1bfe51a0f61c23c59aa066443586dc
7
- data.tar.gz: 265efe01bb2bb7bf2a3c6a4b68affd55b28aa04392223884359e0164b9a798b7707cb232291ce756877c51be71e36b1ccbca727140c390863697770e989cfa9f
6
+ metadata.gz: f953e8453922bbc437efafab4716533172d1e1c4c14d0c9ed3c426b569ef4b415edb9095ec95fa47ca70dd92412ab57489111f8d22e216a20033fc838b2bf4e9
7
+ data.tar.gz: b5ff8f63294fe88bcfe982ec494989c30f1748926990a68310a629b8eaa3c15b995c5ec015c373512c6f1ff3a6f46773d3f1c0f3a4a00b219fdb744f624c6ede
data/Gemfile CHANGED
@@ -6,5 +6,5 @@ gemspec
6
6
  eval_gemfile File.expand_path('../support/bundler/Gemfile.shared', __dir__)
7
7
 
8
8
  gem 'protobuf_nested_struct'
9
- gem 'google-protobuf', '~> 3.7.0'
9
+ gem 'google-protobuf', '~> 3.12.2', '>= 3.12.2'
10
10
  gem 'activesupport', '~> 5.0'
data/Makefile CHANGED
@@ -5,6 +5,7 @@ IGNORE = RubyEventStore::InMemoryRepository\#append_with_synchronize \
5
5
  RubyEventStore::Client::Within\#add_thread_subscribers \
6
6
  RubyEventStore::Client::Within\#add_thread_global_subscribers \
7
7
  RubyEventStore::Client::Within\#call \
8
+ RubyEventStore::Client\#default_correlation_id_generator \
8
9
  RubyEventStore::Mappers::InMemoryEncryptionKeyRepository\#prepare_encrypt \
9
10
  RubyEventStore::Mappers::EncryptionKey\#prepare_encrypt \
10
11
  RubyEventStore::Mappers::EncryptionKey\#prepare_decrypt \
@@ -10,9 +10,10 @@ module RubyEventStore
10
10
 
11
11
  def each
12
12
  return to_enum unless block_given?
13
+
13
14
  0.step(total_limit - 1, batch_size) do |batch_offset|
14
- batch_limit = [batch_size, total_limit - batch_offset].min
15
- result = reader.call(batch_offset, batch_limit)
15
+ batch_limit = [batch_size, total_limit - batch_offset].min
16
+ result = reader.call(batch_offset, batch_limit)
16
17
 
17
18
  break if result.empty?
18
19
  yield result
@@ -29,6 +30,6 @@ module RubyEventStore
29
30
 
30
31
  private
31
32
 
32
- attr_accessor :batch_size, :total_limit, :reader
33
+ attr_reader :batch_size, :total_limit, :reader
33
34
  end
34
35
  end
@@ -8,7 +8,7 @@ module RubyEventStore
8
8
  end
9
9
 
10
10
  def call(event, serialized_event)
11
- subscribers = subscriptions.all_for(event.type)
11
+ subscribers = subscriptions.all_for(event.event_type)
12
12
  subscribers.each do |subscriber|
13
13
  dispatcher.call(subscriber, event, serialized_event)
14
14
  end
@@ -4,17 +4,18 @@ require 'concurrent'
4
4
 
5
5
  module RubyEventStore
6
6
  class Client
7
-
8
7
  def initialize(repository:,
9
8
  mapper: Mappers::Default.new,
10
9
  subscriptions: Subscriptions.new,
11
10
  dispatcher: Dispatcher.new,
12
- clock: ->{ Time.now.utc })
11
+ clock: default_clock,
12
+ correlation_id_generator: default_correlation_id_generator)
13
13
  @repository = repository
14
14
  @mapper = mapper
15
15
  @broker = Broker.new(subscriptions: subscriptions, dispatcher: dispatcher)
16
16
  @clock = clock
17
17
  @metadata = Concurrent::ThreadLocalVar.new
18
+ @correlation_id_generator = correlation_id_generator
18
19
  end
19
20
 
20
21
 
@@ -30,7 +31,7 @@ module RubyEventStore
30
31
  append_to_stream_serialized_events(serialized_events, stream_name: stream_name, expected_version: expected_version)
31
32
  enriched_events.zip(serialized_events) do |event, serialized_event|
32
33
  with_metadata(
33
- correlation_id: event.metadata[:correlation_id] || event.event_id,
34
+ correlation_id: event.metadata.fetch(:correlation_id),
34
35
  causation_id: event.event_id,
35
36
  ) do
36
37
  broker.(event, serialized_event)
@@ -293,7 +294,8 @@ module RubyEventStore
293
294
 
294
295
  def enrich_event_metadata(event)
295
296
  metadata.each { |key, value| event.metadata[key] ||= value }
296
- event.metadata[:timestamp] ||= clock.call
297
+ event.metadata[:timestamp] ||= clock.call
298
+ event.metadata[:correlation_id] ||= correlation_id_generator.call
297
299
  end
298
300
 
299
301
  def append_to_stream_serialized_events(serialized_events, stream_name:, expected_version:)
@@ -306,6 +308,14 @@ module RubyEventStore
306
308
  @metadata.value = value
307
309
  end
308
310
 
309
- attr_reader :repository, :mapper, :broker, :clock
311
+ def default_clock
312
+ ->{ Time.now.utc }
313
+ end
314
+
315
+ def default_correlation_id_generator
316
+ ->{ SecureRandom.uuid }
317
+ end
318
+
319
+ attr_reader :repository, :mapper, :broker, :clock, :correlation_id_generator
310
320
  end
311
321
  end
@@ -30,24 +30,10 @@ module RubyEventStore
30
30
 
31
31
  # Type of event. Used when matching with subscribed handlers.
32
32
  # @return [String]
33
- def type
33
+ def event_type
34
34
  self.class.name
35
35
  end
36
36
 
37
- # Returns a hash representation of the event.
38
- #
39
- # Metadata is converted to hash as well
40
- #
41
- # @return [Hash] with :event_id, :metadata, :data, :type keys
42
- def to_h
43
- {
44
- event_id: event_id,
45
- metadata: metadata.to_h,
46
- data: data,
47
- type: type,
48
- }
49
- end
50
-
51
37
  # Timestamp from metadata
52
38
  #
53
39
  # @return [Time, nil]
@@ -140,4 +126,4 @@ module RubyEventStore
140
126
 
141
127
  alias_method :eql?, :==
142
128
  end
143
- end
129
+ end
@@ -13,7 +13,7 @@ module RubyEventStore
13
13
  return unless event.metadata.has_key?(@key)
14
14
 
15
15
  @event_store.link(
16
- [event.message_id],
16
+ [event.event_id],
17
17
  stream_name: "#{@prefix}#{event.metadata.fetch(@key)}"
18
18
  )
19
19
  end
@@ -48,10 +48,10 @@ module RubyEventStore
48
48
 
49
49
  def call(event)
50
50
  @event_store.link(
51
- [event.message_id],
52
- stream_name: "#{@prefix}#{event.type}"
51
+ [event.event_id],
52
+ stream_name: "#{@prefix}#{event.event_type}"
53
53
  )
54
54
  end
55
55
  end
56
56
 
57
- end
57
+ end
@@ -2,15 +2,9 @@
2
2
 
3
3
  module RubyEventStore
4
4
  class Proto < RubyEventStore::Event
5
- def type
5
+ def event_type
6
6
  data.class.descriptor.name
7
7
  end
8
-
9
- def ==(other_event)
10
- other_event.instance_of?(self.class) &&
11
- other_event.event_id.eql?(event_id) &&
12
- other_event.data == data # https://github.com/google/protobuf/issues/4455
13
- end
14
8
  end
15
9
 
16
10
  module Mappers
@@ -9,7 +9,7 @@ module RubyEventStore
9
9
  event_id: domain_event.event_id,
10
10
  metadata: domain_event.metadata.to_h,
11
11
  data: domain_event.data,
12
- event_type: domain_event.type
12
+ event_type: domain_event.event_type
13
13
  )
14
14
  end
15
15
 
@@ -82,14 +82,14 @@ module RubyEventStore
82
82
 
83
83
  def encrypt_data(data, meta)
84
84
  meta.reduce(data) do |acc, (key, value)|
85
- acc[key] = encrypt_attribute(acc, key, value)
85
+ acc[key] = encrypt_attribute(acc, key, value) if data.has_key?(key)
86
86
  acc
87
87
  end
88
88
  end
89
89
 
90
90
  def decrypt_data(data, meta)
91
91
  meta.reduce(data) do |acc, (key, value)|
92
- acc[key] = decrypt_attribute(data, key, value)
92
+ acc[key] = decrypt_attribute(data, key, value) if data.has_key?(key)
93
93
  acc
94
94
  end
95
95
  end
@@ -98,7 +98,7 @@ module RubyEventStore
98
98
  case meta
99
99
  when Leaf
100
100
  value = data.fetch(attribute)
101
- return unless value
101
+ return if value.nil?
102
102
 
103
103
  encryption_key = key_repository.key_of(meta.fetch(:identifier))
104
104
  encryption_key.encrypt(serializer.dump(value), meta.fetch(:iv))
@@ -9,8 +9,7 @@ module RubyEventStore
9
9
  end
10
10
 
11
11
  def dump(item)
12
- stringify = StringifyMetadataKeys.new
13
- metadata = ProtobufNestedStruct::HashMapStringValue.dump(stringify.dump(item).metadata)
12
+ metadata = ProtobufNestedStruct::HashMapStringValue.dump(item.metadata)
14
13
  item.merge(metadata: metadata)
15
14
  end
16
15
 
@@ -15,8 +15,8 @@ module RubyEventStore
15
15
 
16
16
  def initialize(streams: [])
17
17
  @streams = streams
18
- @handlers = Hash.new { ->(_, _) {} }
19
- @init = -> { Hash.new }
18
+ @handlers = {}
19
+ @init = -> { {} }
20
20
  end
21
21
 
22
22
  attr_reader :streams, :handlers
@@ -43,7 +43,7 @@ module RubyEventStore
43
43
  end
44
44
 
45
45
  def call(event)
46
- handlers.fetch(event.type).(current_state, event)
46
+ handlers.fetch(event.event_type).(current_state, event)
47
47
  end
48
48
 
49
49
  def handled_events
@@ -51,6 +51,7 @@ module RubyEventStore
51
51
  end
52
52
 
53
53
  def run(event_store, start: nil, count: PAGE_SIZE)
54
+ return initial_state if handled_events.empty?
54
55
  if streams.any?
55
56
  reduce_from_streams(event_store, start, count)
56
57
  else
@@ -83,6 +84,7 @@ module RubyEventStore
83
84
 
84
85
  def read_scope(event_store, stream, count, start)
85
86
  scope = event_store.read.in_batches(count)
87
+ scope = scope.of_type(handled_events)
86
88
  scope = scope.stream(stream) if stream
87
89
  scope = scope.from(start) if start
88
90
  scope
@@ -93,7 +95,7 @@ module RubyEventStore
93
95
  end
94
96
 
95
97
  def transition(state, event)
96
- handlers[event.type].(state, event)
98
+ handlers.fetch(event.event_type).call(state, event)
97
99
  state
98
100
  end
99
101
  end
@@ -1,5 +1,5 @@
1
1
  RSpec.shared_examples :broker do |broker_klass|
2
- let(:event) { instance_double(::RubyEventStore::Event, type: 'EventType') }
2
+ let(:event) { instance_double(::RubyEventStore::Event, event_type: 'EventType') }
3
3
  let(:serialized_event) { instance_double(::RubyEventStore::SerializedRecord) }
4
4
  let(:handler) { HandlerClass.new }
5
5
  let(:subscriptions) { ::RubyEventStore::Subscriptions.new }
@@ -1,9 +1,7 @@
1
- RSpec.shared_examples :event do |event_class|
1
+ RSpec.shared_examples :event do |event_class, data, metadata|
2
2
  it 'allows initialization' do
3
3
  expect {
4
- metadata = double(:metadata)
5
- allow(metadata).to receive(:to_h).and_return({})
6
- event_class.new(event_id: Object.new, data: Object.new, metadata: metadata)
4
+ event_class.new(event_id: Object.new, data: data || Object.new, metadata: metadata || {})
7
5
  }.not_to raise_error
8
6
  end
9
7
 
@@ -29,11 +27,11 @@ RSpec.shared_examples :event do |event_class|
29
27
  expect(event.event_id).to eq '1234567890'
30
28
  end
31
29
 
32
- it 'provides type as string' do
30
+ it 'provides event type as string' do
33
31
  event = event_class.new
34
- expect(event.type).to be_an_instance_of(String)
35
- expect(event.type).not_to eq ''
36
- expect(event.type).not_to eq nil
32
+ expect(event.event_type).to be_an_instance_of(String)
33
+ expect(event.event_type).not_to eq ''
34
+ expect(event.event_type).not_to eq nil
37
35
  end
38
36
 
39
37
  it "provides data" do
@@ -5,7 +5,7 @@ module RubyEventStore
5
5
 
6
6
  expect(record).to be_kind_of(SerializedRecord)
7
7
  expect(record.event_id).to eq(domain_event.event_id)
8
- expect(record.event_type).to eq(domain_event.type)
8
+ expect(record.event_type).to eq(domain_event.event_type)
9
9
  end
10
10
 
11
11
  specify "serialize and deserialize gives equal event" do
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module RubyEventStore
4
- VERSION = "0.43.0"
4
+ VERSION = "1.2.0"
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ruby_event_store
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.43.0
4
+ version: 1.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Arkency
8
- autorequire:
8
+ autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2020-02-26 00:00:00.000000000 Z
11
+ date: 2020-10-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: concurrent-ruby
@@ -105,7 +105,7 @@ metadata:
105
105
  changelog_uri: https://github.com/RailsEventStore/rails_event_store/releases
106
106
  source_code_uri: https://github.com/RailsEventStore/rails_event_store
107
107
  bug_tracker_uri: https://github.com/RailsEventStore/rails_event_store/issues
108
- post_install_message:
108
+ post_install_message:
109
109
  rdoc_options: []
110
110
  require_paths:
111
111
  - lib
@@ -120,8 +120,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
120
120
  - !ruby/object:Gem::Version
121
121
  version: '0'
122
122
  requirements: []
123
- rubygems_version: 3.0.3
124
- signing_key:
123
+ rubygems_version: 3.1.4
124
+ signing_key:
125
125
  specification_version: 4
126
126
  summary: Event Store in Ruby
127
127
  test_files: []