Upgraded Rails and RSpec
[monkeycharger.git] / vendor / plugins / rspec / rspec_on_rails / lib / spec / rails / example / ivar_proxy.rb
blobbf9b790b2ed706852c435c7b80aefe89b511e9d7
1 ##
2 # A wrapper that allows instance variables to be manipulated using +[]+ and
3 # +[]=+
5 module Spec
6   module Rails
7     module Example
8       class IvarProxy #:nodoc:
10         ##
11         # Wraps +object+ allowing its instance variables to be manipulated.
13         def initialize(object)
14           @object = object
15         end
17         ##
18         # Retrieves +ivar+ from the wrapped object.
20         def [](ivar)
21           get_variable "@#{ivar}"
22         end
24         ##
25         # Sets +ivar+ to +val+ on the wrapped object.
27         def []=(ivar, val)
28           set_variable "@#{ivar}", val
29         end
31         def each
32           @object.instance_variables.each do |variable_full_name|
33             variable_name = variable_full_name[1...variable_full_name.length]
34             yield variable_name, get_variable(variable_full_name)
35           end
36         end
38         def delete(key)
39           var_name = "@#{key}"
40           if @object.instance_variables.include?(var_name)
41             @object.send(:remove_instance_variable, var_name)
42           else
43             return nil
44           end
45         end
47         def has_key?(key)
48           @object.instance_variables.include?("@#{key}")
49         end
51         protected
52         def get_variable(name)
53           @object.instance_variable_get name
54         end
56         def set_variable(name, value)
57           @object.instance_variable_set name, value
58         end
59       end
60     end
61   end
62 end