changelog shortlog tags changeset manifest revisions annotate raw

vendor/plugins/rspec_on_rails/lib/spec/rails/example/ivar_proxy.rb

changeset 16: 01fd3f10ae84
author: moriq@moriq.com
date: Mon Mar 10 10:13:18 2008 +0900 (16 years ago)
permissions: -rw-r--r--
description: add plugins rspec_on_rails
1##
2# A wrapper that allows instance variables to be manipulated using +[]+ and
3# +[]=+
4
5module Spec
6 module Rails
7 module Example
8 class IvarProxy #:nodoc:
9
10 ##
11 # Wraps +object+ allowing its instance variables to be manipulated.
12
13 def initialize(object)
14 @object = object
15 end
16
17 ##
18 # Retrieves +ivar+ from the wrapped object.
19
20 def [](ivar)
21 get_variable "@#{ivar}"
22 end
23
24 ##
25 # Sets +ivar+ to +val+ on the wrapped object.
26
27 def []=(ivar, val)
28 set_variable "@#{ivar}", val
29 end
30
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
37
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
46
47 def has_key?(key)
48 @object.instance_variables.include?("@#{key}")
49 end
50
51 protected
52 def get_variable(name)
53 @object.instance_variable_get name
54 end
55
56 def set_variable(name, value)
57 @object.instance_variable_set name, value
58 end
59 end
60 end
61 end
62end