Class: Euston::EventStore::Commit

Inherits:
Object
  • Object
show all
Includes:
Persistence::Mongodb::MongoCommit
Defined in:
lib/euston-eventstore/commit.rb,
lib/euston-eventstore/persistence/mongodb/mongo_commit.rb

Overview

Represents a series of events which have been fully committed as a single unit and which apply to the stream indicated.

Instance Attribute Summary collapse

Attributes included from Persistence::Mongodb::MongoCommit

#dispatched

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Persistence::Mongodb::MongoCommit

from_hash, #mongo_initialize, #to_id_query, #to_mongo_commit, #to_mongo_hash

Constructor Details

#initialize(hash) ⇒ Commit

Returns a new instance of Commit.



6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/euston-eventstore/commit.rb', line 6

def initialize(hash)
  defaults = {
    :stream_id => nil,
    :stream_revision => 1,
    :commit_id => nil,
    :commit_sequence => 1,
    :commit_timestamp => Time.now.utc,
    :headers => OpenStruct.new,
    :events => [],
    :commands => []
  }
  values = defaults.merge hash
  defaults.keys.each { |key| instance_variable_set "@#{key}", values[key] }
end

Instance Attribute Details

#commandsObject (readonly)

Gets the collection of command messages to be committed as a single unit.



55
56
57
# File 'lib/euston-eventstore/commit.rb', line 55

def commands
  @commands
end

#commit_idObject (readonly)

Gets the value which uniquely identifies the commit within the stream.



40
41
42
# File 'lib/euston-eventstore/commit.rb', line 40

def commit_id
  @commit_id
end

#commit_sequenceObject (readonly)

Gets the value which indicates the sequence (or position) in the stream to which this commit applies.



43
44
45
# File 'lib/euston-eventstore/commit.rb', line 43

def commit_sequence
  @commit_sequence
end

#commit_timestampObject (readonly)

Gets the point in time at which the commit was persisted.



46
47
48
# File 'lib/euston-eventstore/commit.rb', line 46

def commit_timestamp
  @commit_timestamp
end

#eventsObject (readonly)

Gets the collection of event messages to be committed as a single unit.



52
53
54
# File 'lib/euston-eventstore/commit.rb', line 52

def events
  @events
end

#headersObject (readonly)

Gets the metadata which provides additional, unstructured information about this commit.



49
50
51
# File 'lib/euston-eventstore/commit.rb', line 49

def headers
  @headers
end

#stream_idObject (readonly)

Gets the value which uniquely identifies the stream to which the commit belongs.



34
35
36
# File 'lib/euston-eventstore/commit.rb', line 34

def stream_id
  @stream_id
end

#stream_revisionObject (readonly)

Gets the value which indicates the revision of the most recent event in the stream to which this commit applies.



37
38
39
# File 'lib/euston-eventstore/commit.rb', line 37

def stream_revision
  @stream_revision
end

Class Method Details

.empty?(attempt) ⇒ Boolean

Returns:

  • (Boolean)


62
63
64
# File 'lib/euston-eventstore/commit.rb', line 62

def empty?(attempt)
  attempt.nil? || attempt.events.empty?
end

.has_identifier?(attempt) ⇒ Boolean

Returns:

  • (Boolean)


66
67
68
# File 'lib/euston-eventstore/commit.rb', line 66

def has_identifier?(attempt)
  !(attempt.stream_id.nil? || attempt.commit_id.nil?)
end

.valid?(attempt) ⇒ Boolean

Returns:

  • (Boolean)

Raises:

  • (ArgumentError)


70
71
72
73
74
75
76
77
78
# File 'lib/euston-eventstore/commit.rb', line 70

def valid?(attempt)
  raise ArgumentError.new('The commit must not be nil.') if attempt.nil?
  raise ArgumentError.new('The commit must be uniquely identified.') unless Commit.has_identifier? attempt
  raise ArgumentError.new('The commit sequence must be a positive number.') unless attempt.commit_sequence > 0
  raise ArgumentError.new('The stream revision must be a positive number.') unless attempt.stream_revision > 0
  raise ArgumentError.new('The stream revision must always be greater than or equal to the commit sequence.') if (attempt.stream_revision < attempt.commit_sequence)

  true
end

Instance Method Details

#==(other) ⇒ Object



57
58
59
# File 'lib/euston-eventstore/commit.rb', line 57

def ==(other)
  (other.is_a? Commit) && (@stream_id == other.stream_id) && (@commit_id == other.commit_id)
end

#to_hashObject



21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/euston-eventstore/commit.rb', line 21

def to_hash
  {
    :stream_id => stream_id,
    :stream_revision => stream_revision,
    :commit_id => commit_id,
    :commit_sequence => commit_sequence,
    :commit_timestamp => commit_timestamp,
    :headers => headers.is_a?(OpenStruct) ? headers.instance_variable_get(:@table) : headers,
    :events => events,
    :commands => commands
  }
end