ruby_event_store 0.33.0 → 0.34.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: 72b0e4a52d4e09c87ef8b190fd2e310c306469d053b517c3d93facf2fee6a918
4
- data.tar.gz: ff0344153ae4e205e709da00b703b3a85605ac0c4c3aafb5b20439adce26a60e
3
+ metadata.gz: 212817e54f12a8231a77260d38f5c41980d0e0c4d465ac3fae5ef49fb5b5301d
4
+ data.tar.gz: c24731e1641aa7874d8417acda1550cd182df6d24c79f2f0906bdbb17b0e8e8f
5
5
  SHA512:
6
- metadata.gz: 1bd120b03290fa27864af3ab4aa7d07cbda4bf1786beb7523103e67d04d02ec2c3fdeb5af6fea9df304a296a658cff944aefd91790ea21839cbc213a53313b60
7
- data.tar.gz: 710cba6e537aa3378ad677d72d6f170a91fb2c617e8bdc583aa35f03ebe78d202f0c4c40b48e6a3c88728bbcefc422a7414335d8205c6813ebd87b86b12fb892
6
+ metadata.gz: d13612719037ba3a5e703f9eb34280968957056a5bd4f53c169817cdeec59c1965c984aeb322843f826e7baa875cd25690f7073900718a0ae2c86fd747d0718e
7
+ data.tar.gz: eb230b36b3103386bbe315765a165842d1bc287400f6543f8c9b0625a42c7e85e9cbb8583b4fd8897f3429e26bdb021243aac6954802155d8417e37fd8f57063
@@ -18,6 +18,14 @@ module RubyEventStore
18
18
  end
19
19
  end
20
20
 
21
+ def first
22
+ each.first
23
+ end
24
+
25
+ def to_a
26
+ each.to_a
27
+ end
28
+
21
29
  private
22
30
 
23
31
  attr_accessor :batch_size, :total_limit, :reader
@@ -255,7 +255,7 @@ module RubyEventStore
255
255
  #
256
256
  # @example Add data and metadata to existing events
257
257
  #
258
- # events = event_store.read.limit(10).each.to_a
258
+ # events = event_store.read.limit(10).to_a
259
259
  # events.each do |ev|
260
260
  # ev.data[:tenant_id] = 1
261
261
  # ev.metadata[:server_id] = "eu-west-2"
@@ -40,7 +40,7 @@ module RubyEventStore
40
40
  query << ".from(#{start})" if start
41
41
  query << ".limit(#{count})" if count
42
42
 
43
- replace(range, "#{query}.each.to_a")
43
+ replace(range, "#{query}.to_a")
44
44
  end
45
45
 
46
46
  def parse_args(args)
@@ -1,17 +1,19 @@
1
1
  module RubyEventStore
2
- WrongExpectedEventVersion = Class.new(StandardError)
3
- InvalidExpectedVersion = Class.new(StandardError)
4
- IncorrectStreamData = Class.new(StandardError)
5
- SubscriberNotExist = Class.new(StandardError)
6
- InvalidPageStart = Class.new(ArgumentError)
7
- InvalidPageSize = Class.new(ArgumentError)
8
- EventDuplicatedInStream = Class.new(StandardError)
9
- NotSupported = Class.new(StandardError)
10
- ReservedInternalName = Class.new(StandardError)
11
- InvalidHandler = Class.new(StandardError)
2
+ WrongExpectedEventVersion = Class.new(StandardError)
3
+ InvalidExpectedVersion = Class.new(StandardError)
4
+ IncorrectStreamData = Class.new(StandardError)
5
+ SubscriberNotExist = Class.new(StandardError)
6
+ InvalidPageStart = Class.new(ArgumentError)
7
+ InvalidPageSize = Class.new(ArgumentError)
8
+ EventDuplicatedInStream = Class.new(StandardError)
9
+ NotSupported = Class.new(StandardError)
10
+ ReservedInternalName = Class.new(StandardError)
11
+ InvalidHandler = Class.new(StandardError)
12
+ ProtobufEncodingFailed = Class.new(StandardError)
12
13
 
