class Twobook::Event

Attributes

agreements[RW]
data[R]
entries[RW]
happened_at[R]
partial_order[R]
uuid[R]

Public Class Methods

event_name() click to toggle source
# File lib/twobook/event.rb, line 67
def self.event_name
  name.underscore.gsub("#{Twobook.configuration.accounting_namespace.underscore}/events/", '')
end
from_name(name) click to toggle source
# File lib/twobook/event.rb, line 71
def self.from_name(name)
  match = types.detect { |t| t.name =~ /#{name.camelize}$/ }
  raise "Bad event name #{name}" unless match
  match
end
has(*args) click to toggle source
# File lib/twobook/event.rb, line 81
def self.has(*args)
  @has ||= []
  return @has if args.empty?
  @has += args
end
new(happened_at: Time.current, uuid: SecureRandom.uuid, **data) click to toggle source
# File lib/twobook/event.rb, line 8
def initialize(happened_at: Time.current, uuid: SecureRandom.uuid, **data)
  @happened_at = happened_at
  @data = data
  @uuid = uuid
  @agreements = []
  @entries = []

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

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

    define_singleton_method k, -> { @data.dig(k) }
  end
  raise "Cannot initialize event #{inspect}: required #{remaining_keys_to_match}" if remaining_keys_to_match.any?
end
types() click to toggle source
# File lib/twobook/event.rb, line 77
def self.types
  Utilities.types(self)
end

Public Instance Methods

<=>(other) click to toggle source
# File lib/twobook/event.rb, line 62
def <=>(other)
  return @happened_at <=> other if other.is_a?(Time)
  [@happened_at, @partial_order || 0] <=> [other.happened_at, other.partial_order || 0]
end
==(other) click to toggle source
# File lib/twobook/event.rb, line 58
def ==(other)
  other.is_a?(Event) && @uuid == other.uuid
end
clone() click to toggle source
Calls superclass method
# File lib/twobook/event.rb, line 27
def clone
  c = super
  c.instance_variable_set(:@entries, @entries.map(&:clone))
  c.instance_variable_set(:@data, @data.deep_dup)
  c
end
fetch_agreements!() click to toggle source
# File lib/twobook/event.rb, line 34
def fetch_agreements!
  raise "I don't know how to fetch the agreements for #{@name}"
end
fetch_and_assign_agreements!() click to toggle source
# File lib/twobook/event.rb, line 38
def fetch_and_assign_agreements!
  @agreements = fetch_agreements!
  self
end
Also aliased as: load!
inspect() click to toggle source
# File lib/twobook/event.rb, line 44
def inspect
  "<#{self.class.name} @data=#{@data} @happened_at=#{@happened_at}>"
end
load!()
update_happened_at(happened_at) click to toggle source
# File lib/twobook/event.rb, line 53
def update_happened_at(happened_at)
  @happened_at = happened_at
  self
end
update_partial_order(i) click to toggle source
# File lib/twobook/event.rb, line 48
def update_partial_order(i)
  @partial_order = i
  self
end