Class: Context::ExecutionProxy
Overview
A Proxy for carrying on conditional execution.
Instance Method Summary collapse
-
#execute_if_you_can(&block) ⇒ Object
Conditionally execute the block.
-
#initialize(object, cond) ⇒ ExecutionProxy
constructor
Set the true object on which to execute and the condition to execute.
-
#method_missing(symbol) ⇒ Object
Conditionally evaluate the symbol.
Constructor Details
#initialize(object, cond) ⇒ ExecutionProxy
Set the true object on which to execute and the condition to execute. NOTE: The condition is a bool. In other words it has already been evaluated before we get to this point.
8 9 10 11 |
# File 'lib/Context/ExecutionProxy.rb', line 8 def initialize(object, cond) @object = object @cond = cond end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(symbol) ⇒ Object
Conditionally evaluate the symbol. If the condition is true, evaluate the symbol. Otherwise return the true object.
27 28 29 30 31 32 33 |
# File 'lib/Context/ExecutionProxy.rb', line 27 def method_missing(symbol) if @cond @object.instance_eval(symbol.to_s) else @object end end |
Instance Method Details
#execute_if_you_can(&block) ⇒ Object
Conditionally execute the block. If the condition is true, execute the block. Otherwise return the true object. NOTE: Crazy name is so that you don’t have something with the same name and accidently call it with method_missing.
17 18 19 20 21 22 23 |
# File 'lib/Context/ExecutionProxy.rb', line 17 def execute_if_you_can(&block) if @cond @object.instance_eval(&block) else @object end end |