Class: Fortitude::Support::InstanceVariableSet

Inherits:
Object
  • Object
show all
Defined in:
lib/fortitude/support/instance_variable_set.rb

Instance Method Summary collapse

Constructor Details

#initialize(target_object) ⇒ InstanceVariableSet

Returns a new instance of InstanceVariableSet.


4
5
6
# File 'lib/fortitude/support/instance_variable_set.rb', line 4

def initialize(target_object)
  @target_object = target_object
end

Instance Method Details

#[](name) ⇒ Object


8
9
10
# File 'lib/fortitude/support/instance_variable_set.rb', line 8

def [](name)
  target_object.instance_variable_get("@#{name}")
end

#[]=(name, value) ⇒ Object


12
13
14
# File 'lib/fortitude/support/instance_variable_set.rb', line 12

def []=(name, value)
  target_object.instance_variable_set("@#{name}", value)
end

#eachObject


22
23
24
# File 'lib/fortitude/support/instance_variable_set.rb', line 22

def each
  keys.each { |k| yield k, self[k] }
end

#keysObject


16
17
18
19
20
# File 'lib/fortitude/support/instance_variable_set.rb', line 16

def keys
  target_object.instance_variables.map do |instance_variable_name|
    $1.to_sym if instance_variable_name.to_s =~ /^@(.*)$/
  end.compact
end

#with_instance_variable_copying(widget) ⇒ Object


26
27
28
29
30
31
32
33
34
35
36
# File 'lib/fortitude/support/instance_variable_set.rb', line 26

def with_instance_variable_copying(widget)
  before_copy = widget.instance_variables
  skip_copy = before_copy - target_object.instance_variables

  copy_to_widget(widget)
  begin
    yield
  ensure
    copy_from_widget(widget, skip_copy)
  end
end