Class: Deferral::Deferred

Inherits:
Object
  • Object
show all
Defined in:
lib/deferral/deferred.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(block) ⇒ Deferred

Returns a new instance of Deferred.



18
19
20
21
# File 'lib/deferral/deferred.rb', line 18

def initialize(block)
  @block = block
  @local_variables = Deferred.get_local_variables(block)
end

Class Method Details

.get_local_variables(block) ⇒ Object



3
4
5
6
7
8
9
# File 'lib/deferral/deferred.rb', line 3

def self.get_local_variables(block)
  vars = {}
  block.binding.local_variables.each do |name|
    vars[name] = block.binding.local_variable_get(name)
  end
  vars
end

.set_local_variables(block, vars) ⇒ Object



11
12
13
14
15
16
# File 'lib/deferral/deferred.rb', line 11

def self.set_local_variables(block, vars)
  vars.each_pair do |name, val|
    block.binding.local_variable_set(name, val)
  end
  nil
end

Instance Method Details

#callObject



23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/deferral/deferred.rb', line 23

def call
  current_vars = Deferred.get_local_variables(@block)
  begin
    Deferred.set_local_variables(@block, @local_variables)
    @block.call
  rescue Exception
    # ignore all exceptions ...
    # no way to add "suppressed" exceptions to the exception already thrown
  ensure
    Deferred.set_local_variables(@block, current_vars)
  end
end