ruby_event_store 3.0.1 → 3.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: d8351508cfb25ee4e763b68e8ccdaecbf8b8ecb7209bd0063ed49fa05cc6b4fa
4
- data.tar.gz: 76a337cc5a687292f9dd0ed89c60f6bac9a38071edcd775105606300422114e6
3
+ metadata.gz: 122d18b13759b12a2916e695e0f846a185912493275718bd7d8b1f665cdf6182
4
+ data.tar.gz: d744d6882572eeff995fe60d21a0e6ee1dc7bd486c4c9bd584b7f461c38c12c3
5
5
  SHA512:
6
- metadata.gz: f8e16fca54e179cd4f5f458040338930a25566d0cd3b26a7456aec9367ec17d4894d717c0f2c0fe9f3314a1f8f7a40f757975786386780ab2112945db79887ca
7
- data.tar.gz: fee4fc62bbae87d3421c6b35865872fedde255ce3115ae49dc96e9c63f2de4f7c9a28dcae0c0a4cd864a5325d9366e6058efec3de988adfa36a3ad8ab3d125c8
6
+ metadata.gz: 73529e99d9af66ee1c4bfba654854e48a7d381789fc12784e7f859bcfb509c66b3004c13cf3607a61871799d3b393683576eb28690829110e01935b9e422f071
7
+ data.tar.gz: b2d083b7458c6bf4b0b3dbc5800792b619028f679d7b3490f9c45d3941ec116a97410e9078f45acdc023fc486d85d81b27980ac2296667de428477bdfd0c9f6a
@@ -14,6 +14,8 @@ module RubyEventStore
14
14
  InvalidHandler = Class.new(Error)
15
15
  EventNotFoundInStream = Class.new(Error)
16
16
  SubscriptionsNotSupported = Class.new(Error)
17
+ StringsRequired = Class.new(Error)
18
+ InvalidUpcast = Class.new(Error)
17
19
 
18
20
  class EventNotFound < Error
19
21
  attr_reader :event_id
@@ -148,7 +148,6 @@ module RubyEventStore
148
148
  serialized_records = serialized_records.reverse if spec.backward?
149
149
  serialized_records = serialized_records.drop(index_of(serialized_records, spec.start) + 1) if spec.start
150
150
  serialized_records = serialized_records.take(index_of(serialized_records, spec.stop)) if spec.stop
151
- serialized_records = serialized_records.take(spec.limit) if spec.limit?
152
151
  serialized_records =
153
152
  serialized_records.select do |sr|
154
153
  Time.iso8601(time_comparison_field(spec, sr)) < spec.older_than
@@ -165,6 +164,7 @@ module RubyEventStore
165
164
  serialized_records.select do |sr|
166
165
  Time.iso8601(time_comparison_field(spec, sr)) >= spec.newer_than_or_equal
167
166
  end if spec.newer_than_or_equal
167
+ serialized_records = serialized_records.take(spec.limit) if spec.limit?
168
168
  serialized_records
169
169
  end
170
170
 
@@ -12,7 +12,12 @@ module RubyEventStore
12
12
  def call(record)
13
13
  identity = lambda { |r| r }
14
14
  new_record = @upcast_map.fetch(record.event_type, identity)[record]
15
- new_record.equal?(record) ? record : call(new_record)
15
+ return record if new_record.equal?(record)
16
+ if new_record.event_type == record.event_type
17
+ raise InvalidUpcast,
18
+ "Upcast for '#{record.event_type}' returned the same event_type. In order to upcast provide a new event_type (e.g. '#{record.event_type}_v2')."
19
+ end
20
+ call(new_record)
16
21
  end
17
22
  end
18
23
 
@@ -1,41 +1,11 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module RubyEventStore
4
- class Record
5
- StringsRequired = Class.new(StandardError)
4
+ Record = Data.define(:event_id, :data, :metadata, :event_type, :timestamp, :valid_at) do
6
5
  def initialize(event_id:, data:, metadata:, event_type:, timestamp:, valid_at:)
