Module: Euston::EventStore::Persistence::Mongodb::MongoCommit

Extended by:
ActiveSupport::Concern
Included in:
Commit
Defined in:
lib/euston-eventstore/persistence/mongodb/mongo_commit.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#dispatchedObject (readonly)

Returns the value of attribute dispatched.



42
43
44
# File 'lib/euston-eventstore/persistence/mongodb/mongo_commit.rb', line 42

def dispatched
  @dispatched
end

Class Method Details

.from_hash(hash) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/euston-eventstore/persistence/mongodb/mongo_commit.rb', line 16

def from_hash(hash)
  return nil if hash.nil?

  id = hash['_id']
  events = hash['events'].sort_by { |e| e["stream_revision"] }.to_a
  commands = hash['commands'] || []
  stream_revision = events.last['stream_revision']
  events = events.map { |e| MongoEventMessage.from_hash e['payload'] }
  commands = commands.map { |c| MongoCommandMessage.from_hash c }

  Euston::EventStore::Commit.new :stream_id => id['stream_id'],
                                 :stream_revision => stream_revision,
                                 :commit_id => hash['commit_id'],
                                 :commit_sequence => id['commit_sequence'],
                                 :commit_timestamp => hash['commit_timestamp'],
                                 :headers => hash['headers'].recursive_symbolize_keys!,
                                 :events => events,
                                 :commands => commands
end

Instance Method Details

#mongo_initialize(hash) ⇒ Object



37
38
39
40
# File 'lib/euston-eventstore/persistence/mongodb/mongo_commit.rb', line 37

def mongo_initialize(hash)
  original_initialize(hash)
  @dispatched = hash[:dispatched]
end

#to_id_queryObject



76
77
78
79
80
81
# File 'lib/euston-eventstore/persistence/mongodb/mongo_commit.rb', line 76

def to_id_query
  {
    '_id.commit_sequence' => commit_sequence,
    '_id.stream_id' => stream_id
  }
end

#to_mongo_commitObject



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/euston-eventstore/persistence/mongodb/mongo_commit.rb', line 56

def to_mongo_commit
  mongo_stream_revision = stream_revision - (events.length - 1)

  hash = to_mongo_hash

  hash[:events] = events.map do |e|
    event_hash = { :stream_revision => mongo_stream_revision, :payload => e.to_hash }
    mongo_stream_revision += 1
    event_hash
  end

  hash[:commands] = commands.map do |c|
    c.to_hash
  end

  hash[:commit_timestamp_for_humans] = Time.at(hash[:commit_timestamp]).utc

  hash
end

#to_mongo_hashObject



44
45
46
47
48
49
50
51
52
53
54
# File 'lib/euston-eventstore/persistence/mongodb/mongo_commit.rb', line 44

def to_mongo_hash
  hash = original_to_hash
  hash[:_id] = { :stream_id => hash.delete(:stream_id), :commit_sequence => hash.delete(:commit_sequence) }
  hash.delete :stream_revision
  hash.delete :commit_sequence
  hash[:dispatched] ||= false
  hash[:events] = hash[:events].map { |e| e.to_hash }
  hash[:commands] = hash[:commands].map { |c| c.to_hash }
  hash[:commit_timestamp] = hash[:commit_timestamp].to_f
  hash
end