Class: EventLoggerRails::Event
- Inherits:
-
Object
- Object
- EventLoggerRails::Event
- Defined in:
- lib/event_logger_rails/event.rb
Overview
Models an event for logging.
Instance Attribute Summary collapse
-
#description ⇒ Object
readonly
Returns the value of attribute description.
-
#identifier ⇒ Object
readonly
Returns the value of attribute identifier.
-
#level ⇒ Object
readonly
Returns the value of attribute level.
Instance Method Summary collapse
-
#==(other) ⇒ Boolean
Determines if the event is equivalent to the given object through string comparison.
-
#initialize(provided_identifier) ⇒ Event
constructor
Initializes the event using the provided identifier to determine its properties from either the default registration (for default events) or the user-defined registry.
-
#merge ⇒ Hash
Converts the event into a hash and merges the given hash into it.
-
#to_hash ⇒ Hash
Returns a hash representation of the event.
-
#to_s ⇒ String
Returns a string representation of the event.
-
#valid? ⇒ Boolean
Determines if the event is valid.
-
#validate! {|self| ... } ⇒ Object
Validates the event and yields it to the given block.
Constructor Details
#initialize(provided_identifier) ⇒ Event
Initializes the event using the provided identifier to determine its properties from either the default registration (for default events) or the user-defined registry.
39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/event_logger_rails/event.rb', line 39 def initialize(provided_identifier) @provided_identifier = provided_identifier.to_s # Attempt to find default registration for event if (default_event = DEFAULT_EVENTS[@provided_identifier]) default_registration = [@provided_identifier, *default_event&.values] end # Fallback to user-defined registration if default not found. # Deconstruct registration to set identifier, description, and level attributes. @identifier, @description, @level = default_registration || config_registration end |
Instance Attribute Details
#description ⇒ Object (readonly)
Returns the value of attribute description.
29 30 31 |
# File 'lib/event_logger_rails/event.rb', line 29 def description @description end |
#identifier ⇒ Object (readonly)
Returns the value of attribute identifier.
25 26 27 |
# File 'lib/event_logger_rails/event.rb', line 25 def identifier @identifier end |
#level ⇒ Object (readonly)
Returns the value of attribute level.
33 34 35 |
# File 'lib/event_logger_rails/event.rb', line 33 def level @level end |
Instance Method Details
#==(other) ⇒ Boolean
Determines if the event is equivalent to the given object through string comparison.
131 132 133 |
# File 'lib/event_logger_rails/event.rb', line 131 def ==(other) to_s == other.to_s end |
#merge ⇒ Hash
Converts the event into a hash and merges the given hash into it.
64 65 66 |
# File 'lib/event_logger_rails/event.rb', line 64 def merge(...) to_hash.merge(...) end |
#to_hash ⇒ Hash
Returns a hash representation of the event.
106 107 108 109 110 111 |
# File 'lib/event_logger_rails/event.rb', line 106 def to_hash { event_identifier: identifier, event_description: description } end |
#to_s ⇒ String
Returns a string representation of the event. The provided identifier is returned if the event is not registered.
120 121 122 |
# File 'lib/event_logger_rails/event.rb', line 120 def to_s identifier&.to_s || provided_identifier.to_s end |
#valid? ⇒ Boolean
Determines if the event is valid.
76 77 78 |
# File 'lib/event_logger_rails/event.rb', line 76 def valid? identifier.present? end |
#validate! {|self| ... } ⇒ Object
This only validates the event registration. Logger level is validated at the time of logging.
Validates the event and yields it to the given block.
90 91 92 93 94 |
# File 'lib/event_logger_rails/event.rb', line 90 def validate! raise Exceptions::UnregisteredEvent.new(unregistered_event: self) unless valid? yield(self) end |