Class: NexusDomainEvents::Event
- Inherits:
-
Object
- Object
- NexusDomainEvents::Event
- Defined in:
- lib/nexus_domain_events/event.rb
Overview
A domain event value object
Instance Attribute Summary collapse
-
#actor ⇒ Object
readonly
Returns the value of attribute actor.
-
#created_at ⇒ Object
readonly
Returns the value of attribute created_at.
-
#message_id ⇒ Object
readonly
Returns the value of attribute message_id.
-
#message_name ⇒ Object
readonly
Returns the value of attribute message_name.
-
#payload ⇒ Object
readonly
Returns the value of attribute payload.
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(message_name:, actor:, payload:, message_id: nil, created_at: nil) ⇒ Event
constructor
A new instance of Event.
- #to_json(*_args) ⇒ Object
- #validate ⇒ Object
Constructor Details
#initialize(message_name:, actor:, payload:, message_id: nil, created_at: nil) ⇒ Event
Returns a new instance of Event.
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 ) = .upcase @actor = actor @payload = payload = || SecureRandom.uuid @created_at = created_at || Time.now errors = validate raise StandardError.new(message: errors.join(', ')) unless errors.empty? end |
Instance Attribute Details
#actor ⇒ Object (readonly)
Returns the value of attribute actor.
11 12 13 |
# File 'lib/nexus_domain_events/event.rb', line 11 def actor @actor end |
#created_at ⇒ Object (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_id ⇒ Object (readonly)
Returns the value of attribute message_id.
13 14 15 |
# File 'lib/nexus_domain_events/event.rb', line 13 def end |
#message_name ⇒ Object (readonly)
Returns the value of attribute message_name.
10 11 12 |
# File 'lib/nexus_domain_events/event.rb', line 10 def end |
#payload ⇒ Object (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: , actor: @actor, payload: @payload, message_id: , created_at: @created_at.rfc3339(9), }) end |
#validate ⇒ Object
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 .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 .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 |