class Stenotype::ContextHandlers::Base
An abstract base class for implementing contexts handlers
@abstract @attr_reader {Object} context A context in which the event was emitted @attr_reader {Hash} options A hash of additional options
@example 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
Attributes
handler_name[W]
Handler name by which it will be registered in {Stenotype::ContextHandlers::Collection}
context[R]
options[R]
Public Class Methods
handler_name()
click to toggle source
@return {Symbol} Name of the handler @raise {NotImplementedError} in case handler name is not specified.
# File lib/stenotype/context_handlers/base.rb, line 61 def handler_name @handler_name || raise(NotImplementedError, "Please, specify the handler_name of #{self}") end
inherited(subklass)
click to toggle source
# File lib/stenotype/context_handlers/base.rb, line 27 def self.inherited(subklass) ContextHandlers.register(subklass) end
new(context, options: {})
click to toggle source
@param context {Object} A context where the event was emitted @param options {Hash} A hash of additional options
@return {#as_json} A context handler implementing [#as_json]
# File lib/stenotype/context_handlers/base.rb, line 37 def initialize(context, options: {}) @context = context @options = options end
Private Class Methods
before_remove_const()
click to toggle source
A rails specific workaround to make reloading in dev env happy
# File lib/stenotype/context_handlers/base.rb, line 71 def self.before_remove_const Stenotype::ContextHandlers.known.unregister(self) end
Public Instance Methods
as_json(*_args)
click to toggle source
@abstract @raise {NotImplementedError} subclasses must implement this method
# File lib/stenotype/context_handlers/base.rb, line 46 def as_json(*_args) raise NotImplementedError, "#{self} must implement method ##{__method__}" end