Class: RubyEventStore::Client
- Inherits:
-
Object
- Object
- RubyEventStore::Client
- Defined in:
- lib/ruby_event_store/client.rb
Defined Under Namespace
Classes: Within
Instance Method Summary collapse
-
#append(events, stream_name: GLOBAL_STREAM, expected_version: :any) ⇒ self
Persists new event(s) without notifying any subscribed handlers.
-
#delete_stream(stream_name) ⇒ self
Deletes a stream.
-
#deserialize(serializer:, event_type:, event_id:, data:, metadata:, timestamp: nil, valid_at: nil) ⇒ Event
Deserialize event which was serialized for async event handlers Read more.
-
#initialize(repository:, mapper: Mappers::Default.new, subscriptions: Subscriptions.new, dispatcher: Dispatcher.new, clock: default_clock, correlation_id_generator: default_correlation_id_generator) ⇒ Client
constructor
A new instance of Client.
- #inspect ⇒ Object
-
#link(event_ids, stream_name:, expected_version: :any) ⇒ self
Links already persisted event(s) to a different stream.
-
#metadata ⇒ Hash
Read additional metadata which will be added for published events Read more.
-
#overwrite(events_or_event) ⇒ self
Overwrite existing event(s) with the same ID.
-
#publish(events, stream_name: GLOBAL_STREAM, expected_version: :any) ⇒ self
Persists events and notifies subscribed handlers about them.
-
#read ⇒ Specification
Starts building a query specification for reading events.
-
#streams_of(event_id) ⇒ Array<Stream>
Gets list of streams where event is stored or linked.
-
#subscribe(subscriber = nil, to:, &proc) ⇒ Object
Subscribes a handler (subscriber) that will be invoked for published events of provided type.
-
#subscribe_to_all_events(subscriber = nil, &proc) ⇒ Object
Subscribes a handler (subscriber) that will be invoked for all published events.
-
#subscribers_for(event_class) ⇒ Array<Object, Class>
Get list of handlers subscribed to an event.
-
#with_metadata(metadata, &block) ⇒ Object
Set additional metadata for all events published within the provided block Read more.
-
#within(&block) ⇒ Within
Use for starting temporary subscriptions.
Constructor Details
#initialize(repository:, mapper: Mappers::Default.new, subscriptions: Subscriptions.new, dispatcher: Dispatcher.new, clock: default_clock, correlation_id_generator: default_correlation_id_generator) ⇒ Client
Returns a new instance of Client.
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/ruby_event_store/client.rb', line 7 def initialize(repository:, mapper: Mappers::Default.new, subscriptions: Subscriptions.new, dispatcher: Dispatcher.new, clock: default_clock, correlation_id_generator: default_correlation_id_generator) @repository = repository @mapper = mapper @subscriptions = subscriptions @broker = Broker.new(subscriptions: subscriptions, dispatcher: dispatcher) @clock = clock @metadata = Concurrent::ThreadLocalVar.new @correlation_id_generator = correlation_id_generator end |
Instance Method Details
#append(events, stream_name: GLOBAL_STREAM, expected_version: :any) ⇒ self
Persists new event(s) without notifying any subscribed handlers
50 51 52 53 54 55 56 57 |
# File 'lib/ruby_event_store/client.rb', line 50 def append(events, stream_name: GLOBAL_STREAM, expected_version: :any) append_records_to_stream( transform((events)), stream_name: stream_name, expected_version: expected_version ) self end |
#delete_stream(stream_name) ⇒ self
Deletes a stream. All events from the stream remain intact but they are no longer linked to the stream.
77 78 79 80 |
# File 'lib/ruby_event_store/client.rb', line 77 def delete_stream(stream_name) repository.delete_stream(Stream.new(stream_name)) self end |
#deserialize(serializer:, event_type:, event_id:, data:, metadata:, timestamp: nil, valid_at: nil) ⇒ Event
Deserialize event which was serialized for async event handlers Read more
240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 |
# File 'lib/ruby_event_store/client.rb', line 240 def deserialize(serializer:, event_type:, event_id:, data:, metadata:, timestamp: nil, valid_at: nil) = lambda do |m| (m[:timestamp] || Time.parse(m.fetch('timestamp'))).iso8601 end mapper.record_to_event( SerializedRecord.new( event_type: event_type, event_id: event_id, data: data, metadata: , timestamp: || = [serializer.load()], valid_at: valid_at || , ).deserialize(serializer) ) end |
#inspect ⇒ Object
299 300 301 |
# File 'lib/ruby_event_store/client.rb', line 299 def inspect "#<#{self.class}:0x#{__id__.to_s(16)}>" end |
#link(event_ids, stream_name:, expected_version: :any) ⇒ self
Links already persisted event(s) to a different stream. Does not notify any subscribed handlers.
66 67 68 69 |
# File 'lib/ruby_event_store/client.rb', line 66 def link(event_ids, stream_name:, expected_version: :any) repository.link_to_stream(Array(event_ids), Stream.new(stream_name), ExpectedVersion.new(expected_version)) self end |
#metadata ⇒ Hash
Read additional metadata which will be added for published events Read more
261 262 263 |
# File 'lib/ruby_event_store/client.rb', line 261 def @metadata.value || EMPTY_HASH end |
#overwrite(events_or_event) ⇒ self
Overwrite existing event(s) with the same ID.
Does not notify any subscribed handlers. Does not enrich with additional current metadata. Does not allow changing which streams these events are in. Read more
294 295 296 297 |
# File 'lib/ruby_event_store/client.rb', line 294 def overwrite(events_or_event) repository.(transform(Array(events_or_event))) self end |
#publish(events, stream_name: GLOBAL_STREAM, expected_version: :any) ⇒ self
Persists events and notifies subscribed handlers about them
31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/ruby_event_store/client.rb', line 31 def publish(events, stream_name: GLOBAL_STREAM, expected_version: :any) enriched_events = (events) records = transform(enriched_events) append_records_to_stream(records, stream_name: stream_name, expected_version: expected_version) enriched_events.zip(records) do |event, record| ( correlation_id: event..fetch(:correlation_id), causation_id: event.event_id, ) do broker.(event, record) end end self end |
#read ⇒ Specification
Starts building a query specification for reading events. / More info.
86 87 88 |
# File 'lib/ruby_event_store/client.rb', line 86 def read Specification.new(SpecificationReader.new(repository, mapper)) end |
#streams_of(event_id) ⇒ Array<Stream>
Gets list of streams where event is stored or linked
93 94 95 |
# File 'lib/ruby_event_store/client.rb', line 93 def streams_of(event_id) repository.streams_of(event_id) end |
#subscribe(subscriber, to: ) ⇒ Proc #subscribe(to: , &subscriber) ⇒ Proc
Subscribes a handler (subscriber) that will be invoked for published events of provided type.
109 110 111 112 113 |
# File 'lib/ruby_event_store/client.rb', line 109 def subscribe(subscriber = nil, to:, &proc) raise ArgumentError, "subscriber must be first argument or block, cannot be both" if subscriber && proc subscriber ||= proc broker.add_subscription(subscriber, to) end |
#subscribe_to_all_events(subscriber) ⇒ Proc #subscribe_to_all_events(&subscriber) ⇒ Proc
Subscribes a handler (subscriber) that will be invoked for all published events
125 126 127 128 |
# File 'lib/ruby_event_store/client.rb', line 125 def subscribe_to_all_events(subscriber = nil, &proc) raise ArgumentError, "subscriber must be first argument or block, cannot be both" if subscriber && proc broker.add_global_subscription(subscriber || proc) end |
#subscribers_for(event_class) ⇒ Array<Object, Class>
Get list of handlers subscribed to an event
134 135 136 |
# File 'lib/ruby_event_store/client.rb', line 134 def subscribers_for(event_class) subscriptions.all_for(event_type_resolver.call(event_class)) end |
#with_metadata(metadata, &block) ⇒ Object
Set additional metadata for all events published within the provided block Read more
228 229 230 231 232 233 234 |
# File 'lib/ruby_event_store/client.rb', line 228 def (, &block) = () self. = .merge() block.call if block_given? ensure self. = end |