Class: Twobook::Event

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/twobook/event.rb

Direct Known Subclasses

Corrections::CorrectionMade

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(happened_at: Time.current, uuid: SecureRandom.uuid, **data) ⇒ Event



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# 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

Instance Attribute Details

#agreementsObject

Returns the value of attribute agreements.



6
7
8
# File 'lib/twobook/event.rb', line 6

def agreements
  @agreements
end

#dataObject (readonly)

Returns the value of attribute data.



5
6
7
# File 'lib/twobook/event.rb', line 5

def data
  @data
end

#entriesObject

Returns the value of attribute entries.



6
7
8
# File 'lib/twobook/event.rb', line 6

def entries
  @entries
end

#happened_atObject (readonly)

Returns the value of attribute happened_at.



5
6
7
# File 'lib/twobook/event.rb', line 5

def happened_at
  @happened_at
end

#partial_orderObject (readonly)

Returns the value of attribute partial_order.



5
6
7
# File 'lib/twobook/event.rb', line 5

def partial_order
  @partial_order
end

#uuidObject (readonly)

Returns the value of attribute uuid.



5
6
7
# File 'lib/twobook/event.rb', line 5

def uuid
  @uuid
end

Class Method Details

.event_nameObject



67
68
69
# File 'lib/twobook/event.rb', line 67

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

.from_name(name) ⇒ Object



71
72
73
74
75
# 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) ⇒ Object



81
82
83
84
85
# File 'lib/twobook/event.rb', line 81

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

.typesObject



77
78
79
# File 'lib/twobook/event.rb', line 77

def self.types
  Utilities.types(self)
end

Instance Method Details

#<=>(other) ⇒ Object



62
63
64
65
# 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) ⇒ Object



58
59
60
# File 'lib/twobook/event.rb', line 58

def ==(other)
  other.is_a?(Event) && @uuid == other.uuid
end

#cloneObject



27
28
29
30
31
32
# 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!Object



34
35
36
# 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!Object Also known as: load!



38
39
40
41
# File 'lib/twobook/event.rb', line 38

def fetch_and_assign_agreements!
  @agreements = fetch_agreements!
  self
end

#inspectObject



44
45
46
# File 'lib/twobook/event.rb', line 44

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

#update_happened_at(happened_at) ⇒ Object



53
54
55
56
# File 'lib/twobook/event.rb', line 53

def update_happened_at(happened_at)
  @happened_at = happened_at
  self
end

#update_partial_order(i) ⇒ Object



48
49
50
51
# File 'lib/twobook/event.rb', line 48

def update_partial_order(i)
  @partial_order = i
  self
end