fxri 0.3.2 → 0.3.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -16,218 +16,218 @@
16
16
  #
17
17
  # ac.close
18
18
  #
19
- # After closing,
19
+ # After closing,
20
20
  #
21
21
  # ac.widnow.name = "UberSuperDuper"
22
22
  #
23
23
  # You get the usual NoMethodError, because 'widnow' does not exist.
24
24
  #
25
25
  class Recursive_Open_Struct
26
- # Create a new Recursive_Open_Struct.
27
- def initialize
28
- @methods = Hash.new
29
- @open = true
30
- end
31
-
32
- # automatically add parameters
33
- def method_missing(method, *params) # :nodoc:
34
- # setting or getting?
35
- is_setting = !params.empty?
36
-
37
- key = method.id2name
38
- # remove trailing =
39
- key.chop! if is_setting
40
-
41
- # if structure is closed, disable hierarchy creation
42
- super unless @methods.has_key?(key) || @open
43
-
44
- if is_setting
45
- # assigning a new value
46
- if @methods[key].class == Recursive_Open_Struct
47
- raise TypeError, "overwriting previously created hierarchy entry '#{key}' not allowed", caller(1)
48
- end
49
- @methods[key] = *params
50
- else
51
- # no param: create new Recursive_Open_Struct object, if nothing is set.
52
- unless @methods.has_key?(key)
53
- @methods[key] = Recursive_Open_Struct.new
54
- end
55
- end
56
- @methods[key]
57
- end
58
-
59
- # An alternative way to access the value of an attribute
60
- # s = Recursive_Open_Struct.new
61
- # s.name = "Hugo"
62
- # s["name"] # "Hugo"
63
- def [](key)
64
- @methods[key]
65
- end
66
-
67
- # An alternative way to set the value of an attribute
68
- # s = Recursive_Open_Struct.new
69
- # s["name"] = "Hugo"
70
- # s.name # "Hugo"
71
- def []=(key, value)
72
- self.send((key+"=").to_sym, value)
73
- end
74
-
75
- # call-seq:
76
- # attrs() -> an_array
77
- #
78
- # Return a sorted array of attribute names, similar to #methods.
79
- #
80
- # s = Recursive_Open_Struct.new
81
- # s.name = "martinus"
82
- # s.age = 25
83
- # s.attrs # returns ["age", "name"]
84
- def attrs
85
- @methods.keys.sort
86
- end
87
-
88
- # After calling #close, no further modification of the configuration hierarchy
89
- # is allowed. This is not as strict as #freeze, because you are still allowed
90
- # to modify data.
91
- #
92
- # s = Recursive_Open_Struct.new
93
- # s.name = "Hugo"
94
- # s.close
95
- # s.name = "martinus" # does still work
96
- # s.age = 25 # raises NoMethodError
97
- def close
98
- do_set_open_status(false)
99
- end
100
-
101
- # Reopens a Recursive_Open_Struct which was closed with #close earlier.
102
- # After this call, it is possible to modify the structure again.
103
- def re_open
104
- do_set_open_status(true)
105
- end
106
-
107
- # call-seq:
108
- # open?() -> boolean
109
- #
110
- # Return whether the structure is still open or not.
111
- #
112
- # s = Recursive_Open_Struct.new
113
- # s.open? # returns true
114
- # s.close
115
- # s.open? # returns false
116
- def open?
117
- @open
118
- end
119
-
120
- # call-seq:
121
- # each() { |elem| ... }
122
- #
123
- # Iterates through all elements of the Recursive_Open_Struct in alphabetic order.
124
- def each
125
- attrs.each do |attr|
126
- yield @methods[attr]
127
- end
128
- end
129
-
130
- protected
131
-
132
- def do_set_open_status(status)
133
- @methods.each_value do |val|
134
- val.do_set_open_status(status) if val.class == Recursive_Open_Struct
135
- end
136
- @open = status
137
- end
26
+ # Create a new Recursive_Open_Struct.
27
+ def initialize
28
+ @methods = Hash.new
29
+ @open = true
30
+ end
31
+
32
+ # automatically add parameters
33
+ def method_missing(method, *params) # :nodoc:
34
+ # setting or getting?
35
+ is_setting = !params.empty?
36
+
37
+ key = method.id2name
38
+ # remove trailing =
39
+ key.chop! if is_setting
40
+
41
+ # if structure is closed, disable hierarchy creation
42
+ super unless @methods.has_key?(key) || @open
43
+
44
+ if is_setting
45
+ # assigning a new value
46
+ if @methods[key].class == Recursive_Open_Struct
47
+ raise TypeError, "overwriting previously created hierarchy entry '#{key}' not allowed", caller(1)
48
+ end
49
+ @methods[key] = *params
50
+ else
51
+ # no param: create new Recursive_Open_Struct object, if nothing is set.
52
+ unless @methods.has_key?(key)
53
+ @methods[key] = Recursive_Open_Struct.new
54
+ end
55
+ end
56
+ @methods[key]
57
+ end
58
+
59
+ # An alternative way to access the value of an attribute
60
+ # s = Recursive_Open_Struct.new
61
+ # s.name = "Hugo"
62
+ # s["name"] # "Hugo"
63
+ def [](key)
64
+ @methods[key]
65
+ end
66
+
67
+ # An alternative way to set the value of an attribute
68
+ # s = Recursive_Open_Struct.new
69
+ # s["name"] = "Hugo"
70
+ # s.name # "Hugo"
71
+ def []=(key, value)
72
+ self.send((key+"=").to_sym, value)
73
+ end
74
+
75
+ # call-seq:
76
+ # attrs() -> an_array
77
+ #
78
+ # Return a sorted array of attribute names, similar to #methods.
79
+ #
80
+ # s = Recursive_Open_Struct.new
81
+ # s.name = "martinus"
82
+ # s.age = 25
83
+ # s.attrs # returns ["age", "name"]
84
+ def attrs
85
+ @methods.keys.sort
86
+ end
87
+
88
+ # After calling #close, no further modification of the configuration hierarchy
89
+ # is allowed. This is not as strict as #freeze, because you are still allowed
90
+ # to modify data.
91
+ #
92
+ # s = Recursive_Open_Struct.new
93
+ # s.name = "Hugo"
94
+ # s.close
95
+ # s.name = "martinus" # does still work
96
+ # s.age = 25 # raises NoMethodError
97
+ def close
98
+ do_set_open_status(false)
99
+ end
100
+
101
+ # Reopens a Recursive_Open_Struct which was closed with #close earlier.
102
+ # After this call, it is possible to modify the structure again.
103
+ def re_open
104
+ do_set_open_status(true)
105
+ end
106
+
107
+ # call-seq:
108
+ # open?() -> boolean
109
+ #
110
+ # Return whether the structure is still open or not.
111
+ #
112
+ # s = Recursive_Open_Struct.new
113
+ # s.open? # returns true
114
+ # s.close
115
+ # s.open? # returns false
116
+ def open?
117
+ @open
118
+ end
119
+
120
+ # call-seq:
121
+ # each() { |elem| ... }
122
+ #
123
+ # Iterates through all elements of the Recursive_Open_Struct in alphabetic order.
124
+ def each
125
+ attrs.each do |attr|
126
+ yield @methods[attr]
127
+ end
128
+ end
129
+
130
+ protected
131
+
132
+ def do_set_open_status(status)
133
+ @methods.each_value do |val|
134
+ val.do_set_open_status(status) if val.class == Recursive_Open_Struct
135
+ end
136
+ @open = status
137
+ end
138
138
  end
