Module: IIRC::Hooks
- Included in:
- Bot
- Defined in:
- lib/iirc/modules/hooks.rb
Instance Method Summary collapse
- #>>(line) ⇒ Object
- #call(action, *args, **kwargs) ⇒ Object
- #fire!(verb, *args, **kwargs) ⇒ Object
- #hook(action = nil, &blk) ⇒ Object
- #hook_seq(verb) ⇒ Object protected
- #hooks ⇒ Object
- #off(verb, action = nil, &blk) ⇒ Object
- #on(verb = nil, action = nil, &blk) ⇒ Object
- #run ⇒ Object
Instance Method Details
#>>(line) ⇒ Object
18 19 20 21 |
# File 'lib/iirc/modules/hooks.rb', line 18 def >>(line) evt = parse(line) fire! evt.verb, evt end |
#call(action, *args, **kwargs) ⇒ Object
57 58 59 60 61 62 63 64 65 |
# File 'lib/iirc/modules/hooks.rb', line 57 def call action, *args, **kwargs case action when Symbol args = method(action).arity.then { |n| n.negative? ? args : args[...n] } send(action, *args, **kwargs) else action.call(*args, **kwargs) end end |
#fire!(verb, *args, **kwargs) ⇒ Object
49 50 51 52 53 54 55 |
# File 'lib/iirc/modules/hooks.rb', line 49 def fire! verb, *args, **kwargs for action in hook_seq(verb) call action, *args, **kwargs end rescue StopIteration StopIteration end |
#hook(action = nil, &blk) ⇒ Object
45 46 47 |
# File 'lib/iirc/modules/hooks.rb', line 45 def hook action=nil, &blk on nil, action, &blk end |
#hook_seq(verb) ⇒ Object (protected)
68 69 70 |
# File 'lib/iirc/modules/hooks.rb', line 68 def hook_seq(verb) [*hooks[nil], (:"on_#{verb}" if respond_to?(:"on_#{verb}")), *hooks[verb]].compact end |
#hooks ⇒ Object
3 4 5 |
# File 'lib/iirc/modules/hooks.rb', line 3 def hooks @hooks ||= Hash.new { |h,v| h[v] = Set.new } end |
#off(verb, action = nil, &blk) ⇒ Object
36 37 38 39 40 41 42 43 |
# File 'lib/iirc/modules/hooks.rb', line 36 def off verb, action=nil, &blk action ||= blk if action hooks[verb].delete action else hooks[verb].clear end end |
#on(verb = nil, action = nil, &blk) ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/iirc/modules/hooks.rb', line 23 def on verb=nil, action=nil, &blk if action and blk define_singleton_method(action, blk) singleton_class.send :private, action end if action or blk hooks[verb] << (action || blk) else hooks[verb] end end |
#run ⇒ Object
7 8 9 10 11 12 13 14 15 16 |
# File 'lib/iirc/modules/hooks.rb', line 7 def run lines { |line| begin self >> line rescue Exception => ex puts ex. puts ex.backtrace end } end |