class RubyCqrs::Domain::AggregateRepository

Public Class Methods

new(event_store, command_context) click to toggle source
# File lib/ruby_cqrs/domain/aggregate_repository.rb, line 18
def initialize event_store, command_context
  raise ArgumentError unless event_store.is_a? Data::EventStore
  @event_store = event_store
  @command_context = command_context
end

Public Instance Methods

find_by(aggregate_id) click to toggle source
# File lib/ruby_cqrs/domain/aggregate_repository.rb, line 25
def find_by aggregate_id
  state = @event_store.load_by(aggregate_id, @command_context)
  raise AggregateNotFoundError if (state.nil? or state[:aggregate_type].nil? or\
                              ((state[:events].nil? or state[:events].empty?) and state[:snapshot].nil?))

  create_instance_from state
end
save(one_aggregate) click to toggle source
# File lib/ruby_cqrs/domain/aggregate_repository.rb, line 34
def save one_aggregate
  delegate_persistence_of [ one_aggregate ]
end

Private Instance Methods

create_instance_from(state) click to toggle source
# File lib/ruby_cqrs/domain/aggregate_repository.rb, line 46
def create_instance_from state
  try_decode_serialized_from state
  instance = state[:aggregate_type].constantize.new
  instance.send(:load_from, state)
  instance
end
decode_event_from(event_record) click to toggle source
# File lib/ruby_cqrs/domain/aggregate_repository.rb, line 107
def decode_event_from event_record
  decoded_event = try_decode event_record[:event_type], event_record[:data]
  decoded_event.instance_variable_set(:@aggregate_id, event_record[:aggregate_id])
  decoded_event.instance_variable_set(:@version, event_record[:version])
  decoded_event
end
decode_snapshot_state_from(snapshot_record) click to toggle source
# File lib/ruby_cqrs/domain/aggregate_repository.rb, line 102
def decode_snapshot_state_from snapshot_record
  snapshot_state = try_decode snapshot_record[:state_type], snapshot_record[:data]
  { :state => snapshot_state, :version => snapshot_record[:version] }
end
delegate_persistence_of(aggregates) click to toggle source
# File lib/ruby_cqrs/domain/aggregate_repository.rb, line 54
def delegate_persistence_of aggregates
  verify_uniqueness_of aggregates

  changes = prep_changes_for(aggregates)
  if changes.size > 0
    @event_store.save changes, @command_context
    aggregates.each do |aggregate|
      aggregate.send(:commit)
    end
  end

  nil
end
encode_data_from(obj) click to toggle source
# File lib/ruby_cqrs/domain/aggregate_repository.rb, line 136
def encode_data_from obj
  data = obj
  data = data.try_encode if data.is_a? RubyCqrs::Data::Encodable
  data
end
prep_changes_for(aggregates) click to toggle source
# File lib/ruby_cqrs/domain/aggregate_repository.rb, line 78
def prep_changes_for aggregates
  to_return = []
  aggregates.inject(to_return) do |product, aggregate|
    raise ArgumentError unless aggregate.is_a? Aggregate
    aggregate_change = aggregate.send(:get_changes)
    next if aggregate_change.nil?
    try_encode_serializable_in aggregate_change
    product << aggregate_change
  end
  to_return
end
try_decode_serialized_from(state) click to toggle source
# File lib/ruby_cqrs/domain/aggregate_repository.rb, line 92
def try_decode_serialized_from state
  state[:snapshot] = decode_snapshot_state_from state[:snapshot]\
    if state.has_key? :snapshot

  state[:events] = state[:events].map { |event_record| decode_event_from event_record }\
    if state[:events].size > 0

  nil
end
try_encode_serializable_in(change) click to toggle source
# File lib/ruby_cqrs/domain/aggregate_repository.rb, line 116
def try_encode_serializable_in change
  if change.has_key? :snapshot
    encoded_snapshot = encode_data_from change[:snapshot][:state]
    change[:snapshot] = { :state_type => change[:snapshot][:state_type],
                          :version => change[:snapshot][:version],
                          :data => encoded_snapshot }
  end

  if change[:events].size > 0
    change[:events].map! { |event|
      { :data => encode_data_from(event),
        :aggregate_id => event.aggregate_id,
        :event_type => event.class.name,
        :version => event.version }
    }
  end

  nil
end
verify_uniqueness_of(aggregates) click to toggle source
# File lib/ruby_cqrs/domain/aggregate_repository.rb, line 69
def verify_uniqueness_of aggregates
  uniq_array =  aggregates.uniq { |aggregate| aggregate.aggregate_id }
  raise AggregateDuplicationError unless uniq_array.size == aggregates.size
  nil
end