ruby_event_store 2.7.0 → 2.8.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: 812fd5359d77a05770e48775b328225aacdf815dba3bcdd3d178d12c99c37702
4
- data.tar.gz: 0e559d46b60ef26c19262b6507baed2190d426a865067ad76aa39d8a25c861c5
3
+ metadata.gz: 2f56d74dfd29aa94e36814a0b4f481938561767c4fbe8ae75023a3036c949170
4
+ data.tar.gz: 38f1f724af62bbef3c8f428f3854bd66d6b8baa206368afbd4e04b947d524cb5
5
5
  SHA512:
6
- metadata.gz: ab93715c45c4d08b58ff5a294e64b517ba7e0798e1518ef9affd39c884c9e648a95713da7991b6eee74b1b4ea5e938e2536033ba3e8dc262adb65197745e5f14
7
- data.tar.gz: 908a2d22813f3decf421199b8ae53f2cae97fe34f150ce050c9fecefe2bfa30cd99e76c527b98386519af06aa4922dc531d89940b556151af818280f0852f0dd
6
+ metadata.gz: 52802d70f0a913f792a5a1cc1a10552164f5d19a44e496b3dc4459910836f1558aca29ddbed27e0b7d0fed2009abdcd157b093fcd63da2a3f82f2cb71539bc46
7
+ data.tar.gz: e720f4c54f8cb30688e4bde99380abff995d3f451fd990e607ce8eaf2e7fce961e1b06e2eb266953eacb05474771b7da1523120d0572ee1af754f342678ac38f
@@ -4,16 +4,64 @@ module RubyEventStore
4
4
  module Mappers
5
5
  module Transformation
6
6
  class PreserveTypes
7
- def initialize(type_resolver: ->(type) { type.to_s } )
8
- @registered_type_serializers = {}
9
- @type_resolver = type_resolver
7
+ def initialize(type_resolver: ->(type) { type.to_s })
8
+ @registry = Registry.new(type_resolver)
10
9
  end
11
10
 
12
- def register(type, serializer:, deserializer:)
13
- @registered_type_serializers[@type_resolver.(type)] = {
14
- serializer: serializer,
15
- deserializer: deserializer
16
- }
11
+ class NullType
12
+ PASS_THROUGH = ->(v) { v }
13
+ private_constant :PASS_THROUGH
14
+
15
+ def serializer
16
+ PASS_THROUGH
17
+ end
18
+
19
+ def deserializer
20
+ PASS_THROUGH
21
+ end
22
+
23
+ def stored_type
24
+ DEFAULT_STORE_TYPE
25
+ end
26
+ end
27
+ private_constant :NullType
28
+
29
+ class RegisteredType
30
+ def initialize(serializer, deserializer, stored_type)
31
+ @serializer = serializer
32
+ @deserializer = deserializer
33
+ @stored_type = stored_type
34
+ end
35
+
36
+ attr_reader :serializer, :deserializer, :stored_type
37
+ end
38
+ private_constant :RegisteredType
39
+
40
+ class Registry
41
+ def initialize(resolver)
42
+ @types = {}
43
+ @resolver = resolver
44
+ end
45
+
46
+
47
+ NULL_TYPE = NullType.new
48
+ private_constant :NULL_TYPE
49
+
50
+ def add(type, serializer, deserializer, stored_type)
51
+ types[resolver[type]] = RegisteredType.new(serializer, deserializer, stored_type)
52
+ end
53
+
54
+ def of(type)
55
+ types.fetch(resolver[type]) { NULL_TYPE }
56
+ end
57
+
58
+ private
59
+ attr_reader :resolver, :types
60
+ end
61
+ private_constant :Registry
62
+
63
+ def register(type, serializer:, deserializer:, stored_type: DEFAULT_STORE_TYPE)
64
+ registry.add(type, serializer, deserializer, stored_type)
17
65
  self
18
66
  end
19
67
 
@@ -21,46 +69,49 @@ module RubyEventStore
21
69
  data = transform(record.data)
22
70
  metadata = transform(record.metadata)
23
71
  if (metadata.respond_to?(:[]=))
24
- metadata[:types] = {
25
- data: store_type(record.data),
26
- metadata: store_type(record.metadata),
27
- }
72
+ metadata[:types] = { data: store_type(record.data), metadata: store_type(record.metadata) }
28
73
  end
29
74
 
30
75
  Record.new(
31
- event_id: record.event_id,
76
+ event_id: record.event_id,
32
77
  event_type: record.event_type,
33
- data: data,
34
- metadata: metadata,
35
- timestamp: record.timestamp,
36
- valid_at: record.valid_at,
78
+ data: data,
79
+ metadata: metadata,
80
+ timestamp: record.timestamp,
81
+ valid_at: record.valid_at
37
82
  )
38
83
  end
39
84
 
40
85
  def load(record)
