Class: Twobook::Agreement
- Inherits:
-
Object
- Object
- Twobook::Agreement
- Defined in:
- lib/twobook/agreement.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#data ⇒ Object
readonly
Returns the value of attribute data.
Class Method Summary collapse
- .agreement_name ⇒ Object
- .from_name(name) ⇒ Object
- .handles(event_name = nil, with: nil) ⇒ Object
- .has(*args) ⇒ Object
- .types ⇒ Object
Instance Method Summary collapse
- #handlers_for(event) ⇒ Object
-
#initialize(data = {}) ⇒ Agreement
constructor
A new instance of Agreement.
- #inspect ⇒ Object
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
#data ⇒ Object (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_name ⇒ Object
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 |
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 |
#inspect ⇒ Object
37 38 39 |
# File 'lib/twobook/agreement.rb', line 37 def inspect "<#{self.class.name} @data=#{@data}>" end |