Method: CodeEvents#upon
- Defined in:
- lib/source/redshift/code_events.rb
#upon(sym_or_hash, unignorable, &block) ⇒ Object
call-seq:
obj.upon(sym, unignorable = false) { |arg,...| block } -> obj
obj.upon(hash) -> obj
Stores a number of Proc objects to be called when the event sym is fired.
The first form adds block to the array of Proc objects executed when the event sym is fired, then returns obj. If unignorable is set to true, the block will be executed when sym fires, regardless of whether it has been ignored.
obj.upon(:A, true) { puts '1st executed' } #=> obj
obj.upon(:A, true) { puts '2nd executed' } #=> obj
obj.upon(:A) { puts '3rd executed' } #=> obj
obj.ignore(:A) #=> obj
obj.fire(:A) #=> obj
produces:
1st executed
2nd executed
The second form takes a hash of Proc objects keyed by event name, running obj.upon(name, &proc) on each key-value pair, then returns obj.
proc_1 = Proc.new { puts '1st executed' } #=> #<Proc:0x3e78ee>
proc_2 = Proc.new { puts '2nd executed' } #=> #<Proc:0x3e888a>
obj.upon(:A => proc_1, :B => proc_2) #=> obj
obj.fire(:B) #=> obj
produces
2nd executed
183 184 185 186 187 188 189 190 191 192 193 194 195 |
# File 'lib/source/redshift/code_events.rb', line 183 def upon(sym_or_hash, unignorable, &block) if sym_or_hash.instance_of?(Hash) sym_or_hash.each {|name,proc| self.upon(name, &proc) } return self else name = sym_or_hash.to_sym @code_events ||= {} @code_events[name] ||= [] @code_events[name] << block `block.__block__.__unignorable__=typeof(unignorable)=='function'?false:unignorable` return self end end |