safer 0.2.0 → 0.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/History.txt +6 -0
- data/README.txt +6 -6
- data/lib/safer/ivar.rb +9 -9
- data/lib/safer/protocol.rb +27 -26
- data/lib/safer.rb +10 -2
- data/test/test_safer_ivar.rb +17 -17
- data/test/test_safer_protocol.rb +4 -4
- metadata +10 -10
data/History.txt
CHANGED
@@ -7,3 +7,9 @@
|
|
7
7
|
* ClassProtocol renamed to Protocol.
|
8
8
|
* Protocol can non-violently check if a class or instance conforms.
|
9
9
|
* Added much documentation.
|
10
|
+
|
11
|
+
=== 0.2.1 / 2010-10-17
|
12
|
+
|
13
|
+
* README.txt still referred to ClassProtocol. Fixed.
|
14
|
+
* The Safer module documentation now refers to its submodules.
|
15
|
+
* Regularized use of '::' and '.'.
|
data/README.txt
CHANGED
@@ -8,12 +8,12 @@ Safer is an umbrella library, with components designed to make it simple to
|
|
8
8
|
verify and improve the safety of your ruby code. There are at present two
|
9
9
|
modules under the safer umbrella:
|
10
10
|
|
11
|
-
[<tt>Safer::IVar</tt>]
|
12
|
-
|
13
|
-
[<tt>Safer::
|
14
|
-
|
15
|
-
|
16
|
-
|
11
|
+
[<tt>Safer::IVar</tt>] generates specially-named accessor functions
|
12
|
+
for class instance variables.
|
13
|
+
[<tt>Safer::Protocol</tt>] is used to provide a ruby analogue to
|
14
|
+
Objective-C Protocols (which are similar to
|
15
|
+
Java interfaces, but do not require
|
16
|
+
inheritance).
|
17
17
|
|
18
18
|
== FEATURES/PROBLEMS:
|
19
19
|
|
data/lib/safer/ivar.rb
CHANGED
@@ -130,7 +130,7 @@ module Safer
|
|
130
130
|
# For example, given the following listing:
|
131
131
|
# class OuterClass
|
132
132
|
# class InnerClass
|
133
|
-
# SYMNM = Safer::IVar
|
133
|
+
# SYMNM = Safer::IVar.symbol_names(self, :foo)
|
134
134
|
# puts(SYMNM.to_s)
|
135
135
|
# end
|
136
136
|
# end
|
@@ -146,12 +146,12 @@ module Safer
|
|
146
146
|
# [+klass+] Class object into which to generate the variable accessor
|
147
147
|
# functions.
|
148
148
|
# [+nmlist+] List of symbols for which to create accessor routines.
|
149
|
-
# Uses Safer::IVar
|
149
|
+
# Uses Safer::IVar.symbol_names to determine the symbol names of the
|
150
150
|
# accessor routines.
|
151
151
|
# For example, the listing:
|
152
152
|
# class MyClass
|
153
153
|
# class SubClass
|
154
|
-
# Safer::IVar
|
154
|
+
# Safer::IVar.instance_variable(self, :foo, :bar)
|
155
155
|
# end
|
156
156
|
# end
|
157
157
|
# is equivalent to the following code:
|
@@ -174,10 +174,10 @@ module Safer
|
|
174
174
|
##
|
175
175
|
# export the reader routines for instance variables in a nicer way.
|
176
176
|
# [+klass+] Class object for which to define reader accessors for
|
177
|
-
# instance variables defined by Safer::IVar
|
177
|
+
# instance variables defined by Safer::IVar.instance_variable.
|
178
178
|
# [+nmlist+] List of symbols for which to define reader accessors.
|
179
179
|
# Each symbol in +nmlist+ should have previously been given as
|
180
|
-
# an argument to Safer::IVar
|
180
|
+
# an argument to Safer::IVar.instance_variable(+klass+).
|
181
181
|
def self.export_reader(klass, *nmlist)
|
182
182
|
symlist = self.symbol_names(klass, *nmlist)
|
183
183
|
nmlist.size.times do |index|
|
@@ -190,10 +190,10 @@ module Safer
|
|
190
190
|
##
|
191
191
|
# export the writer routines for instance variables in a nicer way.
|
192
192
|
# [+klass+] Class object for which to define writer accessors for
|
193
|
-
# instance variables defined by Safer::IVar
|
193
|
+
# instance variables defined by Safer::IVar.instance_variable.
|
194
194
|
# [+nmlist+] List of symbols for which to define writer accessors.
|
195
195
|
# Each symbol in +nmlist+ should have previously been given as
|
196
|
-
# an argument to Safer::IVar
|
196
|
+
# an argument to Safer::IVar.instance_variable(+klass+).
|
197
197
|
def self.export_writer(klass, *nmlist)
|
198
198
|
symlist = self.symbol_names(klass, *nmlist)
|
199
199
|
nmlist.size.times do |index|
|
@@ -209,10 +209,10 @@ module Safer
|
|
209
209
|
# export both reader and writer routines for instance variables in a
|
210
210
|
# nicer way.
|
211
211
|
# [+klass+] Class object for which to define accessors for
|
212
|
-
# instance variables defined by Safer::IVar
|
212
|
+
# instance variables defined by Safer::IVar.instance_variable.
|
213
213
|
# [+nmlist+] List of symbols for which to define accessors.
|
214
214
|
# Each symbol in +nmlist+ should have previously been given as
|
215
|
-
# an argument to Safer::IVar
|
215
|
+
# an argument to Safer::IVar.instance_variable(+klass+).
|
216
216
|
def self.export_accessor(klass, *nmlist)
|
217
217
|
self.export_reader(klass, *nmlist)
|
218
218
|
self.export_writer(klass, *nmlist)
|
data/lib/safer/protocol.rb
CHANGED
@@ -62,9 +62,9 @@ module Safer
|
|
62
62
|
# end
|
63
63
|
# end
|
64
64
|
# # should succeed.
|
65
|
-
# a = YourClass
|
65
|
+
# a = YourClass.new(Foo.new)
|
66
66
|
# # should raise a Safer::Protocol::Error::InstanceError.
|
67
|
-
# b = YourClass
|
67
|
+
# b = YourClass.new(15)
|
68
68
|
#
|
69
69
|
# ==Rationale
|
70
70
|
# It is often the case that we expect objects passed to our methods to
|
@@ -115,7 +115,7 @@ module Safer
|
|
115
115
|
if ! block
|
116
116
|
block = proc do |h, el| true ; end
|
117
117
|
end
|
118
|
-
array.inject(Hash
|
118
|
+
array.inject(Hash.new) do |h, el|
|
119
119
|
newval = block.call(h, el)
|
120
120
|
if newval
|
121
121
|
h[el] = newval
|
@@ -130,8 +130,8 @@ module Safer
|
|
130
130
|
# Protocol::Violation objects: one for class-method protocol
|
131
131
|
# violations, and one for instance-method protocol violations.
|
132
132
|
class Violation
|
133
|
-
Safer::IVar
|
134
|
-
Safer::IVar
|
133
|
+
Safer::IVar.instance_variable(self, :table)
|
134
|
+
Safer::IVar.instance_variable(self, :report)
|
135
135
|
|
136
136
|
##
|
137
137
|
# :attr_reader: table
|
@@ -142,12 +142,13 @@ module Safer
|
|
142
142
|
# the protocol, the value for that method will be the arity discovered in
|
143
143
|
# the object. (The desired arity can be accessed through the
|
144
144
|
# Protocol::Signature for that method.)
|
145
|
-
Safer::IVar
|
145
|
+
Safer::IVar.export_reader(self, :table)
|
146
|
+
|
146
147
|
##
|
147
148
|
# :attr_reader: report
|
148
149
|
# String describing the errors from +table+ in a more human-readable
|
149
150
|
# fashion.
|
150
|
-
Safer::IVar
|
151
|
+
Safer::IVar.export_reader(self, :report)
|
151
152
|
|
152
153
|
##
|
153
154
|
# Create a new Protocol::Violation object.
|
@@ -169,12 +170,12 @@ module Safer
|
|
169
170
|
# two of these objects - one describing class methods, and one describing
|
170
171
|
# instance methods.
|
171
172
|
class Signature
|
172
|
-
Safer::IVar
|
173
|
+
Safer::IVar.instance_variable(self, :table)
|
173
174
|
##
|
174
175
|
# :attr_reader: table
|
175
176
|
# +Hash+ object, in which the keys are the names of methods, and the
|
176
177
|
# value for a key is the required arity of that method.
|
177
|
-
Safer::IVar
|
178
|
+
Safer::IVar.export_reader(self, :table)
|
178
179
|
|
179
180
|
##
|
180
181
|
# Create a Protocol::Signature object.
|
@@ -195,7 +196,7 @@ module Safer
|
|
195
196
|
def find_violations(object, get_method)
|
196
197
|
report = ''
|
197
198
|
have_error = false
|
198
|
-
error_table = Hash
|
199
|
+
error_table = Hash.new
|
199
200
|
self.safer_protocol_signature__table.each_pair do |name, arity|
|
200
201
|
begin
|
201
202
|
m = object.send(get_method, name)
|
@@ -236,34 +237,34 @@ module Safer
|
|
236
237
|
## Safer::Protocol::Error
|
237
238
|
# Error generated when a class does not conform to a protocol signature.
|
238
239
|
class Error < StandardError
|
239
|
-
Safer::IVar
|
240
|
-
Safer::IVar
|
241
|
-
Safer::IVar
|
242
|
-
Safer::IVar
|
243
|
-
Safer::IVar
|
240
|
+
Safer::IVar.instance_variable(self, :error_object)
|
241
|
+
Safer::IVar.instance_variable(self, :protocol)
|
242
|
+
Safer::IVar.instance_variable(self, :class_violations)
|
243
|
+
Safer::IVar.instance_variable(self, :instance_violations)
|
244
|
+
Safer::IVar.instance_variable(self, :report)
|
244
245
|
|
245
246
|
##
|
246
247
|
# :attr_reader: error_object
|
247
248
|
# Object that does not properly implement a desired method protocol.
|
248
|
-
Safer::IVar
|
249
|
+
Safer::IVar.export_reader(self, :error_object)
|
249
250
|
##
|
250
251
|
# :attr_reader: protocol
|
251
252
|
# Protocol object describing the signature that error_object failed
|
252
253
|
# to properly implement.
|
253
|
-
Safer::IVar
|
254
|
+
Safer::IVar.export_reader(self, :protocol)
|
254
255
|
##
|
255
256
|
# :attr_reader: class_violations
|
256
257
|
# Protocol::Violation describing class-method protocol violations.
|
257
|
-
Safer::IVar
|
258
|
+
Safer::IVar.export_reader(self, :class_violations)
|
258
259
|
##
|
259
260
|
# :attr_reader: instance_violations
|
260
261
|
# Protocol::Violation describing instance-method protocol violations.
|
261
|
-
Safer::IVar
|
262
|
+
Safer::IVar.export_reader(self, :instance_violations)
|
262
263
|
##
|
263
264
|
# :attr_reader: report
|
264
265
|
# String for displaying human-readable error message describing protocol
|
265
266
|
# violations.
|
266
|
-
Safer::IVar
|
267
|
+
Safer::IVar.export_reader(self, :report)
|
267
268
|
|
268
269
|
|
269
270
|
##
|
@@ -332,25 +333,25 @@ module Safer
|
|
332
333
|
end # Safer::Protocol::Error
|
333
334
|
|
334
335
|
|
335
|
-
Safer::IVar
|
336
|
-
Safer::IVar
|
337
|
-
Safer::IVar
|
336
|
+
Safer::IVar.instance_variable(self, :name)
|
337
|
+
Safer::IVar.instance_variable(self, :class_signature)
|
338
|
+
Safer::IVar.instance_variable(self, :instance_signature)
|
338
339
|
|
339
340
|
##
|
340
341
|
# :attr_reader: name
|
341
342
|
# Name of this protocol signature. Typically derived from the class name
|
342
343
|
# from which this signature is derived.
|
343
|
-
Safer::IVar
|
344
|
+
Safer::IVar.export_reader(self, :name)
|
344
345
|
##
|
345
346
|
# :attr_reader: class_signature
|
346
347
|
# Signatures of required class methods for objects implementing this
|
347
348
|
# protocol.
|
348
|
-
Safer::IVar
|
349
|
+
Safer::IVar.export_reader(self, :class_signature)
|
349
350
|
##
|
350
351
|
# :attr_reader: instance_signature
|
351
352
|
# Signatures of required instance methods for objects implementing this
|
352
353
|
# protocol.
|
353
|
-
Safer::IVar
|
354
|
+
Safer::IVar.export_reader(self, :instance_signature)
|
354
355
|
|
355
356
|
##
|
356
357
|
# Create a +Protocol+ object.
|
data/lib/safer.rb
CHANGED
@@ -1,8 +1,16 @@
|
|
1
1
|
##
|
2
2
|
# Safer is an umbrella library, whose components are designed to improve
|
3
|
-
# the safety of your code.
|
3
|
+
# the safety of your code. There are at present two modules under the safer
|
4
|
+
# umbrella:
|
5
|
+
#
|
6
|
+
# [<tt>Safer::IVar</tt>] generates specially-named accessor functions
|
7
|
+
# for class instance variables.
|
8
|
+
# [<tt>Safer::Protocol</tt>] is used to provide a ruby analogue to
|
9
|
+
# Objective-C Protocols (which are similar to
|
10
|
+
# Java interfaces, but do not require
|
11
|
+
# inheritance).
|
4
12
|
module Safer
|
5
13
|
##
|
6
14
|
# Current release of Safer.
|
7
|
-
VERSION = "0.2.
|
15
|
+
VERSION = "0.2.1"
|
8
16
|
end
|
data/test/test_safer_ivar.rb
CHANGED
@@ -4,19 +4,19 @@ require 'test/unit'
|
|
4
4
|
module Root
|
5
5
|
class Test
|
6
6
|
class Instance
|
7
|
-
Safer::IVar
|
7
|
+
Safer::IVar.instance_variable(self, :instance)
|
8
8
|
end
|
9
9
|
class Reader
|
10
|
-
Safer::IVar
|
11
|
-
Safer::IVar
|
10
|
+
Safer::IVar.instance_variable(self, :instance)
|
11
|
+
Safer::IVar.export_reader(self, :instance)
|
12
12
|
end
|
13
13
|
class Writer
|
14
|
-
Safer::IVar
|
15
|
-
Safer::IVar
|
14
|
+
Safer::IVar.instance_variable(self, :instance)
|
15
|
+
Safer::IVar.export_writer(self, :instance)
|
16
16
|
end
|
17
17
|
class Accessor
|
18
|
-
Safer::IVar
|
19
|
-
Safer::IVar
|
18
|
+
Safer::IVar.instance_variable(self, :instance)
|
19
|
+
Safer::IVar.export_accessor(self, :instance)
|
20
20
|
end
|
21
21
|
end
|
22
22
|
end
|
@@ -50,13 +50,13 @@ class TC_SaferIVar < Test::Unit::TestCase
|
|
50
50
|
end
|
51
51
|
|
52
52
|
def test_class_symbol_prefix
|
53
|
-
assert_equal("root", Safer::IVar
|
54
|
-
assert_equal("root_test", Safer::IVar
|
55
|
-
assert_equal("root_test_instance", Safer::IVar
|
56
|
-
k = Root.class_eval("$k = Class
|
57
|
-
assert_same(nil, Safer::IVar
|
53
|
+
assert_equal("root", Safer::IVar.class_symbol_prefix(Root))
|
54
|
+
assert_equal("root_test", Safer::IVar.class_symbol_prefix(Root::Test))
|
55
|
+
assert_equal("root_test_instance", Safer::IVar.class_symbol_prefix(Root::Test::Instance))
|
56
|
+
k = Root.class_eval("$k = Class.new(Root::Test::Instance)")
|
57
|
+
assert_same(nil, Safer::IVar.class_symbol_prefix(k))
|
58
58
|
b = k.class_eval("class Blah ; end ; Blah")
|
59
|
-
assert_equal("blah", Safer::IVar
|
59
|
+
assert_equal("blah", Safer::IVar.class_symbol_prefix(b))
|
60
60
|
end
|
61
61
|
|
62
62
|
def test_instance
|
@@ -72,14 +72,14 @@ class TC_SaferIVar < Test::Unit::TestCase
|
|
72
72
|
assert_no_respond_to(obj, :instance=)
|
73
73
|
end
|
74
74
|
end
|
75
|
-
ti = Root::Test::Instance
|
75
|
+
ti = Root::Test::Instance.new
|
76
76
|
assert_respond_to(ti, :root_test_instance__instance)
|
77
77
|
test_accessors.call(ti, false, false)
|
78
|
-
tr = Root::Test::Reader
|
78
|
+
tr = Root::Test::Reader.new
|
79
79
|
test_accessors.call(tr, true, false)
|
80
|
-
tw = Root::Test::Writer
|
80
|
+
tw = Root::Test::Writer.new
|
81
81
|
test_accessors.call(tw, false, true)
|
82
|
-
ta = Root::Test::Accessor
|
82
|
+
ta = Root::Test::Accessor.new
|
83
83
|
test_accessors.call(ta, true, true)
|
84
84
|
end
|
85
85
|
end
|
data/test/test_safer_protocol.rb
CHANGED
@@ -38,10 +38,10 @@ class TC_SaferProtocol < Test::Unit::TestCase
|
|
38
38
|
@instance_protocol = Safer::Protocol.create_from_class(TestArityInstance)
|
39
39
|
@both_protocol = Safer::Protocol.create_from_class(TestArityBoth)
|
40
40
|
|
41
|
-
@class_instance = TestArityClass
|
42
|
-
@instance_instance = TestArityInstance
|
43
|
-
@both_instance = TestArityBoth
|
44
|
-
@error_instance = TestArityError
|
41
|
+
@class_instance = TestArityClass.new
|
42
|
+
@instance_instance = TestArityInstance.new
|
43
|
+
@both_instance = TestArityBoth.new
|
44
|
+
@error_instance = TestArityError.new
|
45
45
|
end
|
46
46
|
def teardown
|
47
47
|
@class_protocol = nil
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: safer
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 21
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 2
|
9
|
-
-
|
10
|
-
version: 0.2.
|
9
|
+
- 1
|
10
|
+
version: 0.2.1
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Aidan Cully
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2010-10-
|
18
|
+
date: 2010-10-17 00:00:00 -04:00
|
19
19
|
default_executable:
|
20
20
|
dependencies:
|
21
21
|
- !ruby/object:Gem::Dependency
|
@@ -55,12 +55,12 @@ description: |-
|
|
55
55
|
verify and improve the safety of your ruby code. There are at present two
|
56
56
|
modules under the safer umbrella:
|
57
57
|
|
58
|
-
[<tt>Safer::IVar</tt>]
|
59
|
-
|
60
|
-
[<tt>Safer::
|
61
|
-
|
62
|
-
|
63
|
-
|
58
|
+
[<tt>Safer::IVar</tt>] generates specially-named accessor functions
|
59
|
+
for class instance variables.
|
60
|
+
[<tt>Safer::Protocol</tt>] is used to provide a ruby analogue to
|
61
|
+
Objective-C Protocols (which are similar to
|
62
|
+
Java interfaces, but do not require
|
63
|
+
inheritance).
|
64
64
|
email:
|
65
65
|
- aidan@panix.com
|
66
66
|
executables: []
|