Class: Events::Base Abstract

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

Overview

This class is abstract.

Subclass and implement #type

Base class for all Anima events. Subclasses must implement #type returning a string identifier (e.g. “user_message”).

Events are POROs — they carry typed payloads through the event bus. Persistence is a separate concern handled by ActiveRecord models.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(content:, session_id: nil) ⇒ Base

Returns a new instance of Base.

Parameters:

  • content (String)

    event payload content

  • session_id (String, nil) (defaults to: nil)

    optional session identifier



16
17
18
19
20
# File 'lib/events/base.rb', line 16

def initialize(content:, session_id: nil)
  @content = content
  @session_id = session_id
  @timestamp = Process.clock_gettime(Process::CLOCK_REALTIME, :nanosecond)
end

Instance Attribute Details

#contentObject (readonly)

Returns the value of attribute content.



12
13
14
# File 'lib/events/base.rb', line 12

def content
  @content
end

#session_idObject (readonly)

Returns the value of attribute session_id.



12
13
14
# File 'lib/events/base.rb', line 12

def session_id
  @session_id
end

#timestampObject (readonly)

Returns the value of attribute timestamp.



12
13
14
# File 'lib/events/base.rb', line 12

def timestamp
  @timestamp
end

Instance Method Details

#event_nameString

Returns namespaced event name for Rails.event (e.g. “anima.user_message”).

Returns:

  • (String)

    namespaced event name for Rails.event (e.g. “anima.user_message”)



29
30
31
# File 'lib/events/base.rb', line 29

def event_name
  "#{Bus::NAMESPACE}.#{type}"
end

#to_hHash

Returns serialized event payload.

Returns:

  • (Hash)

    serialized event payload



34
35
36
# File 'lib/events/base.rb', line 34

def to_h
  {type: type, content: content, session_id: session_id, timestamp: timestamp}
end

#typeString

Returns event type identifier.

Returns:

  • (String)

    event type identifier

Raises:

  • (NotImplementedError)

    if subclass does not implement



24
25
26
# File 'lib/events/base.rb', line 24

def type
  raise NotImplementedError, "#{self.class} must implement #type"
end