Class: Twobook::Handler

Inherits:
Object
  • Object
show all
Includes:
BookingHelpers, QueryHelpers
Defined in:
lib/twobook/handler.rb,
lib/twobook/handler/query_helpers.rb,
lib/twobook/handler/booking_helpers.rb

Defined Under Namespace

Modules: BookingHelpers, QueryHelpers

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from BookingHelpers

#add_account, #book, #credit, #debit, #entry, #record

Methods included from QueryHelpers

#account_requirements, #accounts, #existing, #labelled_account_requirements, #many, #method_missing, #one, #respond_to_missing?, #satisfy_requirement, #where

Constructor Details

#initialize(event:, **data) ⇒ Handler

Returns a new instance of Handler.



11
12
13
14
15
16
17
18
# 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

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Twobook::Handler::QueryHelpers

Instance Attribute Details

#data_in_processObject (readonly)

Returns the value of attribute data_in_process.



9
10
11
# File 'lib/twobook/handler.rb', line 9

def data_in_process
  @data_in_process
end

#event_in_processObject (readonly)

Returns the value of attribute event_in_process.



9
10
11
# File 'lib/twobook/handler.rb', line 9

def event_in_process
  @event_in_process
end

Class Method Details

.from_name(name) ⇒ Object



49
50
51
52
53
# 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_nameObject



45
46
47
# File 'lib/twobook/handler.rb', line 45

def self.handler_name
  name.underscore.gsub("#{Twobook.configuration.accounting_namespace.underscore}/handlers/", '')
end

.typesObject



55
56
57
# File 'lib/twobook/handler.rb', line 55

def self.types
  Utilities.types(self)
end

Instance Method Details

#handleObject



37
38
39
# File 'lib/twobook/handler.rb', line 37

def handle
  raise 'This handler needs a #handle method'
end

#nameObject



41
42
43
# File 'lib/twobook/handler.rb', line 41

def name
  self.class.handler_name
end

#run(accounts) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# 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