Class: Affect::Fiber::Context
- Inherits:
-
Object
- Object
- Affect::Fiber::Context
- Defined in:
- lib/affect/fiber.rb
Instance Attribute Summary collapse
-
#handlers ⇒ Object
readonly
Returns the value of attribute handlers.
Instance Method Summary collapse
- #call_handler(handler, effect, *args) ⇒ Object
- #capture(&block) ⇒ Object
- #find_handler(effect) ⇒ Object
- #handle(effect, *args) ⇒ Object
- #handler_proc ⇒ Object
-
#initialize(handlers = nil, &block) ⇒ Context
constructor
A new instance of Context.
Constructor Details
#initialize(handlers = nil, &block) ⇒ Context
Returns a new instance of Context.
66 67 68 |
# File 'lib/affect/fiber.rb', line 66 def initialize(handlers = nil, &block) @handlers = handlers || { nil => block || -> { } } end |
Instance Attribute Details
#handlers ⇒ Object (readonly)
Returns the value of attribute handlers.
70 71 72 |
# File 'lib/affect/fiber.rb', line 70 def handlers @handlers end |
Instance Method Details
#call_handler(handler, effect, *args) ⇒ Object
93 94 95 96 97 98 99 100 101 |
# File 'lib/affect/fiber.rb', line 93 def call_handler(handler, effect, *args) if handler.arity == 0 handler.call elsif args.empty? handler.call(effect) else handler.call(*args) end end |
#capture(&block) ⇒ Object
103 104 105 |
# File 'lib/affect/fiber.rb', line 103 def capture(&block) Affect.capture(block, handler_proc) end |
#find_handler(effect) ⇒ Object
89 90 91 |
# File 'lib/affect/fiber.rb', line 89 def find_handler(effect) @handlers[effect] || @handlers[effect.class] || @handlers[nil] end |
#handle(effect, *args) ⇒ Object
76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/affect/fiber.rb', line 76 def handle(effect, *args) handler = find_handler(effect) if handler call_handler(handler, effect, *args) else begin Fiber.yield Intent.new(effect, *args) rescue FiberError raise RuntimeError, "No handler found for #{effect.inspect}" end end end |
#handler_proc ⇒ Object
72 73 74 |
# File 'lib/affect/fiber.rb', line 72 def handler_proc proc { |effect, *args| handle(effect, *args) } end |