Class: Hark::Dispatcher
- Inherits:
-
Object
- Object
- Hark::Dispatcher
- Defined in:
- lib/hark/dispatcher.rb
Instance Attribute Summary collapse
-
#handlers ⇒ Object
readonly
Returns the value of attribute handlers.
Class Method Summary collapse
-
.from(*args, &block) ⇒ Object
from(:success) do “success” end.
- .to_handler(object) ⇒ Object
Instance Method Summary collapse
- #handle(method, *args, &block) ⇒ Object
- #handles?(method) ⇒ Boolean
-
#initialize(handlers) ⇒ Dispatcher
constructor
A new instance of Dispatcher.
Constructor Details
#initialize(handlers) ⇒ Dispatcher
Returns a new instance of Dispatcher.
38 39 40 41 |
# File 'lib/hark/dispatcher.rb', line 38 def initialize handlers @handlers = handlers freeze end |
Instance Attribute Details
#handlers ⇒ Object (readonly)
Returns the value of attribute handlers.
36 37 38 |
# File 'lib/hark/dispatcher.rb', line 36 def handlers @handlers end |
Class Method Details
.from(*args, &block) ⇒ Object
from(:success) do
"success"
end
from(success: ->{ "success" })
from do |on|
on.success { "success" }
end
13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/hark/dispatcher.rb', line 13 def self.from(*args, &block) if block if args.last.is_a?(Symbol) args << {args.pop => block} elsif args.empty? args << block end end new args.map{|o| to_handler(o) }.flatten.freeze end |
.to_handler(object) ⇒ Object
25 26 27 28 29 30 31 32 33 34 |
# File 'lib/hark/dispatcher.rb', line 25 def self.to_handler object case object when Listener then object.dispatcher.handlers when Dispatcher then object.handlers when Hash then AdHoc.new(object) when Proc then AdHoc.new(&object) when Array then object.map{|o| to_handler(o) } else object end end |
Instance Method Details
#handle(method, *args, &block) ⇒ Object
47 48 49 50 51 |
# File 'lib/hark/dispatcher.rb', line 47 def handle method, *args, &block handlers.each_with_object([]) do |handler, results| results << handler.send(method, *args, &block) if handler.respond_to?(method) end end |
#handles?(method) ⇒ Boolean
43 44 45 |
# File 'lib/hark/dispatcher.rb', line 43 def handles? method handlers.any? {|handler| handler.respond_to?(method) } end |