class Stenotype::Event

{Stenotype::Event} represents a triggered event

Attributes

attributes[R]
dispatcher[R]
eval_context[R]
name[R]

Public Class Methods

emit!(name, attributes = {}, eval_context: {}, dispatcher: Stenotype.config.dispatcher) click to toggle source

Delegates event to instance of {Stenotype::Event}.

@example Emit an event using class method

Stenotype::Event.emit!(data, options, eval_context)

@param {[String, Symbol]} name Event name. @param {Hash} attributes Data to be published to the targets. @param {Hash} eval_context A context having handler defined in {Stenotype::ContextHandlers}. @param dispatcher {#publish} A dispatcher object responding to [#publish]. @return {Stenotype::Event} An instance of {Stenotype::Event}

# File lib/stenotype/event.rb, line 20
def self.emit!(name, attributes = {}, eval_context: {}, dispatcher: Stenotype.config.dispatcher)
  return unless Stenotype.config.enabled

  begin
    event = new(name, attributes, eval_context: eval_context, dispatcher: dispatcher)
    event.emit!
    event
  rescue StandardError => exception
    #
    # @todo This is a temporary solution to enable conditional logger fetching
    #   needs a fix to use default Spicerack::Configuration functionality
    #
    Stenotype::Configuration.logger.error(exception)

    raise Stenotype::Error unless Stenotype.config.graceful_error_handling
  end
end
new(name, attributes = {}, eval_context: {}, dispatcher: Stenotype.config.dispatcher) click to toggle source

@example Create an event

event = Stenotype::Event.new(data, options, eval_context)

@example Create an event with custom dispatcher

event = Stenotype::Event.new(data, options, eval_context, dispatcher: MyDispatcher.new)

@param {[String, Symbol]} name Event name. @param {Hash} attributes Data to be published to the targets. @param {Hash} eval_context A context having handler defined in {Stenotype::ContextHandlers}. @param dispatcher {#publish} A dispatcher object responding to [#publish]. @return {Stenotype::Event} An instance of event

# File lib/stenotype/event.rb, line 52
def initialize(name, attributes = {}, eval_context: {}, dispatcher: Stenotype.config.dispatcher)
  @name = name
  @attributes = attributes
  @eval_context = eval_context
  @dispatcher = dispatcher.new
end

Public Instance Methods

emit!() click to toggle source

Emits a {Stenotype::Event}.

@example Emit an instance of event

event = Stenotype::Event.new('events_name', { key: :value }, eval_context: { controller: ctrl })
event.emit! #=> Publishes the event to targets
# File lib/stenotype/event.rb, line 66
def emit!
  return unless Stenotype.config.enabled

  begin
    dispatcher.publish(self)
  rescue StandardError => exception
    #
    # @todo This is a temporary solution to enable conditional logger fetching
    #   needs a fix to use default Spicerack::Configuration functionality
    #
    Stenotype::Configuration.logger.error(exception)

    raise Stenotype::Error unless Stenotype.config.graceful_error_handling
  end
end