Class: Twobook::Agreement

Inherits:
Object
  • Object
show all
Defined in:
lib/twobook/agreement.rb

Direct Known Subclasses

Corrections::Correction

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data = {}) ⇒ Agreement

Returns a new instance of Agreement.



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/twobook/agreement.rb', line 5

def initialize(data = {})
  @data = data

  remaining_keys_to_match = self.class.has.deep_dup
  @data.each do |k, v|
    raise "Cannot initialize agreement #{inspect}: unexpected parameter #{k}" if remaining_keys_to_match.delete(k).nil?
    raise "Cannot initialize agreement #{inspect}: #{k} is nil" if v.nil?
    define_singleton_method k, -> { @data.dig(k) }

    @data[k] = Twobook.wrap_number(v) if v.is_a?(Numeric)
  end

  if remaining_keys_to_match.any?
    raise "Cannot initialize agreement #{inspect}: required #{remaining_keys_to_match}"
  end
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



3
4
5
# File 'lib/twobook/agreement.rb', line 3

def data
  @data
end

Class Method Details

.agreement_nameObject



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

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

.from_name(name) ⇒ Object



66
67
68
69
70
71
72
# File 'lib/twobook/agreement.rb', line 66

def self.from_name(name)
  match = types.detect do |t|
    t.name =~ /#{name.camelize}$/
  end
  raise "Bad agreement name: #{name}" unless match
  match
end

.handles(event_name = nil, with: nil) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/twobook/agreement.rb', line 45

def self.handles(event_name = nil, with: nil)
  @handles ||= {}
  if event_name.nil?
    # We're trying to get the handler for an event. Check all the names map to valid classes:
    @handles.transform_values { |handler_names| handler_names.map { |name| Handler.from_name(name) } }
  end
  return @handles if event_name.nil?

  if @handles[event_name].present?
    raise "Duplicate handler: more than one handler defined for #{event_name} on #{name}"
  end

  @handles[event_name] = with.is_a?(Array) ? with : [with]
end

.has(*args) ⇒ Object



60
61
62
63
64
# File 'lib/twobook/agreement.rb', line 60

def self.has(*args)
  @has ||= []
  return @has if args.empty?
  @has += args
end

.typesObject



74
75
76
# File 'lib/twobook/agreement.rb', line 74

def self.types
  Utilities.types(self)
end

Instance Method Details

#handlers_for(event) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/twobook/agreement.rb', line 22

def handlers_for(event)
  handler_names = self.class.handles[event.class.event_name]

  if handler_names.nil? || handler_names.none?
    raise "Missing handler: #{inspect} cannot handle #{event.class.event_name}"
  end

  handler_names.map do |handler_name|
    Handler.from_name(handler_name).new(
      event: event,
      **@data,
    )
  end
end

#inspectObject



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

def inspect
  "<#{self.class.name} @data=#{@data}>"
end