Class: DelayedInvocation

Inherits:
Object
  • Object
show all
Defined in:
lib/probably/delayed_invocation.rb

Instance Method Summary collapse

Constructor Details

#initialize(target) ⇒ DelayedInvocation

Returns a new instance of DelayedInvocation.



2
3
4
5
6
7
# File 'lib/probably/delayed_invocation.rb', line 2

def initialize(target)
  @target = target
  @methods = []
  @args = []
  @blocks = []
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object



9
10
11
12
13
14
# File 'lib/probably/delayed_invocation.rb', line 9

def method_missing(name, *args, &block)
  @methods << name
  @args << args
  @blocks << (block.nil? ? nil : block)
  self
end

Instance Method Details

#or(value) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/probably/delayed_invocation.rb', line 16

def or(value)
  @methods.reduce(@target) do |t, m|
    block = @blocks.shift
    unless block.nil?
      t.__send__(m, *(@args.shift), &block)
    else
      t.__send__(m, *(@args.shift))
    end
  end
rescue
  value
end