7
6
  raise StringsRequired unless [event_id, event_type].all? { |v| v.instance_of?(String) }
8
- @event_id = event_id
9
- @data = data
10
- @metadata = metadata
11
- @event_type = event_type
12
- @timestamp = timestamp
13
- @valid_at = valid_at
14
7
  @serialized_records = {}
15
- freeze
16
- end
17
-
18
- attr_reader :event_id, :data, :metadata, :event_type, :timestamp, :valid_at
19
-
20
- def hash
21
- [event_id, data, metadata, event_type, timestamp, valid_at].hash ^ self.class.hash
22
- end
23
-
24
- def ==(other)
25
- other.instance_of?(self.class) && other.event_id.eql?(event_id) && other.data.eql?(data) &&
26
- other.metadata.eql?(metadata) && other.event_type.eql?(event_type) && other.timestamp.eql?(timestamp) &&
27
- other.valid_at.eql?(valid_at)
28
- end
29
-
30
- def to_h
31
- {
32
- event_id: event_id,
33
- data: data,
34
- metadata: metadata,
35
- event_type: event_type,
36
- timestamp: timestamp,
37
- valid_at: valid_at,
38
- }
8
+ super
39
9
  end
40
10
 
41
11
  def serialize(serializer)
@@ -48,7 +18,6 @@ module RubyEventStore
48
18
  valid_at: valid_at.iso8601(TIMESTAMP_PRECISION),
49
19
  )
50
20
  end
51
-
52
- alias_method :eql?, :==
53
21
  end
22
+ Record::StringsRequired = StringsRequired
54
23
  end
@@ -1,40 +1,10 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module RubyEventStore
4
- class SerializedRecord
5
- StringsRequired = Class.new(StandardError)
4
+ SerializedRecord = Data.define(:event_id, :data, :metadata, :event_type, :timestamp, :valid_at) do
6
5
  def initialize(event_id:, data:, metadata:, event_type:, timestamp:, valid_at:)
7
6
  raise StringsRequired unless [event_id, event_type].all? { |v| v.instance_of?(String) }
8
- @event_id = event_id
9
- @data = data
10
- @metadata = metadata
11
- @event_type = event_type
12
- @timestamp = timestamp
13
- @valid_at = valid_at
14
- freeze
15
- end
16
-
17
- attr_reader :event_id, :data, :metadata, :event_type, :timestamp, :valid_at
18
-
19
- def hash
20
- [event_id, data, metadata, event_type, timestamp, valid_at].hash ^ self.class.hash
21
- end
22
-
23
- def ==(other)
24
- other.instance_of?(self.class) && other.event_id.eql?(event_id) && other.data.eql?(data) &&
25
- other.metadata.eql?(metadata) && other.event_type.eql?(event_type) && other.timestamp.eql?(timestamp) &&
26
- other.valid_at.eql?(valid_at)
27
- end
28
-
29
- def to_h
30
- {
31
- event_id: event_id,
32
- data: data,
33
- metadata: metadata,
34
- event_type: event_type,
35
- timestamp: timestamp,
36
- valid_at: valid_at,
37
- }
7
+ super
38
8
  end
39
9
 
40
10
  def deserialize(serializer)
@@ -47,7 +17,6 @@ module RubyEventStore
47
17
  valid_at: Time.iso8601(valid_at),
48
18
  )
49
19
  end
50
-
51
- alias_method :eql?, :==
52
20
  end
21
+ SerializedRecord::StringsRequired = StringsRequired
53
22
  end
@@ -1309,6 +1309,19 @@ module RubyEventStore
1309
1309
  )
1310
1310
  end
1311
1311
 
