class Affect::Context
Implements an effects context
Attributes
handlers[R]
Public Class Methods
current()
click to toggle source
# File lib/affect.rb, line 45 def self.current @@current end
new(handlers = nil, &block)
click to toggle source
# File lib/affect.rb, line 9 def initialize(handlers = nil, &block) @handlers = handlers || { nil => block || -> {} } end
Public Instance Methods
call_handler(handler, effect, *args, &block)
click to toggle source
# File lib/affect.rb, line 34 def call_handler(handler, effect, *args, &block) if handler.arity.zero? handler.call(&block) elsif args.empty? handler.call(effect, &block) else handler.call(*args, &block) end end
capture() { || ... }
click to toggle source
# File lib/affect.rb, line 49 def capture @parent, @@current = @@current, self catch(:escape) { yield } ensure @@current = @parent end
escape(value = nil) { |: value)| ... }
click to toggle source
# File lib/affect.rb, line 56 def escape(value = nil) throw :escape, (block_given? ? yield : value) end
find_handler(effect)
click to toggle source
# File lib/affect.rb, line 30 def find_handler(effect) @handlers[effect] || @handlers[effect.class] || @handlers[nil] end
handler_proc()
click to toggle source
# File lib/affect.rb, line 15 def handler_proc proc { |effect, *args| handle(effect, *args) } end
perform(effect, *args, &block)
click to toggle source
# File lib/affect.rb, line 19 def perform(effect, *args, &block) handler = find_handler(effect) if handler call_handler(handler, effect, *args, &block) elsif @parent @parent.perform(effect, *args, &block) else raise "No handler found for #{effect.inspect}" end end