13
14
  class EventNotFound < StandardError
14
15
  attr_reader :event_id
16
+
15
17
  def initialize(event_id)
16
18
  super("Event not found: #{event_id}")
17
19
  @event_id = event_id
@@ -12,10 +12,10 @@ module RubyEventStore
12
12
  # part of your domain such as remote_ip, request_id, correlation_id,
13
13
  # causation_id etc.
14
14
  # @return [Event]
15
- def initialize(event_id: SecureRandom.uuid, metadata: nil, data: nil)
15
+ def initialize(event_id: SecureRandom.uuid, metadata: nil, data: {})
16
16
  @event_id = event_id.to_s
17
17
  @metadata = Metadata.new(metadata.to_h)
18
- @data = data.to_h
18
+ @data = data
19
19
  end
20
20
 
21
21
  attr_reader :event_id, :metadata, :data
@@ -34,6 +34,7 @@ module RubyEventStore
34
34
  def read(spec)
35
35
  events = spec.stream.global? ? global : stream_of(spec.stream.name)
36
36
  events = events.select{|e| spec.with_ids.any?{|x| x.eql?(e.event_id)}} if spec.with_ids?
37
+ events = events.select{|e| spec.with_types.any?{|x| x.eql?(e.event_type)}} if spec.with_types?
37
38
  events = events.reverse if spec.backward?
38
39
  events = events.drop(index_of(events, spec.start) + 1) unless spec.head?
39
40
  events = events[0...spec.limit] if spec.limit?
@@ -62,7 +63,7 @@ module RubyEventStore
62
63
 
63
64
  def streams_of(event_id)
64
65
  streams.select do |_, stream_events|
65
- stream_events.any? { |event| event.event_id.equal?(event_id) }
66
+ stream_events.any? { |event| event.event_id.eql?(event_id) }
66
67
  end.map { |name, _| Stream.new(name) }
67
68
  end
68
69
 
@@ -21,7 +21,7 @@ module RubyEventStore
21
21
  event_type = events_class_remapping.fetch(record.event_type) { record.event_type }
22
22
  Object.const_get(event_type).new(
23
23
  event_id: record.event_id,
24
- metadata: serializer.load(record.metadata),
24
+ metadata: symbolize(serializer.load(record.metadata)),
25
25
  data: serializer.load(record.data)
26
26
  )
27
27
  end
@@ -30,6 +30,11 @@ module RubyEventStore
30
30
 
31
31
  attr_reader :serializer, :events_class_remapping
32
32
 
33
+ def symbolize(hash)
34
+ hash.each_with_object({}) do |(k, v), memo|
35
+ memo[k.to_sym] = v
36
+ end
37
+ end
33
38
  end
34
39
  end
35
40
  end
@@ -1,11 +1,5 @@
1
1
  module RubyEventStore
2
2
  class Proto < RubyEventStore::Event
3
- def initialize(event_id: SecureRandom.uuid, metadata: nil, data:)
4
- @event_id = event_id
5
- @metadata = Metadata.new(metadata.to_h)
6
- @data = data
7
- end
8
-
9
3
  def type
10
4
  data.class.descriptor.name
11
5
  end
@@ -47,8 +41,8 @@ module RubyEventStore
47
41
  def event_to_serialized_record(domain_event)
48
42
  SerializedRecord.new(
49
43
  event_id: domain_event.event_id,
50
- metadata: ProtobufNestedStruct::HashMapStringValue.dump(domain_event.metadata.each_with_object({}){|(k,v),h| h[k.to_s] =v }),
51
- data: domain_event.data.class.encode(domain_event.data),
44
+ metadata: ProtobufNestedStruct::HashMapStringValue.dump(domain_event.metadata.each_with_object({}) {|(k, v), h| h[k.to_s] = v}),
45
+ data: encode_data(domain_event.data),
52
46
  event_type: domain_event.type
53
47
  )
54
48
  end
@@ -76,6 +70,14 @@ module RubyEventStore
76
70
  Google::Protobuf::DescriptorPool.generated_pool.lookup(event_type).msgclass.decode(protobuf_data)
