class Realm::EventHandler

Attributes

event_namespace[R]
trigger_mapping[R]

Public Class Methods

bind_runtime(runtime) click to toggle source
# File lib/realm/event_handler.rb, line 26
def bind_runtime(runtime)
  RuntimeBound.new(self, runtime)
end
call(event, runtime:) click to toggle source
# File lib/realm/event_handler.rb, line 30
def call(event, runtime:)
  new(runtime: runtime).(event)
end
event_types() click to toggle source
# File lib/realm/event_handler.rb, line 41
def event_types
  defined?(@trigger_mapping) ? @trigger_mapping.keys.uniq : []
end
identifier(value = :not_provided) click to toggle source
# File lib/realm/event_handler.rb, line 34
def identifier(value = :not_provided)
  @identifier = value unless value == :not_provided
  return @identifier if defined?(@identifier)

  @identifier = name.gsub(/(Domain|(::)?(Event)?Handlers?)/, '').underscore.gsub(%r{/+}, '-')
end
new(runtime: nil) click to toggle source
# File lib/realm/event_handler.rb, line 72
def initialize(runtime: nil)
  @runtime = runtime
end

Protected Class Methods

method_added(method_name) click to toggle source
Calls superclass method
# File lib/realm/event_handler.rb, line 60
def method_added(method_name)
  super
  return unless defined?(@method_triggers)

  @trigger_mapping ||= {}
  @method_triggers.each do |trigger|
    (@trigger_mapping[trigger.to_sym] ||= []) << method_name
  end
  remove_instance_variable(:@method_triggers)
end
namespace(value) click to toggle source
# File lib/realm/event_handler.rb, line 47
def namespace(value)
  @event_namespace = value
end
on(*triggers, run: nil, **options, &block) click to toggle source
# File lib/realm/event_handler.rb, line 51
def on(*triggers, run: nil, **options, &block)
  @method_triggers = triggers
  @method_trigger_options = options # TODO: store and pass to gateway
  return unless run || block

  block = ->(event) { self.run(run, event.body) } if run
  define_method("handle_#{triggers.join('_or_')}", &block)
end

Public Instance Methods

call(event) click to toggle source
# File lib/realm/event_handler.rb, line 76
def call(event)
  event_to_methods(event).each do |method_name|
    send(method_name, event)
  rescue Realm::Persistence::Conflict => e
    context[:logger]&.warn(e.full_message)
  end
end

Private Instance Methods

event_to_methods(event) click to toggle source
# File lib/realm/event_handler.rb, line 90
def event_to_methods(event)
  self.class.trigger_mapping.fetch_values(event.type.to_sym, :any) { [] }.flatten
end