Module: HasGuardedHandlers
- Defined in:
- lib/has_guarded_handlers.rb,
lib/has_guarded_handlers/version.rb
Constant Summary collapse
- VERSION =
"0.0.1"
Instance Method Summary collapse
-
#clear_handlers(type, *guards) ⇒ Object
Clear handlers with given guards.
- #initialize(*args) ⇒ Object
-
#register_handler(type, *guards) {|Object| ... } ⇒ Object
Register a handler.
- #trigger_handler(type, event) ⇒ Object
Instance Method Details
#clear_handlers(type, *guards) ⇒ Object
Clear handlers with given guards
23 24 25 |
# File 'lib/has_guarded_handlers.rb', line 23 def clear_handlers(type, *guards) @handlers[type].delete_if { |g, _| g == guards } end |
#initialize(*args) ⇒ Object
4 5 6 |
# File 'lib/has_guarded_handlers.rb', line 4 def initialize(*args) @handlers = {} end |
#register_handler(type, *guards) {|Object| ... } ⇒ Object
Register a handler
13 14 15 16 17 |
# File 'lib/has_guarded_handlers.rb', line 13 def register_handler(type, *guards, &handler) check_guards guards @handlers[type] ||= [] @handlers[type] << [guards, handler] end |
#trigger_handler(type, event) ⇒ Object
27 28 29 30 31 32 33 34 |
# File 'lib/has_guarded_handlers.rb', line 27 def trigger_handler(type, event) return unless handler = @handlers[type] catch :halt do handler.find do |guards, handler| catch(:pass) { call_handler handler, guards, event } end end end |