Class: EventSystem::Event

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type, source = nil, data = {}, id = nil, timestamp = nil) ⇒ Event

Initialize a new event

Parameters:

  • type (String, Symbol)

    The event type identifier

  • source (Object) (defaults to: nil)

    The source/originator of the event

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

    Additional event data

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

    Optional event ID, generated if not provided

  • timestamp (Time, String, nil) (defaults to: nil)

    Optional timestamp, current time if not provided



19
20
21
22
23
24
25
# File 'lib/event_system/event.rb', line 19

def initialize(type, source = nil, data = {}, id = nil, timestamp = nil)
  @id = id || SecureRandom.uuid
  @type = type.to_s
  @source = source
  @data = data
  @timestamp = parse_timestamp(timestamp)
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



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

def data
  @data
end

#idObject (readonly)

Returns the value of attribute id.



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

def id
  @id
end

#sourceObject (readonly)

Returns the value of attribute source.



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

def source
  @source
end

#timestampObject (readonly)

Returns the value of attribute timestamp.



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

def timestamp
  @timestamp
end

#typeObject (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

Parameters:

  • json (String)

    JSON representation of an event

Returns:

  • (Event)

    Reconstructed event



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)

Parameters:

  • other (Event)

    The other event to compare

Returns:

  • (Boolean)

    True if events have the same ID



69
70
71
# File 'lib/event_system/event.rb', line 69

def ==(other)
  other.is_a?(Event) && @id == other.id
end

#hashInteger

Hash code for the event (based on ID)

Returns:

  • (Integer)

    Hash code



75
76
77
# File 'lib/event_system/event.rb', line 75

def hash
  @id.hash
end

#source_to_stringString

Convert source to string representation

Returns:

  • (String)

    String representation of the source



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_hHash

Hash representation of the event

Returns:

  • (Hash)

    Event data as a hash



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: @timestamp.iso8601(3),
    data: safe_serialize(@data)
  }
end

#to_json(*_args) ⇒ String

JSON representation of the event

Returns:

  • (String)

    Event data as a JSON string



47
48
49
# File 'lib/event_system/event.rb', line 47

def to_json(*_args)
  to_h.to_json
end

#to_sString

String representation of the event

Returns:

  • (String)

    Human-readable event string



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

Parameters:

  • event_type (String, Symbol)

    The event type to check

Returns:

  • (Boolean)

    True if the event matches the type



82
83
84
# File 'lib/event_system/event.rb', line 82

def type?(event_type)
  @type == event_type.to_s
end