1312
+ specify "limit applies to records matching the time filter, not before it" do
1313
+ event_1 = SRecord.new(event_id: "8a6f053e-3ce2-4c82-a55b-4d02c66ae6ea", timestamp: Time.utc(2020, 1, 1))
1314
+ event_2 = SRecord.new(event_id: "8cee1139-4f96-483a-a175-2b947283c3c7", timestamp: Time.utc(2020, 1, 2))
1315
+ event_3 = SRecord.new(event_id: "d345f86d-b903-4d78-803f-38990c078d9e", timestamp: Time.utc(2020, 1, 3))
1316
+ repository.append_to_stream([event_1, event_2, event_3], Stream.new("whatever"), version_any)
1317
+
1318
+ expect(
1319
+ repository.read(
1320
+ specification.stream("whatever").backward.older_than_or_equal(Time.utc(2020, 1, 2)).limit(2).result,
1321
+ ).to_a,
1322
+ ).to eq([event_2, event_1])
1323
+ end
1324
+
1312
1325
  specify "fetching records older than or equal to specified date in stream" do
1313
1326
  event_1 = SRecord.new(event_id: "8a6f053e-3ce2-4c82-a55b-4d02c66ae6ea", timestamp: Time.utc(2020, 1, 1))
1314
1327
  event_2 = SRecord.new(event_id: "8cee1139-4f96-483a-a175-2b947283c3c7", timestamp: Time.utc(2020, 1, 2))
@@ -1578,6 +1591,35 @@ module RubyEventStore
1578
1591
  )
1579
1592
  end
1580
1593
 
1594
+ specify "named stream order is respected also when reading backward" do
1595
+ repository.append_to_stream(
1596
+ records = [
1597
+ RubyEventStore::SRecord.new(timestamp: with_precision(Time.new(2023, 1, 1, 12, 29, 0))),
1598
+ RubyEventStore::SRecord.new(timestamp: with_precision(Time.new(2023, 1, 1, 12, 28, 0))),
1599
+ RubyEventStore::SRecord.new(timestamp: with_precision(Time.new(2023, 1, 1, 12, 27, 0))),
1600
+ RubyEventStore::SRecord.new(
1601
+ timestamp: with_precision(Time.new(2023, 1, 1, 12, 26, 0)),
1602
+ valid_at: with_precision(Time.new(2023, 1, 1, 12, 30, 0)),
1603
+ ),
1604
+ ],
1605
+ RubyEventStore::Stream.new("stream"),
1606
+ RubyEventStore::ExpectedVersion.any,
1607
+ )
1608
+
1609
+ expect(repository.read(specification.stream("stream").backward.result).to_a).to eq(
1610
+ [records[3], records[2], records[1], records[0]],
1611
+ )
1612
+ expect(repository.read(specification.stream("stream").as_at.backward.result).to_a).to eq(
1613
+ [records[0], records[1], records[2], records[3]],
1614
+ )
1615
+ expect(repository.read(specification.stream("stream").as_at.backward.limit(2).result).to_a).to eq(
1616
+ [records[0], records[1]],
1617
+ )
1618
+ expect(repository.read(specification.stream("stream").as_of.backward.result).to_a).to eq(
1619
+ [records[3], records[0], records[1], records[2]],
1620
+ )
1621
+ end
1622
+
1581
1623
  specify "reading last event sorted by valid_at" do
1582
1624
  repository.append_to_stream(
1583
1625
  records = [
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module RubyEventStore
4
- VERSION = "3.0.1"
4
+ VERSION = "3.1.0"
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ruby_event_store
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.0.1
4
+ version: 3.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Arkency
@@ -112,14 +112,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
112
112
  requirements:
113
113
  - - ">="
114
114
  - !ruby/object:Gem::Version
115
- version: '2.7'
115
+ version: '3.3'
116
116
  required_rubygems_version: !ruby/object:Gem::Requirement
117
117
  requirements:
118
118
  - - ">="
119
119
  - !ruby/object:Gem::Version
120
120
  version: '0'
121
121
  requirements: []
122
- rubygems_version: 3.7.1
122
+ rubygems_version: 3.6.9
123
123
  specification_version: 4
124
124
  summary: Implementation of an event store in Ruby
125
125
  test_files: []