Class: EventSystem::Event
- Inherits:
-
Object
- Object
- EventSystem::Event
- Defined in:
- lib/event_system/event.rb
Overview
Base class for all events in the system Provides unique ID generation, timestamp handling, and serialization
Instance Attribute Summary collapse
-
#data ⇒ Object
readonly
Returns the value of attribute data.
-
#id ⇒ Object
readonly
Returns the value of attribute id.
-
#source ⇒ Object
readonly
Returns the value of attribute source.
-
#timestamp ⇒ Object
readonly
Returns the value of attribute timestamp.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
Class Method Summary collapse
-
.from_json(json) ⇒ Event
Create an event from its JSON representation.
Instance Method Summary collapse
-
#==(other) ⇒ Boolean
Check if two events are equal (by ID).
-
#hash ⇒ Integer
Hash code for the event (based on ID).
-
#initialize(type, source = nil, data = {}, id = nil, timestamp = nil) ⇒ Event
constructor
Initialize a new event.
-
#source_to_string ⇒ String
Convert source to string representation.
-
#to_h ⇒ Hash
Hash representation of the event.
-
#to_json(*_args) ⇒ String
JSON representation of the event.
-
#to_s ⇒ String
String representation of the event.
-
#type?(event_type) ⇒ Boolean
Check if the event is of a specific type.
Constructor Details
#initialize(type, source = nil, data = {}, id = nil, timestamp = nil) ⇒ Event
Initialize a new event
19 20 21 22 23 24 25 |
# File 'lib/event_system/event.rb', line 19 def initialize(type, source = nil, data = {}, id = nil, = nil) @id = id || SecureRandom.uuid @type = type.to_s @source = source @data = data = () end |
Instance Attribute Details
#data ⇒ Object (readonly)
Returns the value of attribute data.
11 12 13 |
# File 'lib/event_system/event.rb', line 11 def data @data end |
#id ⇒ Object (readonly)
Returns the value of attribute id.
11 12 13 |
# File 'lib/event_system/event.rb', line 11 def id @id end |
#source ⇒ Object (readonly)
Returns the value of attribute source.
11 12 13 |
# File 'lib/event_system/event.rb', line 11 def source @source end |
#timestamp ⇒ Object (readonly)
Returns the value of attribute timestamp.
11 12 13 |
# File 'lib/event_system/event.rb', line 11 def end |
#type ⇒ Object (readonly)
Returns the value of attribute type.
11 12 13 |
# File 'lib/event_system/event.rb', line 11 def type @type end |
Class Method Details
.from_json(json) ⇒ Event
Create an event from its JSON representation
54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/event_system/event.rb', line 54 def self.from_json(json) data = JSON.parse(json, symbolize_names: true) new( data[:type], data[:source], data[:data] || {}, data[:id], data[:timestamp] ) end |
Instance Method Details
#==(other) ⇒ Boolean
Check if two events are equal (by ID)
69 70 71 |
# File 'lib/event_system/event.rb', line 69 def ==(other) other.is_a?(Event) && @id == other.id end |
#hash ⇒ Integer
Hash code for the event (based on ID)
75 76 77 |
# File 'lib/event_system/event.rb', line 75 def hash @id.hash end |
#source_to_string ⇒ String
Convert source to string representation
88 89 90 91 92 93 94 95 96 97 |
# File 'lib/event_system/event.rb', line 88 def source_to_string case @source when nil 'unknown' when String @source else @source.to_s end end |
#to_h ⇒ Hash
Hash representation of the event
35 36 37 38 39 40 41 42 43 |
# File 'lib/event_system/event.rb', line 35 def to_h { id: @id, type: @type, source: source_to_string, timestamp: .iso8601(3), data: safe_serialize(@data) } end |
#to_json(*_args) ⇒ String
JSON representation of the event
47 48 49 |
# File 'lib/event_system/event.rb', line 47 def to_json(*_args) to_h.to_json end |
#to_s ⇒ String
String representation of the event
29 30 31 |
# File 'lib/event_system/event.rb', line 29 def to_s "[#{@timestamp}] #{@type}: #{@data.inspect}" end |
#type?(event_type) ⇒ Boolean
Check if the event is of a specific type
82 83 84 |
# File 'lib/event_system/event.rb', line 82 def type?(event_type) @type == event_type.to_s end |