class SandthornSequelProjection::EventHandler

Attributes

filter[R]
message[R]
projection[R]

Public Class Methods

new(options) click to toggle source
# File lib/sandthorn_sequel_projection/event_handler.rb, line 6
def initialize(options)
  @filter = SandthornEventFilter::Filter.new
  parse_options(options)
end

Public Instance Methods

handle(target, event) click to toggle source
# File lib/sandthorn_sequel_projection/event_handler.rb, line 11
def handle(target, event)
  if filter.match?(event)
    call_handler(target, event)
  end
end

Private Instance Methods

call_handler(target, event) click to toggle source
# File lib/sandthorn_sequel_projection/event_handler.rb, line 29
def call_handler(target, event)
  target.send(message, event)
end
construct_filter(options) click to toggle source
# File lib/sandthorn_sequel_projection/event_handler.rb, line 33
def construct_filter(options)
  types, event_names = extract_filter_options(options)
  @filter = @filter.extract(types: types) if types.any?
  @filter = @filter.extract(events: event_names) if types.any?
end
extract_filter_options(options) click to toggle source
# File lib/sandthorn_sequel_projection/event_handler.rb, line 39
def extract_filter_options(options)
  types   = Array.wrap(options[:aggregate_type] || options[:aggregate_types])
  events  = Array.wrap(options[:event_name] || options[:event_names])
  [types, events]
end
parse_options(options) click to toggle source
# File lib/sandthorn_sequel_projection/event_handler.rb, line 19
def parse_options(options)
  if options.is_a? Symbol
    set_method(options)
  elsif options.is_a? Hash
    method_name = options.keys.first
    set_method(method_name)
    construct_filter(options[method_name])
  end
end
set_method(message) click to toggle source
# File lib/sandthorn_sequel_projection/event_handler.rb, line 45
def set_method(message)
  @message = message
end