Class: Stenotype::Event

Inherits:
Object
  • Object
show all
Defined in:
lib/stenotype/event.rb

Overview

Event represents a triggered event

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, attributes = {}, eval_context: {}, dispatcher: Stenotype.config.dispatcher) ⇒ Stenotype::Event

Returns An instance of event.

Examples:

Create an event

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

Create an event with custom dispatcher

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

Parameters:

  • name ([String, Symbol])

    Event name.

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

    Data to be published to the targets.

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

    A context having handler defined in ContextHandlers.

  • dispatcher (#publish) (defaults to: Stenotype.config.dispatcher)

    A dispatcher object responding to [#publish].



52
53
54
55
56
57
# 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

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes.



38
39
40
# File 'lib/stenotype/event.rb', line 38

def attributes
  @attributes
end

#dispatcherObject (readonly)

Returns the value of attribute dispatcher.



38
39
40
# File 'lib/stenotype/event.rb', line 38

def dispatcher
  @dispatcher
end

#eval_contextObject (readonly)

Returns the value of attribute eval_context.



38
39
40
# File 'lib/stenotype/event.rb', line 38

def eval_context
  @eval_context
end

#nameObject (readonly)

Returns the value of attribute name.



38
39
40
# File 'lib/stenotype/event.rb', line 38

def name
  @name
end

Class Method Details

.emit!(name, attributes = {}, eval_context: {}, dispatcher: Stenotype.config.dispatcher) ⇒ Stenotype::Event

Delegates event to instance of Stenotype::Event.

Examples:

Emit an event using class method

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

Parameters:

  • name ([String, Symbol])

    Event name.

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

    Data to be published to the targets.

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

    A context having handler defined in ContextHandlers.

  • dispatcher (#publish) (defaults to: Stenotype.config.dispatcher)

    A dispatcher object responding to [#publish].

Returns:



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# 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

Instance Method Details

#emit!Object

Emits a Stenotype::Event.

Examples:

Emit an instance of event

event = Stenotype::Event.new('events_name', { key: :value }, eval_context: { controller: ctrl })
event.emit! #=> Publishes the event to targets


66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# 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