77
71
  end
78
72
 
73
+ def encode_data(domain_event_data)
74
+ begin
75
+ domain_event_data.class.encode(domain_event_data)
76
+ rescue NoMethodError
77
+ raise ProtobufEncodingFailed
78
+ end
79
+ end
80
+
79
81
  def require_optional_dependency
80
82
  require 'protobuf_nested_struct'
81
83
  rescue LoadError
@@ -15,6 +15,13 @@ module RubyEventStore
15
15
  )
16
16
  end
17
17
  end
18
+
19
+ # @private
20
+ Type1 = Class.new(RubyEventStore::Event)
21
+ # @private
22
+ Type2 = Class.new(RubyEventStore::Event)
23
+ # @private
24
+ Type3 = Class.new(RubyEventStore::Event)
18
25
  end
19
26
 
20
27
  module RubyEventStore
@@ -983,6 +990,17 @@ module RubyEventStore
983
990
  )
984
991
  end
985
992
 
993
+ specify do
994
+ expect(repository.read(specification.in_batches.result)).to be_kind_of(Enumerator)
995
+ events = Array.new(10) { SRecord.new }
996
+ repository.append_to_stream(
997
+ events,
998
+ Stream.new("Dummy"),
999
+ ExpectedVersion.none
1000
+ )
1001
+ expect(repository.read(specification.in_batches.result)).to be_kind_of(Enumerator)
1002
+ end
1003
+
986
1004
  specify do
987
1005
  events = Array.new(400) { SRecord.new }
988
1006
  repository.append_to_stream(
@@ -1157,10 +1175,9 @@ module RubyEventStore
1157
1175
 
1158
1176
  specify do
1159
1177
  skip unless test_link_events_to_stream
1160
- event_1 = SRecord.new
1161
- event_2 = SRecord.new
1162
- event_3 = SRecord.new
1163
- event_4 = SRecord.new
1178
+ event_1 = SRecord.new(event_id: '8a6f053e-3ce2-4c82-a55b-4d02c66ae6ea')
1179
+ event_2 = SRecord.new(event_id: '8cee1139-4f96-483a-a175-2b947283c3c7')
1180
+ event_3 = SRecord.new(event_id: 'd345f86d-b903-4d78-803f-38990c078d9e')
1164
1181
  stream_a = Stream.new('Stream A')
1165
1182
  stream_b = Stream.new('Stream B')
1166
1183
  stream_c = Stream.new('Stream C')
@@ -1168,10 +1185,10 @@ module RubyEventStore
1168
1185
  repository.append_to_stream([event_3], stream_b, version_any)
1169
1186
  repository.link_to_stream(event_1.event_id, stream_c, version_none)
1170
1187
 
1171
- expect(repository.streams_of(event_1.event_id)).to eq [stream_a, stream_c]
1172
- expect(repository.streams_of(event_2.event_id)).to eq [stream_a]
1173
- expect(repository.streams_of(event_3.event_id)).to eq [stream_b]
1174
- expect(repository.streams_of(event_4.event_id)).to eq []
1188
+ expect(repository.streams_of('8a6f053e-3ce2-4c82-a55b-4d02c66ae6ea')).to eq [stream_a, stream_c]
1189
+ expect(repository.streams_of('8cee1139-4f96-483a-a175-2b947283c3c7')).to eq [stream_a]
1190
+ expect(repository.streams_of('d345f86d-b903-4d78-803f-38990c078d9e')).to eq [stream_b]
1191
+ expect(repository.streams_of('d10c8fe9-2163-418d-ba47-88c9a1f9391b')).to eq []
1175
1192
  end
1176
1193
 
1177
1194
  specify do
@@ -1194,6 +1211,20 @@ module RubyEventStore
1194
1211
  '8a6f053e-3ce2-4c82-a55b-4d02c66ae6ea',
1195
1212
  'd345f86d-b903-4d78-803f-38990c078d9e'
1196
1213
  ]).in_batches.result).to_a[0]).to eq([e1,e3])
