class Twobook::Handler

Attributes

data_in_process[R]
event_in_process[R]

Public Class Methods

from_name(name) click to toggle source
# File lib/twobook/handler.rb, line 49
def self.from_name(name)
  match = types.detect { |t| t.name =~ /#{name.camelize}$/ }
  raise "Bad handler name: #{name}" unless match
  match
end
handler_name() click to toggle source
# File lib/twobook/handler.rb, line 45
def self.handler_name
  name.underscore.gsub("#{Twobook.configuration.accounting_namespace.underscore}/handlers/", '')
end
new(event:, **data) click to toggle source
# File lib/twobook/handler.rb, line 11
def initialize(event:, **data)
  raise 'Must be initialized with an event' unless event.is_a?(Event)
  @event_in_process = event
  @data_in_process = {
    **data,
    **event.data,
  }
end
types() click to toggle source
# File lib/twobook/handler.rb, line 55
def self.types
  Utilities.types(self)
end

Public Instance Methods

handle() click to toggle source
# File lib/twobook/handler.rb, line 37
def handle
  raise 'This handler needs a #handle method'
end
name() click to toggle source
# File lib/twobook/handler.rb, line 41
def name
  self.class.handler_name
end
run(accounts) click to toggle source
# File lib/twobook/handler.rb, line 20
def run(accounts)
  raise 'No event set; was this handler initialized properly?' unless @event_in_process.present?
  @accounts_in_process = accounts.map(&:clone)

  Utilities.match_params(
    method(:handle),
    {
      **@data_in_process,
      happened_at: @event_in_process.happened_at,
      event_name: @event_in_process.class.event_name,
    },
    "Cannot run handler #{self.class.handler_name} for event #{@event_in_process}",
  )

  @accounts_in_process
end