Module: Vigilem::Core::EventHandler

Extended by:
ActiveSupport::Concern, Forwardable, Utils
Includes:
Utils
Defined in:
lib/vigilem/core/event_handler.rb

Defined Under Namespace

Modules: ClassMethods, Utils

Instance Method Summary collapse

Methods included from Utils

on_format?, respond_to?, snakecase_class_name, type_from_on_format

Instance Method Details

#default_handler(&block) ⇒ Object

Parameters:

  • (Proc)

Returns:



150
151
152
# File 'lib/vigilem/core/event_handler.rb', line 150

def default_handler(&block)
  self.class.default_handler(&block)
end

#handle(event, *args, &block) ⇒ Object

Returns results of the Proc registered to the type_handless.

Parameters:

  • event,

    the event to handle

  • (Array)

Returns:

  • results of the Proc registered to the type_handless



129
130
131
132
133
# File 'lib/vigilem/core/event_handler.rb', line 129

def handle(event, *args, &block)
  if handler = type_handles[event.class]
    handler.call(event, *args, &block)
  end
end

#handle!(event, *args, &block) ⇒ Object

Returns results of the Proc registered to the type_handless.

Parameters:

  • event,

    the event to handle

  • (Array)

Returns:

  • results of the Proc registered to the type_handless



139
140
141
142
143
144
145
# File 'lib/vigilem/core/event_handler.rb', line 139

def handle!(event, *args, &block)
  if ret = handle(event, *args, &block)
    ret
  else
    raise NotImplementedError, "No handler found for #{key.class}:#{key},#{block} and no default handler set"
  end
end

#on(type, &block) ⇒ Proc Also known as: register_handle

to trigger the handling, either use handle(event, opts={}) or handle_#type configures how this event handler handles an event

Parameters:

  • type (#to_s)

Returns:

  • (Proc)

    the block given/registered



117
118
119
120
121
122
# File 'lib/vigilem/core/event_handler.rb', line 117

def on(type, &block)
  define_singleton_method(on_method_name = :"on_#{class_name = snakecase_class_name(type)}") {|&blk| type_handles[type] = blk }
  ret = send on_method_name, &block
  define_singleton_method(handler_name = :"handle_#{class_name}", &type_handles[type])
  ret
end

#type_handlesHash

Returns:

  • (Hash)


109
110
111
# File 'lib/vigilem/core/event_handler.rb', line 109

def type_handles
  self.class.type_handles
end