1214
+ expect(repository.read(specification.with_id([]).result).to_a).to eq([])
1215
+ end
1216
+
1217
+ specify do
1218
+ e1 = SRecord.new(event_type: Type1.to_s)
1219
+ e2 = SRecord.new(event_type: Type2.to_s)
1220
+ e3 = SRecord.new(event_type: Type1.to_s)
1221
+ stream = Stream.new('Stream A')
1222
+ repository.append_to_stream([e1, e2, e3], stream, version_any)
1223
+
1224
+ expect(repository.read(specification.of_type([Type1]).result).to_a).to eq([e1,e3])
1225
+ expect(repository.read(specification.of_type([Type2]).result).to_a).to eq([e2])
1226
+ expect(repository.read(specification.of_type([Type3]).result).to_a).to eq([])
1227
+ expect(repository.read(specification.of_type([Type1, Type2, Type3]).result).to_a).to eq([e1,e2,e3])
1197
1228
  end
1198
1229
  end
1199
1230
  end
@@ -91,6 +91,15 @@ module RubyEventStore
91
91
  end
92
92
  end
93
93
 
94
+ # Executes the query based on the specification built up to this point.
95
+ # Returns array of domain events.
96
+ # {http://railseventstore.org/docs/read/ Find out more}.
97
+ #
98
+ # @return [Array<Event, Proto>]
99
+ def to_a
100
+ each.to_a
101
+ end
102
+
94
103
  # Specifies that events should be obtained in batches.
95
104
  # {http://railseventstore.org/docs/read/ Find out more}.
96
105
  #
@@ -143,6 +152,15 @@ module RubyEventStore
143
152
  reader.one(read_last.result)
144
153
  end
145
154
 
155
+ # Limits the query to certain event types.
156
+ # {http://railseventstore.org/docs/read/ Find out more}.
157
+ #
158
+ # @types [Array(Class)] types of event to look for.
159
+ # @return [Specification]
160
+ def of_type(types)
161
+ Specification.new(reader, result.dup{ |r| r.with_types = types })
162
+ end
163
+
146
164
  # Limits the query to certain events by given even ids.
147
165
  # {http://railseventstore.org/docs/read/ Find out more}.
148
166
  #
@@ -152,7 +170,7 @@ module RubyEventStore
152
170
  Specification.new(reader, result.dup{ |r| r.with_ids = event_ids })
153
171
  end
154
172
 
155
- # Executes the query based on the specification built up to this point.
173
+ # Reads single event from repository.
156
174
  # Returns the event with specified id or nil if event is not found
157
175
  # in specified collection of events.
158
176
  # {http://railseventstore.org/docs/read/ Find out more}.
@@ -162,8 +180,8 @@ module RubyEventStore
162
180
  reader.one(read_first.with_id([event_id]).result)
163
181
  end
164
182
 
165
- # Executes the query based on the specification built up to this point.
166
- # Returns the event with specified id or raises [EventNotFound[ error if
183
+ # Reads single existing event from repository.
184
+ # Returns the event with specified id or raises [EventNotFound] error if
167
185
  # event is not found in specified collection of events.
168
186
  # {http://railseventstore.org/docs/read/ Find out more}.
169
187
  #
@@ -172,13 +190,13 @@ module RubyEventStore
172
190
  event(event_id) or raise EventNotFound.new(event_id)
173
191
  end
174
192
 
175
- # Executes the query based on the specification built up to this point.
193
+ # Reads all events of given ids from repository.
176
194
  # Yields each event (found by id in specified collection of events)
177
195
  # read from the store if block given. Otherwise, returns enumerable collection.
178
196
  # {http://railseventstore.org/docs/read/ Find out more}.
179
197
  #
180
198
  # @yield [Event, Proto] event
181
- # @return [Enumerator, nil] Enumerator is returned when block not given
199
+ # @return [Enumerator] Enumerator is returned when block not given
182
200
  def events(event_ids)
183
201
  with_id(event_ids).each
184
202
  end
@@ -6,9 +6,10 @@ module RubyEventStore
6
6
  stream: Stream.new(GLOBAL_STREAM),
