module RubyCqrs::Domain::Aggregate
Attributes
aggregate_id[R]
version[R]
Public Class Methods
new()
click to toggle source
Calls superclass method
# File lib/ruby_cqrs/domain/aggregate.rb, line 14 def initialize @aggregate_id = Guid.create @version = 0 @source_version = 0 @event_handler_cache = {} @pending_events = [] super end
Public Instance Methods
is_version_conflicted?(client_side_version)
click to toggle source
# File lib/ruby_cqrs/domain/aggregate.rb, line 24 def is_version_conflicted? client_side_version client_side_version != @source_version end
Private Instance Methods
apply(event)
click to toggle source
# File lib/ruby_cqrs/domain/aggregate.rb, line 107 def apply(event) dispatch_handler_for(event) self.send(:snapshot_countdown) if self.is_a? Snapshotable @version += 1 nil end
commit()
click to toggle source
# File lib/ruby_cqrs/domain/aggregate.rb, line 82 def commit @pending_events = [] @source_version = @version if self.is_a? Snapshotable and self.send :should_reset_snapshot_countdown? self.send(:reset_countdown, 0) end nil end
dispatch_handler_for(event)
click to toggle source
# File lib/ruby_cqrs/domain/aggregate.rb, line 115 def dispatch_handler_for(event) target = retrieve_handler_for(event.class) self.send(target, event) nil end
get_changes()
click to toggle source
# File lib/ruby_cqrs/domain/aggregate.rb, line 53 def get_changes return nil unless @pending_events.size > 0 changes = { :events => @pending_events.dup, :aggregate_id => @aggregate_id, :aggregate_type => self.class.name, :expecting_source_version => @source_version, :expecting_version => @pending_events.max\ { |a, b| a.version <=> b.version }.version } try_extract_snapshot_into changes changes end
load_from(state)
click to toggle source
# File lib/ruby_cqrs/domain/aggregate.rb, line 30 def load_from state sorted_events = state[:events].sort { |x, y| x.version <=> y.version } @aggregate_id = state[:aggregate_id] try_apply_snapshot_in state sorted_events.each do |event| apply(event) @source_version += 1 end nil end
raise_event(event)
click to toggle source
# File lib/ruby_cqrs/domain/aggregate.rb, line 92 def raise_event(event) apply(event) update_dispatch_detail_for(event) @pending_events << event nil end
retrieve_handler_for(event_type)
click to toggle source
# File lib/ruby_cqrs/domain/aggregate.rb, line 122 def retrieve_handler_for(event_type) @event_handler_cache[event_type] ||= begin stripped_event_type_name = event_type.to_s.demodulize.underscore "on_#{stripped_event_type_name}".to_sym end end
try_apply_snapshot_in(state)
click to toggle source
# File lib/ruby_cqrs/domain/aggregate.rb, line 42 def try_apply_snapshot_in state if state.has_key? :snapshot and self.is_a? Snapshotable self.send :apply_snapshot, state[:snapshot][:state] @version = state[:snapshot][:version] @source_version = state[:snapshot][:version] self.send(:reset_countdown, state[:events].size) end nil end
try_extract_snapshot_into(changes)
click to toggle source
# File lib/ruby_cqrs/domain/aggregate.rb, line 68 def try_extract_snapshot_into changes snapshot_state = self.send :take_a_snapshot\ if self.is_a? Snapshotable and self.send(:should_take_a_snapshot?) unless snapshot_state.nil? raise NotADomainSnapshotError unless snapshot_state.is_a? Snapshot changes[:snapshot] = { :state => snapshot_state, :state_type => snapshot_state.class.name, :version => @version } self.send :set_snapshot_taken end nil end
update_dispatch_detail_for(event)
click to toggle source
# File lib/ruby_cqrs/domain/aggregate.rb, line 100 def update_dispatch_detail_for(event) event.instance_variable_set(:@aggregate_id, @aggregate_id) event.instance_variable_set(:@version, @version) nil end