139
139
 
140
140
  if __FILE__ == $0
141
- require 'test/unit'
142
-
143
- class TestRecursiveOpenStruct < Test::Unit::TestCase
144
- def setup
145
- @s = Recursive_Open_Struct.new
146
- end
147
-
148
- def setAndAssertValue(val)
149
- @s.test = val
150
- assert_equal(val, @s.test)
151
- @s.close
152
- assert_equal(val, @s.test)
153
- @s.test = "asdf"
154
- assert_equal("asdf", @s.test)
155
- end
156
-
157
- def testSetNil
158
- setAndAssertValue(nil)
159
- end
160
-
161
- def testSimple
162
- @s.test = "xx"
163
- @s.close
164
- assert_equal("xx", @s.test)
165
- end
166
-
167
- def testSetFalse
168
- setAndAssertValue(false)
169
- end
170
-
171
- def testSetStr
172
- setAndAssertValue("topfen")
173
- end
174
-
175
- def testSetClass
176
- setAndAssertValue(String)
177
- end
178
-
179
- def testSetTrue
180
- setAndAssertValue(true)
181
- end
182
-
183
- def testSet0
184
- setAndAssertValue(0)
185
- end
186
-
187
- def testRaiseTypeError
188
- @s.a.b = 1
189
- assert_raise(TypeError) do
190
- @s.a = 3
191
- end
192
- end
193
-
194
- def testAttrs
195
- assert_equal([], @s.attrs)
196
- @s.b = "x"
197
- @s.a = "a"
198
- assert_equal(["a", "b"], @s.attrs)
199
- end
200
-
201
- def testRecursive
202
- @s.a.b = 1
203
- @s.a.c = 2
204
- assert_equal(["a"], @s.attrs)
205
- end
206
-
207
- def testStrange
208
- @s.a
209
- assert_equal(["a"], @s.attrs)
210
- assert_equal(Recursive_Open_Struct, @s.a.class)
211
- @s.a.x = "asfd"
212
- assert_equal("asfd", @s.a.x)
213
- end
214
-
215
- def testKlammer
216
- @s.a = "asdf"
217
- assert_equal("asdf", @s["a"])
218
- @s.b_x = "hog"
219
- assert_equal("hog", @s["b_x"])
220
- @s.c.b.a = 1234
221
- assert_equal(1234, @s["c"]["b"]["a"])
222
- end
223
-
224
- def testDeep
225
- @s.a.b.c.d.e.f.g.h.i.j.k.l.m.n.o.p.q.r.s.t.u.v.w.x.y.z = false
226
- @s.close
227
- assert_raise(NoMethodError) do
228
- @s.blub = "hellow"
229
- end
230
- assert_equal(false, @s.a.b.c.d.e.f.g.h.i.j.k.l.m.n.o.p.q.r.s.t.u.v.w.x.y.z)
231
- end
232
- end
141
+ require 'test/unit'
142
+
143
+ class TestRecursiveOpenStruct < Test::Unit::TestCase
144
+ def setup
145
+ @s = Recursive_Open_Struct.new
146
+ end
147
+
148
+ def setAndAssertValue(val)
149
+ @s.test = val
150
+ assert_equal(val, @s.test)
151
+ @s.close
152
+ assert_equal(val, @s.test)
153
+ @s.test = "asdf"
154
+ assert_equal("asdf", @s.test)
155
+ end
156
+
157
+ def testSetNil
158
+ setAndAssertValue(nil)
159
+ end
160
+
161
+ def testSimple
162
+ @s.test = "xx"
163
+ @s.close
164
+ assert_equal("xx", @s.test)
165
+ end
166
+
167
+ def testSetFalse
168
+ setAndAssertValue(false)
169
+ end
170
+
171
+ def testSetStr
172
+ setAndAssertValue("topfen")
173
+ end
174
+
175
+ def testSetClass
176
+ setAndAssertValue(String)
177
+ end
178
+
179
+ def testSetTrue
180
+ setAndAssertValue(true)
181
+ end
182
+
183
+ def testSet0
184
+ setAndAssertValue(0)
185
+ end
186
+
187
+ def testRaiseTypeError
188
+ @s.a.b = 1
189
+ assert_raise(TypeError) do
190
+ @s.a = 3
191
+ end
192
+ end
193
+
194
+ def testAttrs
195
+ assert_equal([], @s.attrs)
196
+ @s.b = "x"
197
+ @s.a = "a"
198
+ assert_equal(["a", "b"], @s.attrs)
199
+ end
200
+
201
+ def testRecursive
202
+ @s.a.b = 1
203
+ @s.a.c = 2
204
+ assert_equal(["a"], @s.attrs)
205
+ end
206
+
207
+ def testStrange
208
+ @s.a
209
+ assert_equal(["a"], @s.attrs)
210
+ assert_equal(Recursive_Open_Struct, @s.a.class)
211
+ @s.a.x = "asfd"
212
+ assert_equal("asfd", @s.a.x)
213
+ end
214
+
215
+ def testKlammer
216
+ @s.a = "asdf"
217
+ assert_equal("asdf", @s["a"])
218
+ @s.b_x = "hog"
219
+ assert_equal("hog", @s["b_x"])
220
+ @s.c.b.a = 1234
221
+ assert_equal(1234, @s["c"]["b"]["a"])
222
+ end
223
+
224
+ def testDeep
225
+ @s.a.b.c.d.e.f.g.h.i.j.k.l.m.n.o.p.q.r.s.t.u.v.w.x.y.z = false
226
+ @s.close
227
+ assert_raise(NoMethodError) do
228
+ @s.blub = "hellow"
229
+ end
230
+ assert_equal(false, @s.a.b.c.d.e.f.g.h.i.j.k.l.m.n.o.p.q.r.s.t.u.v.w.x.y.z)
231
+ end
232
+ end
233
233
  end
