Class: RubyEventStore::Specification
- Inherits:
-
Object
- Object
- RubyEventStore::Specification
- Defined in:
- lib/ruby_event_store/specification.rb
Overview
Used for building and executing the query specification.
Constant Summary collapse
- DEFAULT_BATCH_SIZE =
100
Instance Attribute Summary collapse
-
#result ⇒ Object
readonly
Returns the value of attribute result.
Instance Method Summary collapse
-
#backward ⇒ Specification
Sets the order of reading events to descending (backward from the start).
-
#count ⇒ Integer
Calculates the size of result set based on the specification build up to this point.
-
#each {|Event, Proto| ... } ⇒ Enumerator?
Executes the query based on the specification built up to this point.
-
#each_batch {|Array<Event, Proto>| ... } ⇒ Enumerator?
Executes the query based on the specification built up to this point.
-
#event(event_id) ⇒ Event?
Reads single event from repository.
-
#event!(event_id) ⇒ Event
Reads single existing event from repository.
-
#events(event_ids) {|Event, Proto| ... } ⇒ Enumerator
Reads all events of given ids from repository.
-
#first ⇒ Event?
Executes the query based on the specification built up to this point.
-
#forward ⇒ Specification
Sets the order of reading events to ascending (forward from the start).
-
#from(start) ⇒ Specification
Limits the query to events before or after another event.
-
#in_batches(batch_size = DEFAULT_BATCH_SIZE) ⇒ Specification
(also: #in_batches_of)
Specifies that events should be obtained in batches.
-
#initialize(reader, result = SpecificationResult.new) ⇒ Specification
constructor
private
A new instance of Specification.
-
#last ⇒ Event?
Executes the query based on the specification built up to this point.
-
#limit(count) ⇒ Specification
Limits the query to specified number of events.
-
#of_type(types) ⇒ Specification
Limits the query to certain event types.
-
#read_first ⇒ Specification
Specifies that only first event should be read.
-
#read_last ⇒ Specification
Specifies that only last event should be read.
-
#stream(stream_name) ⇒ Specification
Limits the query to certain stream.
-
#to_a ⇒ Array<Event, Proto>
Executes the query based on the specification built up to this point.
-
#with_id(event_ids) ⇒ Specification
Limits the query to certain events by given even ids.
Constructor Details
#initialize(reader, result = SpecificationResult.new) ⇒ Specification
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns a new instance of Specification.
8 9 10 11 |
# File 'lib/ruby_event_store/specification.rb', line 8 def initialize(reader, result = SpecificationResult.new) @reader = reader @result = result end |
Instance Attribute Details
#result ⇒ Object (readonly)
Returns the value of attribute result.
212 213 214 |
# File 'lib/ruby_event_store/specification.rb', line 212 def result @result end |
Instance Method Details
#backward ⇒ Specification
Sets the order of reading events to descending (backward from the start). / Find out more.
52 53 54 |
# File 'lib/ruby_event_store/specification.rb', line 52 def backward Specification.new(reader, result.dup { |r| r.direction = :backward }) end |
#count ⇒ Integer
Calculates the size of result set based on the specification build up to this point. / Find out more.
98 99 100 |
# File 'lib/ruby_event_store/specification.rb', line 98 def count reader.count(result) end |
#each {|Event, Proto| ... } ⇒ Enumerator?
Executes the query based on the specification built up to this point. Yields events read from the store if block given. Otherwise, returns enumerable collection. / Find out more.
86 87 88 89 90 91 92 |
# File 'lib/ruby_event_store/specification.rb', line 86 def each return to_enum unless block_given? each_batch do |batch| batch.each { |event| yield event } end end |
#each_batch {|Array<Event, Proto>| ... } ⇒ Enumerator?
Executes the query based on the specification built up to this point. Yields each batch of records that was retrieved from the store. / Find out more.
72 73 74 75 76 77 78 |
# File 'lib/ruby_event_store/specification.rb', line 72 def each_batch return to_enum(:each_batch) unless block_given? reader.each(in_batches(result.batch_size).result) do |batch| yield batch end end |
#event(event_id) ⇒ Event?
Reads single event from repository. Returns the event with specified id or nil if event is not found in specified collection of events. / Find out more.
187 188 189 |
# File 'lib/ruby_event_store/specification.rb', line 187 def event(event_id) reader.one(read_first.with_id([event_id]).result) end |
#event!(event_id) ⇒ Event
Reads single existing event from repository. Returns the event with specified id or raises [EventNotFound] error if event is not found in specified collection of events. / Find out more.
197 198 199 |
# File 'lib/ruby_event_store/specification.rb', line 197 def event!(event_id) event(event_id) or raise EventNotFound.new(event_id) end |
#events(event_ids) {|Event, Proto| ... } ⇒ Enumerator
Reads all events of given ids from repository. Yields each event (found by id in specified collection of events) read from the store if block given. Otherwise, returns enumerable collection. / Find out more.
208 209 210 |
# File 'lib/ruby_event_store/specification.rb', line 208 def events(event_ids) with_id(event_ids).each end |
#first ⇒ Event?
Executes the query based on the specification built up to this point. Returns the first event in specified collection of events. / Find out more.
150 151 152 |
# File 'lib/ruby_event_store/specification.rb', line 150 def first reader.one(read_first.result) end |
#forward ⇒ Specification
Sets the order of reading events to ascending (forward from the start). / Find out more.
44 45 46 |
# File 'lib/ruby_event_store/specification.rb', line 44 def forward Specification.new(reader, result.dup { |r| r.direction = :forward }) end |
#from(start) ⇒ Specification
Limits the query to events before or after another event. / Find out more.
29 30 31 32 33 34 35 36 37 38 |
# File 'lib/ruby_event_store/specification.rb', line 29 def from(start) case start when Symbol raise InvalidPageStart unless [:head].include?(start) else raise InvalidPageStart if start.nil? || start.empty? raise EventNotFound.new(start) unless reader.has_event?(start) end Specification.new(reader, result.dup { |r| r.start = start }) end |
#in_batches(batch_size = DEFAULT_BATCH_SIZE) ⇒ Specification Also known as: in_batches_of
Specifies that events should be obtained in batches. / Find out more.
Looping through a collection of events from the store can be inefficient since it will try to instantiate all the events at once.
In that case, batch processing methods allow you to work with the records in batches, thereby greatly reducing memory consumption.
124 125 126 |
# File 'lib/ruby_event_store/specification.rb', line 124 def in_batches(batch_size = DEFAULT_BATCH_SIZE) Specification.new(reader, result.dup { |r| r.read_as = :batch; r.batch_size = batch_size }) end |
#last ⇒ Event?
Executes the query based on the specification built up to this point. Returns the last event in specified collection of events. / Find out more.
159 160 161 |
# File 'lib/ruby_event_store/specification.rb', line 159 def last reader.one(read_last.result) end |
#limit(count) ⇒ Specification
Limits the query to specified number of events. / Find out more.
61 62 63 64 |
# File 'lib/ruby_event_store/specification.rb', line 61 def limit(count) raise InvalidPageSize unless count && count > 0 Specification.new(reader, result.dup { |r| r.count = count }) end |
#of_type(types) ⇒ Specification
Limits the query to certain event types. / Find out more.
168 169 170 |
# File 'lib/ruby_event_store/specification.rb', line 168 def of_type(types) Specification.new(reader, result.dup{ |r| r.with_types = types }) end |
#read_first ⇒ Specification
Specifies that only first event should be read. / Find out more.
133 134 135 |
# File 'lib/ruby_event_store/specification.rb', line 133 def read_first Specification.new(reader, result.dup { |r| r.read_as = :first }) end |
#read_last ⇒ Specification
Specifies that only last event should be read. / Find out more.
141 142 143 |
# File 'lib/ruby_event_store/specification.rb', line 141 def read_last Specification.new(reader, result.dup { |r| r.read_as = :last }) end |
#stream(stream_name) ⇒ Specification
Limits the query to certain stream. / Find out more.
18 19 20 |
# File 'lib/ruby_event_store/specification.rb', line 18 def stream(stream_name) Specification.new(reader, result.dup { |r| r.stream = Stream.new(stream_name) }) end |
#to_a ⇒ Array<Event, Proto>
Executes the query based on the specification built up to this point. Returns array of domain events. / Find out more.
107 108 109 |
# File 'lib/ruby_event_store/specification.rb', line 107 def to_a each.to_a end |
#with_id(event_ids) ⇒ Specification
Limits the query to certain events by given even ids. / Find out more.
177 178 179 |
# File 'lib/ruby_event_store/specification.rb', line 177 def with_id(event_ids) Specification.new(reader, result.dup{ |r| r.with_ids = event_ids }) end |