Class: NexusDomainEvents::Event

Inherits:
Object
  • Object
show all
Defined in:
lib/nexus_domain_events/event.rb

Overview

A domain event value object

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(message_name:, actor:, payload:, message_id: nil, created_at: nil) ⇒ Event

Returns a new instance of Event.

Raises:

  • (StandardError)


16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/nexus_domain_events/event.rb', line 16

def initialize(
  message_name:,
  actor:,
  payload:,
  message_id: nil,
  created_at: nil
)
  @message_name = message_name.upcase
  @actor = actor
  @payload = payload
  @message_id = message_id || SecureRandom.uuid
  @created_at = created_at || Time.now

  errors = validate
  raise StandardError.new(message: errors.join(', ')) unless errors.empty?
end

Instance Attribute Details

#actorObject (readonly)

Returns the value of attribute actor.



11
12
13
# File 'lib/nexus_domain_events/event.rb', line 11

def actor
  @actor
end

#created_atObject (readonly)

Returns the value of attribute created_at.



14
15
16
# File 'lib/nexus_domain_events/event.rb', line 14

def created_at
  @created_at
end

#message_idObject (readonly)

Returns the value of attribute message_id.



13
14
15
# File 'lib/nexus_domain_events/event.rb', line 13

def message_id
  @message_id
end

#message_nameObject (readonly)

Returns the value of attribute message_name.



10
11
12
# File 'lib/nexus_domain_events/event.rb', line 10

def message_name
  @message_name
end

#payloadObject (readonly)

Returns the value of attribute payload.



12
13
14
# File 'lib/nexus_domain_events/event.rb', line 12

def payload
  @payload
end

Class Method Details

.from_json(json) ⇒ Object



45
46
47
48
49
50
51
52
53
54
# File 'lib/nexus_domain_events/event.rb', line 45

def self.from_json(json)
  parsed = JSON.parse(json, symbolize_names: true)
  new(
    message_name: parsed[:message_name],
    actor: parsed[:actor],
    payload: parsed[:payload],
    message_id: parsed[:message_id],
    created_at: Time.rfc3339(parsed[:created_at])
  )
end

Instance Method Details

#to_json(*_args) ⇒ Object



56
57
58
59
60
61
62
63
64
# File 'lib/nexus_domain_events/event.rb', line 56

def to_json(*_args)
  JSON.generate({
    message_name: @message_name,
    actor: @actor,
    payload: @payload,
    message_id: @message_id,
    created_at: @created_at.rfc3339(9),
  })
end

#validateObject



33
34
35
36
37
38
39
40
41
42
43
# File 'lib/nexus_domain_events/event.rb', line 33

def validate
  errors = []
  errors << 'message_name cannot be blank' if @message_name.empty?
  errors << 'actor must be an Int' unless @actor.is_a?(Integer)
  errors << 'payload must be a Hash' unless @payload.is_a?(Hash)
  errors << 'message_id cannot be blank' if @message_id.empty?
  errors << 'created_at must be a Time' unless @created_at.is_a?(Time)
  # if we get this far, ensure the whole message is less than 16mb
  errors << 'event size cannot exceed 16mb' unless errors.empty? && to_json.bytesize <= 16 * 1024 * 1024
  errors
end