41
- types = record.metadata.delete(:types) rescue nil
86
+ types =
87
+ begin
88
+ record.metadata.delete(:types)
89
+ rescue StandardError
90
+ nil
91
+ end
42
92
  data_types = types&.fetch(:data, nil)
43
93
  metadata_types = types&.fetch(:metadata, nil)
44
94
  data = data_types ? restore_type(record.data, data_types) : record.data
45
95
  metadata = metadata_types ? restore_type(record.metadata, metadata_types) : record.metadata
46
96
 
47
97
  Record.new(
48
- event_id: record.event_id,
98
+ event_id: record.event_id,
49
99
  event_type: record.event_type,
50
- data: data,
51
- metadata: metadata,
52
- timestamp: record.timestamp,
53
- valid_at: record.valid_at,
100
+ data: data,
101
+ metadata: metadata,
102
+ timestamp: record.timestamp,
103
+ valid_at: record.valid_at
54
104
  )
55
105
  end
56
106
 
107
+ DEFAULT_STORE_TYPE = ->(argument) { argument.class.name }
108
+ private_constant :DEFAULT_STORE_TYPE
109
+
57
110
  private
58
- PASS_THROUGH = ->(v) { v }
111
+ attr_reader :registry
59
112
 
60
113
  def transform_hash(argument)
61
- argument.each_with_object({}) do |(key, value), hash|
62
- hash[transform(key)] = transform(value)
63
- end
114
+ argument.each_with_object({}) { |(key, value), hash| hash[transform(key)] = transform(value) }
64
115
  end
65
116
 
66
117
  def transform(argument)
@@ -68,9 +119,9 @@ module RubyEventStore
68
119
  when Hash
69
120
  transform_hash(argument)
70
121
  when Array
71
- argument.map{|i| transform(i)}
122
+ argument.map { |i| transform(i) }
72
123
  else
73
- serializer_of(argument.class).call(argument)
124
+ registry.of(argument.class).serializer[argument]
74
125
  end
75
126
  end
76
127
 
@@ -85,15 +136,15 @@ module RubyEventStore
85
136
  when Hash
86
137
  store_types(argument)
87
138
  when Array
88
- argument.map{|i| store_type(i)}
139
+ argument.map { |i| store_type(i) }
89
140
  else
90
- argument.class.name
141
+ registry.of(argument.class).stored_type[argument]
91
142
  end
92
143
  end
93
144
 
94
145
  def restore_types(argument, types)
95
146
  argument.each_with_object({}) do |(key, value), hash|
96
- key_type, value_type = types.fetch(key.to_sym)
147
+ key_type, value_type = types.fetch(key.to_sym) { types.fetch(key.to_s) }
97
148
  restored_key = restore_type(key, key_type)
98
149
  hash[restored_key] = restore_type(value, value_type)
99
150
  end
@@ -104,19 +155,11 @@ module RubyEventStore
104
155
  when Hash
105
156
  restore_types(argument, type)
106
157
  when Array
107
- argument.each_with_index.map{|a,idx| restore_type(a, type.fetch(idx))}
158
+ argument.each_with_index.map { |a, idx| restore_type(a, type.fetch(idx)) }
108
159
  else
109
- deserializer_of(type).call(argument)
160
+ registry.of(type).deserializer[argument]
110
161
  end
111
162
  end
112
-
113
- def serializer_of(type)
114
- @registered_type_serializers.dig(@type_resolver.(type), :serializer) || PASS_THROUGH
115
- end
116
-
117
- def deserializer_of(type)
118
- @registered_type_serializers.dig(type, :deserializer) || PASS_THROUGH
119
- end
120
163
  end
121
164
  end
122
165
  end
@@ -223,6 +223,22 @@ module RubyEventStore
223
223
  attributes.read_as.equal?(:all)
224
224
  end
225
225
 
226
+ # Read strategy. True if results will be sorted by timestamp
227
+ # {http://railseventstore.org/docs/read/ Find out more}.
228
+ #
229
+ # @return [Boolean]
230
+ def time_sort_by_as_at?
231
+ time_sort_by.equal?(:as_at)
232
+ end
233
+
234
+ # Read strategy. True if results will be sorted by valid_at
235
+ # {http://railseventstore.org/docs/read/ Find out more}.
236
+ #
237
+ # @return [Boolean]
238
+ def time_sort_by_as_of?
239
+ time_sort_by.equal?(:as_of)
240
+ end
241
+
226
242
  # Clone [SpecificationResult]
227
243
  # If block is given cloned attributes might be modified.
228
244
  #
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module RubyEventStore
4
- VERSION = "2.7.0"
4
+ VERSION = "2.8.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: 2.7.0
4
+ version: 2.8.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Arkency
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-12-19 00:00:00.000000000 Z
11
+ date: 2023-01-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: concurrent-ruby