Class: EventSystem::EventManager
- Inherits:
-
Object
- Object
- EventSystem::EventManager
- Defined in:
- lib/event_system/event_manager.rb
Overview
Central event management class that handles event publication and subscription Provides a complete event-driven architecture with pluggable storage backends
Instance Attribute Summary collapse
-
#config ⇒ Object
readonly
Returns the value of attribute config.
-
#storage ⇒ Object
readonly
Returns the value of attribute storage.
Instance Method Summary collapse
-
#close ⇒ void
Close the event manager and release resources.
-
#create_session(session_id = nil) ⇒ String
Create a new session.
-
#current_session ⇒ String?
Get the current session ID.
-
#initialize(config = nil) ⇒ EventManager
constructor
Initialize a new event manager.
-
#publish(event) ⇒ void
Publish an event to all subscribers.
-
#publish_event(type, source = nil, data = {}) ⇒ EventSystem::Event
Create and publish an event in one step.
-
#query_events(options = {}) ⇒ Array<EventSystem::Event>
Query for events based on options.
-
#ready? ⇒ Boolean
Check if the event manager is ready for use.
-
#stats ⇒ Hash
Get statistics about the event system.
-
#subscribe(event_type, subscriber) ⇒ void
Subscribe to events of a specific type.
-
#subscribe_all(subscriber) ⇒ void
Subscribe to all event types.
-
#switch_session(session_id) ⇒ void
Switch to a different session.
-
#unsubscribe(event_type, subscriber) ⇒ void
Unsubscribe from events of a specific type.
-
#unsubscribe_all(subscriber) ⇒ void
Unsubscribe from all event types.
Constructor Details
#initialize(config = nil) ⇒ EventManager
Initialize a new event manager
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/event_system/event_manager.rb', line 18 def initialize(config = nil) @config = case config when Configuration config when Hash Configuration.new.tap do |c| c.storage_type = config[:storage_type] || :memory c. = config[:storage_options] || {} c.logger = config[:logger] c.session_id = config[:session_id] c.auto_flush = config[:auto_flush] != false end else Configuration.new end @subscribers = Hash.new { |h, k| h[k] = [] } @storage = @config.create_storage @logger = @config.logger @logger.info("EventSystem initialized with #{@config.storage_type} storage") end |
Instance Attribute Details
#config ⇒ Object (readonly)
Returns the value of attribute config.
14 15 16 |
# File 'lib/event_system/event_manager.rb', line 14 def config @config end |
#storage ⇒ Object (readonly)
Returns the value of attribute storage.
14 15 16 |
# File 'lib/event_system/event_manager.rb', line 14 def storage @storage end |
Instance Method Details
#close ⇒ void
This method returns an undefined value.
Close the event manager and release resources
147 148 149 150 |
# File 'lib/event_system/event_manager.rb', line 147 def close @storage&.close @logger.info("EventSystem closed") end |
#create_session(session_id = nil) ⇒ String
Create a new session
129 130 131 132 133 |
# File 'lib/event_system/event_manager.rb', line 129 def create_session(session_id = nil) session_id = @storage&.create_session(session_id) || session_id || Time.now.strftime("%Y%m%d_%H%M%S") @logger.info("Created new session: #{session_id}") session_id end |
#current_session ⇒ String?
Get the current session ID
114 115 116 |
# File 'lib/event_system/event_manager.rb', line 114 def current_session @storage&.current_session end |
#publish(event) ⇒ void
This method returns an undefined value.
Publish an event to all subscribers
80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/event_system/event_manager.rb', line 80 def publish(event) @logger.debug("Publishing event: #{event}") # Store the event if storage is configured @storage&.store(event) # Deliver to type-specific subscribers deliver_to_subscribers(event.type, event) # Deliver to universal subscribers deliver_to_subscribers(:all, event) end |
#publish_event(type, source = nil, data = {}) ⇒ EventSystem::Event
Create and publish an event in one step
98 99 100 101 102 103 |
# File 'lib/event_system/event_manager.rb', line 98 def publish_event(type, source = nil, data = {}) @logger.debug("Publishing event: #{type}, source: #{source}, data: #{data}") event = Event.new(type, source, data) publish(event) event end |
#query_events(options = {}) ⇒ Array<EventSystem::Event>
Query for events based on options
108 109 110 |
# File 'lib/event_system/event_manager.rb', line 108 def query_events( = {}) @storage&.query() || [] end |
#ready? ⇒ Boolean
Check if the event manager is ready for use
154 155 156 |
# File 'lib/event_system/event_manager.rb', line 154 def ready? @storage&.available? || false end |
#stats ⇒ Hash
Get statistics about the event system
137 138 139 140 141 142 143 |
# File 'lib/event_system/event_manager.rb', line 137 def stats { subscribers: @subscribers.transform_values(&:length), storage: @storage&.stats || {}, config: @config.to_h } end |
#subscribe(event_type, subscriber) ⇒ void
This method returns an undefined value.
Subscribe to events of a specific type
45 46 47 48 49 |
# File 'lib/event_system/event_manager.rb', line 45 def subscribe(event_type, subscriber) event_type = event_type.to_s @subscribers[event_type] << subscriber @logger.debug("Subscribed #{subscriber.class} to #{event_type}") end |
#subscribe_all(subscriber) ⇒ void
This method returns an undefined value.
Subscribe to all event types
64 65 66 67 |
# File 'lib/event_system/event_manager.rb', line 64 def subscribe_all(subscriber) @subscribers[:all] << subscriber @logger.debug("Subscribed #{subscriber.class} to all events") end |
#switch_session(session_id) ⇒ void
This method returns an undefined value.
Switch to a different session
121 122 123 124 |
# File 'lib/event_system/event_manager.rb', line 121 def switch_session(session_id) @storage&.switch_session(session_id) @logger.info("Switched to session: #{session_id}") end |
#unsubscribe(event_type, subscriber) ⇒ void
This method returns an undefined value.
Unsubscribe from events of a specific type
55 56 57 58 59 |
# File 'lib/event_system/event_manager.rb', line 55 def unsubscribe(event_type, subscriber) event_type = event_type.to_s @subscribers[event_type].delete(subscriber) @logger.debug("Unsubscribed #{subscriber.class} from #{event_type}") end |
#unsubscribe_all(subscriber) ⇒ void
This method returns an undefined value.
Unsubscribe from all event types
72 73 74 75 |
# File 'lib/event_system/event_manager.rb', line 72 def unsubscribe_all(subscriber) @subscribers[:all].delete(subscriber) @logger.debug("Unsubscribed #{subscriber.class} from all events") end |