module ActiveProjection::ProjectionType

Attributes

handlers[RW]
projection_id[W]

Public Class Methods

new() click to toggle source
# File lib/active_projection/projection_type.rb, line 10
def initialize
  self.handlers = Hash.new do |hash, key|
    hash[key] = []
  end
  self.class.public_instance_methods(false).each do |method_name|
    method = self.class.instance_method method_name
    fail WrongArgumentsCountError if 2 < method.arity || method.arity < 1
    event_type = ProjectionType.method_name_to_event_type method_name
    handlers[event_type] << [method_name, method.arity]
  end
end

Private Class Methods

method_name_to_event_type(method_name) click to toggle source
# File lib/active_projection/projection_type.rb, line 67
def self.method_name_to_event_type(method_name)
  method_name.to_s.gsub('__', '/').camelcase.to_sym
end

Public Instance Methods

evaluate(headers) click to toggle source
# File lib/active_projection/projection_type.rb, line 22
def evaluate(headers)
  unless solid?
    LOGGER.error "[#{self.class.name}] is broken"
    return false
  end
  last_id = fetch_last_id
  event_id = headers[:id]
  case
  when last_id + 1 == event_id
    true
  when last_id >= event_id
    LOGGER.debug "[#{self.class.name}]: event #{event_id} already processed"
    false
  when last_id < event_id
    mark_broken
    LOGGER.error "[#{self.class.name}]: #{event_id - last_id} events are missing"
    false
  end
end
invoke(event, headers) click to toggle source
# File lib/active_projection/projection_type.rb, line 42
def invoke(event, headers)
  event_id = headers[:id]
  event_type = event.class.name.to_sym
  handlers[event_type].each do |method, arity|
    begin
      if 1 == arity
        send method, event
      else
        send method, event, headers
      end
    rescue => e
      LOGGER.error "[#{self.class.name}]: error processing #{event_type}[#{event_id}]\n#{e.message}\n#{e.backtrace}"
      mark_broken
      raise
    end
  end
  update_last_id event_id
  LOGGER.debug "[#{self.class.name}]: successfully processed #{event_type}[#{event_id}]"
end

Private Instance Methods

fetch_last_id() click to toggle source
# File lib/active_projection/projection_type.rb, line 79
def fetch_last_id
  ProjectionRepository.last_id projection_id
end
mark_broken() click to toggle source
# File lib/active_projection/projection_type.rb, line 83
def mark_broken
  ProjectionRepository.mark_broken projection_id
end
projection_id() click to toggle source
# File lib/active_projection/projection_type.rb, line 71
def projection_id
  @projection_id ||= ProjectionRepository.ensure_exists(self.class.name).id
end
solid?() click to toggle source
# File lib/active_projection/projection_type.rb, line 75
def solid?
  ProjectionRepository.solid? projection_id
end
update_last_id(id) click to toggle source
# File lib/active_projection/projection_type.rb, line 87
def update_last_id(id)
  ProjectionRepository.set_last_id projection_id, id
end