Class: CommandPost::AggregateEvent

Inherits:
Object
  • Object
show all
Defined in:
lib/command_post/event_sourcing/aggregate_event.rb

Constant Summary collapse

@@required_by_txn =
[ "aggregate_type", "aggregate_id", "event_description", "content", "transaction_id", "transacted" ]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeAggregateEvent

Returns a new instance of AggregateEvent.



19
20
21
22
23
# File 'lib/command_post/event_sourcing/aggregate_event.rb', line 19

def initialize
  @transaction_id = SequenceGenerator.transaction_id
  @transacted = Time.now 
  @object = @changes = nil
end

Instance Attribute Details

#aggregate_idObject

Returns the value of attribute aggregate_id.



7
8
9
# File 'lib/command_post/event_sourcing/aggregate_event.rb', line 7

def aggregate_id
  @aggregate_id
end

#aggregate_typeObject

Returns the value of attribute aggregate_type.



7
8
9
# File 'lib/command_post/event_sourcing/aggregate_event.rb', line 7

def aggregate_type
  @aggregate_type
end

#call_stackObject

Returns the value of attribute call_stack.



7
8
9
# File 'lib/command_post/event_sourcing/aggregate_event.rb', line 7

def call_stack
  @call_stack
end

#changesObject

Returns the value of attribute changes.



7
8
9
# File 'lib/command_post/event_sourcing/aggregate_event.rb', line 7

def changes
  @changes
end

#contentObject

Returns the value of attribute content.



7
8
9
# File 'lib/command_post/event_sourcing/aggregate_event.rb', line 7

def content
  @content
end

#event_descriptionObject

Returns the value of attribute event_description.



7
8
9
# File 'lib/command_post/event_sourcing/aggregate_event.rb', line 7

def event_description
  @event_description
end

#objectObject

Returns the value of attribute object.



7
8
9
# File 'lib/command_post/event_sourcing/aggregate_event.rb', line 7

def object
  @object
end

#transactedObject

Returns the value of attribute transacted.



7
8
9
# File 'lib/command_post/event_sourcing/aggregate_event.rb', line 7

def transacted
  @transacted
end

#transaction_idObject

Returns the value of attribute transaction_id.



7
8
9
# File 'lib/command_post/event_sourcing/aggregate_event.rb', line 7

def transaction_id
  @transaction_id
end

#user_idObject

Returns the value of attribute user_id.



7
8
9
# File 'lib/command_post/event_sourcing/aggregate_event.rb', line 7

def user_id
  @user_id
end

Class Method Details

.get_history_by_aggregate_id(aggregate_id) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/command_post/event_sourcing/aggregate_event.rb', line 44

def self.get_history_by_aggregate_id aggregate_id 
  hash = Hash.new
  cnt = 0
  results = Array.new 
  $DB.fetch("SELECT * FROM aggregate_events WHERE aggregate_id = ? order by transacted",  aggregate_id ) do |row|
    hash = JSON.parse(row[:content])
    results << "==================================================================================="
    results << "Version: #{cnt += 1} "
    results << "Event Description: #{row[:event_description]}  "
    results << "Change Made By:    #{row[:user_id]}"
    results << "------   change data ------------"
    results << hash.pretty_inspect
  end
  results
end

.publish(event) ⇒ Object



36
37
38
# File 'lib/command_post/event_sourcing/aggregate_event.rb', line 36

def self.publish event 
  event.publish
end

Instance Method Details

#publishObject



25
26
27
28
29
30
31
32
33
34
# File 'lib/command_post/event_sourcing/aggregate_event.rb', line 25

def publish 
  if changes
    save_event changes
  elsif object
    @aggregate_lookup_value = object.aggregate_lookup_value
    save_event object.to_h
  else
    raise 'Event has no state to publish.'
  end
end