Class: Object

Inherits:
BasicObject
Defined in:
lib/Context/ExecutionProxy.rb

Instance Method Summary collapse

Instance Method Details

#execute_if(cond, &block) ⇒ Object

Execute the block if the condition is true. Or if there is no block, then return the proxy object. Usually this is so you can say something like:

  • object.execute_if(condition).property

If the condition is false, execution of the block, or evaluation of the property will return the original object.



45
46
47
48
49
50
51
52
53
# File 'lib/Context/ExecutionProxy.rb', line 45

def execute_if(cond, &block)
    proxy = Context::ExecutionProxy.new(self, cond)
    # Note: Wouldn't it be nice if the following could be replaced by
    # proxy.execute_if(!block.nil) do execute_if_you_can(&block) end
    proxy_if_block_exists = Context::ExecutionProxy.new(proxy, !block.nil?)
    proxy_if_block_exists.execute_if_you_can() do 
        execute_if_you_can(&block) 
    end
end

#unless_nil(&block) ⇒ Object

If the object is not nil, execute the block (or evaluate the property). Otherwise return nil.



57
58
59
# File 'lib/Context/ExecutionProxy.rb', line 57

def unless_nil(&block)
    execute_if(!nil?, &block)
end