Class: Stenotype::ContextHandlers::Base Abstract

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

Overview

This class is abstract.

An abstract base class for implementing contexts handlers

Examples:

Defining a custom Handler

class MyCustomHandler < Stenotype::ContextHandlers::Base
  self.context_name = :custom_handler

  def as_json(*_args)
    {
      value1: context.value1,
      value2: context.value2
    }
  end
end

Direct Known Subclasses

Klass, Rails::ActiveJob, Rails::Controller

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(context, options: {}) ⇒ #as_json

Returns A context handler implementing [#as_json].

Parameters:

  • context (Object)

    A context where the event was emitted

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

    A hash of additional options



37
38
39
40
# File 'lib/stenotype/context_handlers/base.rb', line 37

def initialize(context, options: {})
  @context = context
  @options = options
end

Class Attribute Details

.handler_nameSymbol

Returns Name of the handler.

Returns:

  • (Symbol)

    Name of the handler

Raises:

  • (NotImplementedError)

    in case handler name is not specified.



61
62
63
# File 'lib/stenotype/context_handlers/base.rb', line 61

def handler_name
  @handler_name || raise(NotImplementedError, "Please, specify the handler_name of #{self}")
end

Instance Attribute Details

#contextObject (readonly)

A context in which the event was emitted

Returns:

  • (Object)

    the current value of context



24
25
26
# File 'lib/stenotype/context_handlers/base.rb', line 24

def context
  @context
end

#optionsHash (readonly)

A hash of additional options

Returns:

  • (Hash)

    the current value of options



24
25
26
# File 'lib/stenotype/context_handlers/base.rb', line 24

def options
  @options
end

Class Method Details

.inherited(subklass) ⇒ Object



27
28
29
# File 'lib/stenotype/context_handlers/base.rb', line 27

def self.inherited(subklass)
  ContextHandlers.register(subklass)
end

Instance Method Details

#as_json(*_args) ⇒ Object

This method is abstract.

Raises:

  • (NotImplementedError)

    subclasses must implement this method



46
47
48
# File 'lib/stenotype/context_handlers/base.rb', line 46

def as_json(*_args)
  raise NotImplementedError, "#{self} must implement method ##{__method__}"
end