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
-
#as_at ⇒ Specification
Sets the order of time sorting using transaction time / Find out more.
-
#as_of ⇒ Specification
Sets the order of time sorting using validity time / Find out more.
-
#backward ⇒ Specification
Sets the order of reading events to descending (backward from the start).
-
#between(time_range) ⇒ Specification
Limits the query to events within given time range.
-
#count ⇒ Integer
Calculates the size of result set based on the specification build up to this point.
-
#each {|Event| ... } ⇒ Enumerator?
Executes the query based on the specification built up to this point.
-
#each_batch {|Array<Event>| ... } ⇒ 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| ... } ⇒ 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.
-
#map(&block) ⇒ Array
Executes the query based on the specification built up to this point and maps the result using provided block.
-
#newer_than(time) ⇒ Specification
Limits the query to events that occurred after given time.
-
#newer_than_or_equal(time) ⇒ Specification
Limits the query to events that occurred on or after given time.
-
#of_type(*types) ⇒ Specification
(also: #of_types)
Limits the query to certain event type(s).
-
#older_than(time) ⇒ Specification
Limits the query to events that occurred before given time.
-
#older_than_or_equal(time) ⇒ Specification
Limits the query to events that occurred on or before given time.
-
#read_first ⇒ Specification
Specifies that only first event should be read.
-
#read_last ⇒ Specification
Specifies that only last event should be read.
-
#reduce(accumulator = nil, &block) ⇒ Object
Reduces the results of the query based on the specification built up to this point result using provided block.
-
#stream(stream_name) ⇒ Specification
Limits the query to certain stream.
-
#to(stop) ⇒ Specification
Limits the query to events before or after another event.
-
#to_a ⇒ Array<Event>
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.
10 11 12 13 |
# File 'lib/ruby_event_store/specification.rb', line 10 def initialize(reader, result = SpecificationResult.new) @reader = reader @result = result end |
Instance Attribute Details
#result ⇒ Object (readonly)
Returns the value of attribute result.
335 336 337 |
# File 'lib/ruby_event_store/specification.rb', line 335 def result @result end |
Instance Method Details
#as_at ⇒ Specification
Sets the order of time sorting using transaction time / Find out more
127 128 129 |
# File 'lib/ruby_event_store/specification.rb', line 127 def as_at Specification.new(reader, result.dup { |r| r.time_sort_by = :as_at }) end |
#as_of ⇒ Specification
Sets the order of time sorting using validity time / Find out more
135 136 137 |
# File 'lib/ruby_event_store/specification.rb', line 135 def as_of Specification.new(reader, result.dup { |r| r.time_sort_by = :as_of }) end |
#backward ⇒ Specification
Sets the order of reading events to descending (backward from the start). / Find out more.
151 152 153 |
# File 'lib/ruby_event_store/specification.rb', line 151 def backward Specification.new(reader, result.dup { |r| r.direction = :backward }) end |
#between(time_range) ⇒ Specification
Limits the query to events within given time range. / Find out more.
115 116 117 118 119 120 121 |
# File 'lib/ruby_event_store/specification.rb', line 115 def between(time_range) if time_range.exclude_end? newer_than_or_equal(time_range.first).older_than(time_range.last) else newer_than_or_equal(time_range.first).older_than_or_equal(time_range.last) end end |
#count ⇒ Integer
Calculates the size of result set based on the specification build up to this point. / Find out more.
214 215 216 |
# File 'lib/ruby_event_store/specification.rb', line 214 def count reader.count(result) end |
#each {|Event| ... } ⇒ 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.
183 184 185 186 187 |
# File 'lib/ruby_event_store/specification.rb', line 183 def each return to_enum unless block_given? each_batch { |batch| batch.each { |event| yield event } } end |
#each_batch {|Array<Event>| ... } ⇒ 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.
171 172 173 174 175 |
# File 'lib/ruby_event_store/specification.rb', line 171 def each_batch return to_enum(:each_batch) unless block_given? reader.each(in_batches(result.batch_size).result) { |batch| yield batch } 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.
310 311 312 |
# File 'lib/ruby_event_store/specification.rb', line 310 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.
320 321 322 |
# File 'lib/ruby_event_store/specification.rb', line 320 def event!(event_id) event(event_id) or raise EventNotFound.new(event_id) end |
#events(event_ids) {|Event| ... } ⇒ 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.
331 332 333 |
# File 'lib/ruby_event_store/specification.rb', line 331 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.
272 273 274 |
# File 'lib/ruby_event_store/specification.rb', line 272 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.
143 144 145 |
# File 'lib/ruby_event_store/specification.rb', line 143 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 |
# File 'lib/ruby_event_store/specification.rb', line 29 def from(start) raise InvalidPageStart if start.nil? || start.empty? raise EventNotFound.new(start) unless reader.has_event?(start) 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.
240 241 242 243 244 245 246 247 248 |
# File 'lib/ruby_event_store/specification.rb', line 240 def in_batches(batch_size = DEFAULT_BATCH_SIZE) Specification.new( reader, result.dup do |r| r.read_as = :batch r.batch_size = batch_size end ) 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.
281 282 283 |
# File 'lib/ruby_event_store/specification.rb', line 281 def last reader.one(read_last.result) end |
#limit(count) ⇒ Specification
Limits the query to specified number of events. / Find out more.
160 161 162 163 |
# File 'lib/ruby_event_store/specification.rb', line 160 def limit(count) raise InvalidPageSize unless count && count > 0 Specification.new(reader, result.dup { |r| r.count = count }) end |
#map(&block) ⇒ Array
Executes the query based on the specification built up to this point and maps the result using provided block. / Find out more.
194 195 196 197 |
# File 'lib/ruby_event_store/specification.rb', line 194 def map(&block) raise ArgumentError.new("Block must be given") unless block_given? each.map(&block) end |
#newer_than(time) ⇒ Specification
Limits the query to events that occurred after given time. / Find out more.
83 84 85 86 87 88 89 90 91 92 |
# File 'lib/ruby_event_store/specification.rb', line 83 def newer_than(time) raise ArgumentError unless time.respond_to?(:to_time) Specification.new( reader, result.dup do |r| r.newer_than_or_equal = nil r.newer_than = time end ) end |
#newer_than_or_equal(time) ⇒ Specification
Limits the query to events that occurred on or after given time. / Find out more.
99 100 101 102 103 104 105 106 107 108 |
# File 'lib/ruby_event_store/specification.rb', line 99 def newer_than_or_equal(time) raise ArgumentError unless time.respond_to?(:to_time) Specification.new( reader, result.dup do |r| r.newer_than_or_equal = time r.newer_than = nil end ) end |
#of_type(*types) ⇒ Specification Also known as: of_types
Limits the query to certain event type(s). / Find out more.
290 291 292 |
# File 'lib/ruby_event_store/specification.rb', line 290 def of_type(*types) Specification.new(reader, result.dup { |r| r.with_types = types.flatten }) end |
#older_than(time) ⇒ Specification
Limits the query to events that occurred before given time. / Find out more.
51 52 53 54 55 56 57 58 59 60 |
# File 'lib/ruby_event_store/specification.rb', line 51 def older_than(time) raise ArgumentError unless time.respond_to?(:to_time) Specification.new( reader, result.dup do |r| r.older_than = time r.older_than_or_equal = nil end ) end |
#older_than_or_equal(time) ⇒ Specification
Limits the query to events that occurred on or before given time. / Find out more.
67 68 69 70 71 72 73 74 75 76 |
# File 'lib/ruby_event_store/specification.rb', line 67 def older_than_or_equal(time) raise ArgumentError unless time.respond_to?(:to_time) Specification.new( reader, result.dup do |r| r.older_than = nil r.older_than_or_equal = time end ) end |
#read_first ⇒ Specification
Specifies that only first event should be read. / Find out more.
255 256 257 |
# File 'lib/ruby_event_store/specification.rb', line 255 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.
263 264 265 |
# File 'lib/ruby_event_store/specification.rb', line 263 def read_last Specification.new(reader, result.dup { |r| r.read_as = :last }) end |
#reduce(accumulator = nil, &block) ⇒ Object
Reduces the results of the query based on the specification built up to this point result using provided block. / Find out more.
205 206 207 208 |
# File 'lib/ruby_event_store/specification.rb', line 205 def reduce(accumulator = nil, &block) raise ArgumentError.new("Block must be given") unless block_given? each.reduce(accumulator, &block) end |
#stream(stream_name) ⇒ Specification
Limits the query to certain stream. / Find out more.
20 21 22 |
# File 'lib/ruby_event_store/specification.rb', line 20 def stream(stream_name) Specification.new(reader, result.dup { |r| r.stream = Stream.new(stream_name) }) end |
#to(stop) ⇒ Specification
Limits the query to events before or after another event. / Find out more.
40 41 42 43 44 |
# File 'lib/ruby_event_store/specification.rb', line 40 def to(stop) raise InvalidPageStop if stop.nil? || stop.empty? raise EventNotFound.new(stop) unless reader.has_event?(stop) Specification.new(reader, result.dup { |r| r.stop = stop }) end |
#to_a ⇒ Array<Event>
Executes the query based on the specification built up to this point. Returns array of domain events. / Find out more.
223 224 225 |
# File 'lib/ruby_event_store/specification.rb', line 223 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.
300 301 302 |
# File 'lib/ruby_event_store/specification.rb', line 300 def with_id(event_ids) Specification.new(reader, result.dup { |r| r.with_ids = event_ids }) end |