data/lib/RiManager.rb CHANGED
@@ -3,153 +3,153 @@ require 'rdoc/ri/ri_driver'
3
3
  require 'pp'
4
4
 
5
5
  class NameDescriptor
6
- CLASS = 0
7
- INSTANCE_METHOD = 1
8
- CLASS_METHOD = 2
9
- def type
10
- @method_name ||= false
11
- @is_class_method ||= false
12
- if @method_name
13
- if @is_class_method
14
- CLASS_METHOD
15
- else
16
- INSTANCE_METHOD
17
- end
18
- else
19
- CLASS
20
- end
21
- end
22
-
23
- def to_s
24
- str = ""
25
- str << @class_names.join("::")
26
- if @method_name && str != ""
27
- str << (@is_class_method ? "::" : "#")
28
- end
29
- str << @method_name if @method_name
30
- str
31
- end
6
+ CLASS = 0
7
+ INSTANCE_METHOD = 1
8
+ CLASS_METHOD = 2
9
+ def type
10
+ @method_name ||= false
11
+ @is_class_method ||= false
12
+ if @method_name
13
+ if @is_class_method
14
+ CLASS_METHOD
15
+ else
16
+ INSTANCE_METHOD
17
+ end
18
+ else
19
+ CLASS
20
+ end
21
+ end
22
+
23
+ def to_s
24
+ str = ""
25
+ str << @class_names.join("::")
26
+ if @method_name && str != ""
27
+ str << (@is_class_method ? "::" : "#")
28
+ end
29
+ str << @method_name if @method_name
30
+ str
31
+ end
32
32
  end
