Module: Perennial::Dispatchable::ClassMethods

Defined in:
lib/perennial/dispatchable.rb

Instance Method Summary collapse

Instance Method Details

#handlers(recursive = true) ⇒ Object

Return an array of all registered handlers, ordered by their class and then the order of insertion. Please note that this will include ALL handlers up the inheritance chain unless false is passed as the only argument.



82
83
84
85
86
87
88
89
# File 'lib/perennial/dispatchable.rb', line 82

def handlers(recursive = true)
  handlers = []
  if recursive && superclass.respond_to?(:handlers)
    handlers += superclass.handlers(recursive)
  end
  handlers += Dispatchable.handler_mapping[self]
  return handlers
end

#handlers=(new_value) ⇒ Object

Assigns a new array of handlers and assigns each - Note that this will only set this classes handlers, it will not override those for others above / below it in the inheritance chain.



94
95
96
97
# File 'lib/perennial/dispatchable.rb', line 94

def handlers=(new_value)
  Dispatchable.handler_mapping.delete self
  [*new_value].each { |h| register_handler h }
end

#register_handler(handler) ⇒ Object

Appends a handler to the list of handlers for this object. Handlers are called in the order they are registered.



101
102
103
104
105
# File 'lib/perennial/dispatchable.rb', line 101

def register_handler(handler)
  unless handler.blank? || !handler.respond_to?(:handle)
    Dispatchable.handler_mapping[self] << handler 
  end
end