Module: ActiveProjection::ProjectionType

Extended by:
ActiveSupport::Concern
Defined in:
lib/active_projection/projection_type.rb

Defined Under Namespace

Classes: WrongArgumentsCountError

Instance Method Summary collapse

Instance Method Details

#evaluate(headers) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# 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
      set_broken
      LOGGER.error "[#{self.class.name}]: #{event_id - last_id} events are missing"
      false
  end
end

#initializeObject



10
11
12
13
14
15
16
17
18
19
20
# 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
    raise WrongArgumentsCountError if 2 < method.arity or method.arity < 1
    event_type = ProjectionType.method_name_to_event_type method_name
    handlers[event_type] << [method_name, method.arity]
  end
end

#invoke(event, headers) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# 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
        self.send method, event
      else
        self.send method, event, headers
      end
    rescue Exception => e
      LOGGER.error "[#{self.class.name}]: error processing #{event_type}[#{event_id}]\n#{e.message}\n#{e.backtrace}"
      set_broken
      raise
    end
  end
  update_last_id event_id
  LOGGER.debug "[#{self.class.name}]: successfully processed #{event_type}[#{event_id}]"
end