Class: Easyop::Events::Event

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

Overview

An immutable domain event value object.

Examples:

event = Easyop::Events::Event.new(
  name:    "order.placed",
  payload: { order_id: 42, total: 9900 },
  source:  "PlaceOrder"
)
event.name     # => "order.placed"
event.payload  # => { order_id: 42, total: 9900 }
event.frozen?  # => true

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name:, payload: {}, metadata: {}, timestamp: nil, source: nil) ⇒ Event

Returns a new instance of Event.

Parameters:

  • name (String)

    event name, e.g. “order.placed”

  • payload (Hash) (defaults to: {})

    domain data extracted from ctx

  • metadata (Hash) (defaults to: {})

    extra envelope data (correlation_id, etc.)

  • timestamp (Time) (defaults to: nil)

    defaults to Time.now

  • source (String) (defaults to: nil)

    class name of the emitting operation



24
25
26
27
28
29
30
31
# File 'lib/easyop/events/event.rb', line 24

def initialize(name:, payload: {}, metadata: {}, timestamp: nil, source: nil)
  @name      = name.to_s.freeze
  @payload   = payload.freeze
  @metadata  = .freeze
  @timestamp = (timestamp || Time.now).freeze
  @source    = source&.freeze
  freeze
end

Instance Attribute Details

#metadataObject (readonly)

Returns the value of attribute metadata.



17
18
19
# File 'lib/easyop/events/event.rb', line 17

def 
  @metadata
end

#nameObject (readonly)

Returns the value of attribute name.



17
18
19
# File 'lib/easyop/events/event.rb', line 17

def name
  @name
end

#payloadObject (readonly)

Returns the value of attribute payload.



17
18
19
# File 'lib/easyop/events/event.rb', line 17

def payload
  @payload
end

#sourceObject (readonly)

Returns the value of attribute source.



17
18
19
# File 'lib/easyop/events/event.rb', line 17

def source
  @source
end

#timestampObject (readonly)

Returns the value of attribute timestamp.



17
18
19
# File 'lib/easyop/events/event.rb', line 17

def timestamp
  @timestamp
end

Instance Method Details

#inspectObject



39
40
41
# File 'lib/easyop/events/event.rb', line 39

def inspect
  "#<Easyop::Events::Event name=#{@name.inspect} source=#{@source.inspect}>"
end

#to_hHash

Returns serializable hash representation.

Returns:

  • (Hash)

    serializable hash representation



34
35
36
37
# File 'lib/easyop/events/event.rb', line 34

def to_h
  { name: @name, payload: @payload, metadata: @metadata,
    timestamp: @timestamp, source: @source }
end