Class: RubyEventStore::Specification

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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

#resultObject (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

#backwardSpecification

Sets the order of reading events to descending (backward from the start). / Find out more.

Returns:



52
53
54
# File 'lib/ruby_event_store/specification.rb', line 52

def backward
  Specification.new(reader, result.dup { |r| r.direction = :backward })
end

#countInteger

Calculates the size of result set based on the specification build up to this point. / Find out more.

Returns:

  • (Integer)

    Number of events to read



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.

Yields:

Returns:

  • (Enumerator, nil)

    Enumerator is returned when block not given



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.

Yields:

Returns:

  • (Enumerator, nil)

    Enumerator is returned when block not given



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.

Returns:



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.

Returns:



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.

Yields:

Returns:

  • (Enumerator)

    Enumerator is returned when block not given



208
209
210
# File 'lib/ruby_event_store/specification.rb', line 208

def events(event_ids)
  with_id(event_ids).each
end

#firstEvent?

Executes the query based on the specification built up to this point. Returns the first event in specified collection of events. / Find out more.

Returns:



150
151
152
# File 'lib/ruby_event_store/specification.rb', line 150

def first
  reader.one(read_first.result)
end

#forwardSpecification

Sets the order of reading events to ascending (forward from the start). / Find out more.

Returns:



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.

Parameters:

  • start (:head, String)

    id of event to start reading from. :head can mean the beginning or end of the stream, depending on the #direction

Returns:



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.

Parameters:

  • batch_size (Integer) (defaults to: DEFAULT_BATCH_SIZE)

    number of events to read in a single batch

Returns:



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

#lastEvent?

Executes the query based on the specification built up to this point. Returns the last event in specified collection of events. / Find out more.

Returns:



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.

Parameters:

  • count (Integer)

    maximal number of events to retrieve

Returns:

Raises:



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.

Returns:



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_firstSpecification

Specifies that only first event should be read. / Find out more.

Returns:



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_lastSpecification

Specifies that only last event should be read. / Find out more.

Returns:



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.

Parameters:

  • stream_name (String)

    name of the stream to get events from

Returns:



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_aArray<Event, Proto>

Executes the query based on the specification built up to this point. Returns array of domain events. / Find out more.

Returns:



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.

Parameters:

  • even_ids (Array(String))

    ids of event to look for.

Returns:



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