Class: Realm::Event

Inherits:
Dry::Struct
  • Object
show all
Defined in:
lib/realm/event.rb

Constant Summary collapse

T =
Realm::Types

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.new(attributes = {}) ⇒ Object



24
25
26
27
28
29
30
31
32
33
# File 'lib/realm/event.rb', line 24

def new(attributes = {})
  head = {
    id: SecureRandom.uuid,
    correlation_id: SecureRandom.uuid,
    triggered_at: Time.now,
    version: 1, # until we need breaking change (anything except adding attribute) all events are version 1
  }.merge(attributes.fetch(:head, {}))
  body = attributes[:body] || attributes.except(:head)
  super({ head: head }.merge(body.empty? ? {} : { body: body }))
end

.type(value = :not_provided) ⇒ Object



35
36
37
38
# File 'lib/realm/event.rb', line 35

def type(value = :not_provided)
  @type = value unless value == :not_provided
  @type ||= name.demodulize.sub('Event', '').underscore
end

Instance Method Details

#to_json(*args) ⇒ Object



51
52
53
# File 'lib/realm/event.rb', line 51

def to_json(*args)
  JSON.generate(to_h, *args)
end

#typeObject



47
48
49
# File 'lib/realm/event.rb', line 47

def type
  self.class.type
end