7
7
  read_as: :all,
8
8
  batch_size: Specification::DEFAULT_BATCH_SIZE,
9
- with_ids: nil)
10
- @attributes = Struct.new(:direction, :start, :count, :stream, :read_as, :batch_size, :with_ids)
11
- .new(direction, start, count, stream, read_as, batch_size, with_ids)
9
+ with_ids: nil,
10
+ with_types: nil)
11
+ @attributes = Struct.new(:direction, :start, :count, :stream, :read_as, :batch_size, :with_ids, :with_types)
12
+ .new(direction, start, count, stream, read_as, batch_size, with_ids, with_types)
12
13
  freeze
13
14
  end
14
15
 
@@ -89,7 +90,23 @@ module RubyEventStore
89
90
  #
90
91
  # @return [Boolean]
91
92
  def with_ids?
92
- !(with_ids || []).empty?
93
+ !with_ids.nil?
94
+ end
95
+
96
+ # Event types to be read (if any given)
97
+ # {http://railseventstore.org/docs/read/ Find out more}.
98
+ #
99
+ # @return [Array|nil]
100
+ def with_types
101
+ attributes.with_types&.map(&:to_s)
102
+ end
103
+
104
+ # Read by specified event types. True if event types have been specified.
105
+ # {http://railseventstore.org/docs/read/ Find out more}.
106
+ #
107
+ # @return [Boolean]
108
+ def with_types?
109
+ !(with_types || []).empty?
93
110
  end
94
111
 
95
112
  # Read strategy. True if items will be read in batches
@@ -163,6 +180,7 @@ module RubyEventStore
163
180
  # * read_as
164
181
  # * batch_size
165
182
  # * with_ids
183
+ # * with_types
166
184
  #
167
185
  # @return [Integer]
168
186
  def hash
@@ -175,6 +193,7 @@ module RubyEventStore
175
193
  attributes.read_as,
176
194
  batch_size,
177
195
  with_ids,
196
+ with_types,
178
197
  ].hash ^ BIG_VALUE
179
198
  end
180
199
 
@@ -1,3 +1,3 @@
1
1
  module RubyEventStore
2
- VERSION = "0.33.0"
2
+ VERSION = "0.34.0"
3
3
  end
@@ -30,7 +30,7 @@ Gem::Specification.new do |spec|
30
30
  spec.add_development_dependency 'bundler', '~> 1.15'
31
31
  spec.add_development_dependency 'rake', '~> 10.0'
32
32
  spec.add_development_dependency 'rspec', '~> 3.6'
33
- spec.add_development_dependency 'mutant-rspec', '~> 0.8.17'
33
+ spec.add_development_dependency 'mutant-rspec', '= 0.8.17'
34
34
  spec.add_development_dependency 'parser'
35
35
  spec.add_development_dependency 'unparser'
36
36
  spec.add_development_dependency 'astrolabe'
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.33.0
4
+ version: 0.34.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Arkency
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2018-10-12 00:00:00.000000000 Z
11
+ date: 2018-11-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: concurrent-ruby
@@ -70,14 +70,14 @@ dependencies:
70
70
  name: mutant-rspec
71
71
  requirement: !ruby/object:Gem::Requirement
72
72
  requirements:
73
- - - "~>"
73
+ - - '='
74
74
  - !ruby/object:Gem::Version
75
75
  version: 0.8.17
76
76
  type: :development
77
77
  prerelease: false
78
78
  version_requirements: !ruby/object:Gem::Requirement
79
79
  requirements:
80
- - - "~>"
80
+ - - '='
81
81
  - !ruby/object:Gem::Version
82
82
  version: 0.8.17
83
83
  - !ruby/object:Gem::Dependency
@@ -225,7 +225,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
225
225
  version: '0'
226
226
  requirements: []
227
227
  rubyforge_project:
228
- rubygems_version: 3.0.0.beta1
228
+ rubygems_version: 2.7.6
229
229
  signing_key:
230
230
  specification_version: 4
231
231
  summary: Event Store in Ruby