33
33
 
34
34
  # This basically is a stripped down version of RiDriver.
35
35
  # I cannot use RiDriver directly, because I need to set the driver.
36
36
  class RiManager
37
- def initialize(display, path = RI::Paths::PATH)
38
- @reader = RI::RiReader.new(RI::RiCache.new(RI::Paths::PATH))
39
- @display = display
40
- @display.reader = @reader
41
- # prepare all names
42
- @all_names = prepare_all_names
43
- end
44
-
45
- def prepare_all_names
46
- names = Array.new
47
- @reader.all_names.each do |name|
48
- begin
49
- names.push NameDescriptor.new(name)
50
- rescue RiError => e
51
- # silently ignore errors
52
- end
53
- end
54
- names
55
- end
56
-
57
- def check_names
58
- @reader.all_names.each do |name|
59
- begin
60
- if (NameDescriptor.new(name).to_s != name)
61
- p [name, NameDescriptor.new(name).to_s, NameDescriptor.new(name)]
62
- end
63
- rescue RiError => e
64
- puts e
65
- end
66
- end
67
- end
68
-
69
- # Returns all fully names as name descriptors
70
- def all_names
71
- @all_names
72
- end
73
-
74
- # Searches for the description of a name and shows it using +display+.
75
- def show(name_descriptor, width)
76
- @display.width = width
77
- # narrow down namespace
78
- namespace = @reader.top_level_namespace
79
- name_descriptor.class_names.each do |classname|
80
- namespace = @reader.lookup_namespace_in(classname, namespace)
81
- if namespace.empty?
82
- raise RiError.new("Nothing known about #{name_descriptor}")
83
- end
84
- end
85
-
86
- # At this point, if we have multiple possible namespaces, but one
87
- # is an exact match for our requested class, prune down to just it
88
- # PS: this comment is shamlessly stolen from ri_driver.rb
89
- entries = namespace.find_all {|m| m.full_name == name_descriptor.full_class_name}
90
- namespace = entries if entries.size == 1
91
-
92
- if name_descriptor.method_name
93
- methods = @reader.find_methods(name_descriptor.method_name, name_descriptor.is_class_method, namespace)
94
- report_method_stuff(name_descriptor.method_name, methods)
95
- else
96
- report_class_stuff(namespace)
97
- end
98
- end
99
-
100
- def report_class_stuff(namespace)
101
- raise RiError.new("namespace") unless namespace.size==1
102
- @display.display_class_info @reader.get_class(namespace[0])
103
- end
104
-
105
- def report_method_stuff(requested_method_name, methods)
106
- if methods.size == 1
107
- method = @reader.get_method(methods[0])
108
- @display.display_method_info(method)
109
- else
110
- entries = methods.find_all {|m| m.name == requested_method_name}
111
- if entries.size == 1
112
- method = @reader.get_method(entries[0])
113
- @display.display_method_info(method)
114
- else
115
- puts methods.map {|m| m.full_name}.join(", ")
116
- end
117
- end
118
- =begin
119
-
120
- method = if (methods.size == 1)
121
- @reader.get_method(methods[0])
122
- else
123
- entries = methods.find_all {|m| m.name == requested_method_name}
124
- entries.size
125
- # there really should be just *one* method that matches.
126
- raise RiError.new("got a strange method") unless entries.size == 1
127
- @reader.get_method(entries[0])
128
- end
129
- @display.display_method_info(method)
37
+ def initialize(display, path = RI::Paths::PATH)
38
+ @reader = RI::RiReader.new(RI::RiCache.new(RI::Paths::PATH))
39
+ @display = display
40
+ @display.reader = @reader
41
+ # prepare all names
42
+ @all_names = prepare_all_names
43
+ end
44
+
45
+ def prepare_all_names
46
+ names = Array.new
47
+ @reader.all_names.each do |name|
48
+ begin
49
+ names.push NameDescriptor.new(name)
50
+ rescue RiError => e
51
+ # silently ignore errors
52
+ end
53
+ end
54
+ names
55
+ end
56
+
57
+ def check_names
58
+ @reader.all_names.each do |name|
59
+ begin
60
+ if (NameDescriptor.new(name).to_s != name)
61
+ p [name, NameDescriptor.new(name).to_s, NameDescriptor.new(name)]
62
+ end
63
+ rescue RiError => e
64
+ puts e
65
+ end
66
+ end
67
+ end
68
+
69
+ # Returns all fully names as name descriptors
70
+ def all_names
71
+ @all_names
72
+ end
73
+
74
+ # Searches for the description of a name and shows it using +display+.
75
+ def show(name_descriptor, width)
76
+ @display.width = width
77
+ # narrow down namespace
78
+ namespace = @reader.top_level_namespace
79
+ name_descriptor.class_names.each do |classname|
80
+ namespace = @reader.lookup_namespace_in(classname, namespace)
81
+ if namespace.empty?
82
+ raise RiError.new("Nothing known about #{name_descriptor}")
83
+ end
84
+ end
85
+
86
+ # At this point, if we have multiple possible namespaces, but one
87
+ # is an exact match for our requested class, prune down to just it
88
+ # PS: this comment is shamlessly stolen from ri_driver.rb
89
+ entries = namespace.find_all {|m| m.full_name == name_descriptor.full_class_name}
90
+ namespace = entries if entries.size == 1
91
+
92
+ if name_descriptor.method_name
93
+ methods = @reader.find_methods(name_descriptor.method_name, name_descriptor.is_class_method, namespace)
94
+ report_method_stuff(name_descriptor.method_name, methods)
95
+ else
96
+ report_class_stuff(namespace)
97
+ end
98
+ end
99
+
100
+ def report_class_stuff(namespace)
101
+ raise RiError.new("namespace") unless namespace.size==1
102
+ @display.display_class_info @reader.get_class(namespace[0])
103
+ end
104
+
105
+ def report_method_stuff(requested_method_name, methods)
106
+ if methods.size == 1
107
+ method = @reader.get_method(methods[0])
108
+ @display.display_method_info(method)
109
+ else
110
+ entries = methods.find_all {|m| m.name == requested_method_name}
111
+ if entries.size == 1
112
+ method = @reader.get_method(entries[0])
113
+ @display.display_method_info(method)
114
+ else
115
+ puts methods.map {|m| m.full_name}.join(", ")
116
+ end
117
+ end
118
+ =begin
119
+
120
+ method = if (methods.size == 1)
121
+ @reader.get_method(methods[0])
122
+ else
123
+ entries = methods.find_all {|m| m.name == requested_method_name}
124
+ entries.size
125
+ # there really should be just *one* method that matches.
126
+ raise RiError.new("got a strange method") unless entries.size == 1
127
+ @reader.get_method(entries[0])
128
+ end
129
+ @display.display_method_info(method)
130
130
  =end
131
- end
131
+ end
132
132
  end
133
133
 
134
134
  if __FILE__ == $0
135
- display = Displayer.new
136
- ri = RiManager.new(display)
137
- ri.all_names.each do |name|
138
- p [name.type, name.to_s] if name.type==0
139
- end
135
+ display = Displayer.new
136
+ ri = RiManager.new(display)
137
+ ri.all_names.each do |name|
138
+ p [name.type, name.to_s] if name.type==0
139
+ end
140
140
  end
141
141
 
142
142
 
143
143
  =begin
144
144
  # iterate through everything
145
145
  reader.full_class_names.sort.each do |class_name|
146
- classDesc = reader.find_class_by_name(class_name)
147
-
148
- if class_name=="Integer"
149
- pp classDesc.instance_methods
150
- puts classDesc.to_yaml
151
- #puts classDesc.methods
152
- gets
153
- end
146
+ classDesc = reader.find_class_by_name(class_name)
147
+
148
+ if class_name=="Integer"
149
+ pp classDesc.instance_methods
150
+ puts classDesc.to_yaml
151
+ #puts classDesc.methods
152
+ gets
153
+ end
154
154
  end
155
155
  =end