interchangeable 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/interchangeable/version.rb +1 -1
- data/lib/interchangeable.rb +18 -4
- data/spec/interchangeable_spec.rb +71 -8
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 5df0ea6a13cdf08bf9339d7933cb1a6765a4f383
|
4
|
+
data.tar.gz: 9835d7d3a325ea760ccb587486a77bd1bb8e1713
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7e3ac3cfd0390ef4900a94ead1bf8f5b696a9ee12237de563f78e0ac21d9093023d1805b98741bc8554740fda20903940c3b268cb4a5de43eac4e1fa2d0b97aa
|
7
|
+
data.tar.gz: d231000374e534a5b5f773e357a184ae1ddce695f91c4a3ea4d6035270707e996691bad2789569cfd3f670132b8dd9e871ee89fe42515b556afe7bea580c7a8b
|
data/lib/interchangeable.rb
CHANGED
@@ -1,10 +1,21 @@
|
|
1
1
|
require "interchangeable/version"
|
2
2
|
|
3
3
|
class Class
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
4
|
+
|
5
|
+
def interchangeable_describe description
|
6
|
+
@interchangeable_description = description
|
7
|
+
end
|
8
|
+
|
9
|
+
def interchangeable_method *args, &block
|
10
|
+
description = @interchangeable_description
|
11
|
+
@interchangeable_description = nil
|
12
|
+
entry = Struct.new(:method_name, :target, :implemented, :default, :description)
|
13
|
+
.new(args[0], self, false, false, description)
|
14
|
+
Interchangeable.entries << entry
|
15
|
+
if block
|
16
|
+
Interchangeable.define self, args[0], &block
|
17
|
+
entry.default = true
|
18
|
+
end
|
8
19
|
end
|
9
20
|
end
|
10
21
|
|
@@ -21,6 +32,9 @@ module Interchangeable
|
|
21
32
|
the_class.instance_eval do
|
22
33
|
define_method method_name, &block
|
23
34
|
end
|
35
|
+
entry = Interchangeable.entries.select { |x| x.target == the_class && x.method_name && method_name }.first
|
36
|
+
entry.implemented = true
|
37
|
+
entry.default = false
|
24
38
|
end
|
25
39
|
end
|
26
40
|
|
@@ -6,18 +6,19 @@ describe Interchangeable do
|
|
6
6
|
Interchangeable.instance_eval { @settings = nil }
|
7
7
|
end
|
8
8
|
|
9
|
-
describe "defining
|
9
|
+
describe "defining a method on a class" do
|
10
10
|
|
11
11
|
[
|
12
|
-
["Blah", "something"],
|
13
|
-
["Yawn", "applesauce"],
|
14
|
-
].map { |x| Struct.new(:class_name, :method_name).new(*x) }.each do |example|
|
12
|
+
["Blah", "something", "this is a special method"],
|
13
|
+
["Yawn", "applesauce", "make sure this works"],
|
14
|
+
].map { |x| Struct.new(:class_name, :method_name, :description).new(*x) }.each do |example|
|
15
15
|
|
16
16
|
describe "noting the method on a class" do
|
17
17
|
|
18
18
|
before do
|
19
19
|
eval("class #{example.class_name}
|
20
|
-
|
20
|
+
interchangeable_describe \"#{example.description}\"
|
21
|
+
interchangeable_method :#{example.method_name}
|
21
22
|
end")
|
22
23
|
end
|
23
24
|
|
@@ -35,8 +36,16 @@ describe Interchangeable do
|
|
35
36
|
Interchangeable.entries.first.target.must_equal eval(example.class_name)
|
36
37
|
end
|
37
38
|
|
38
|
-
it "should say that it
|
39
|
-
Interchangeable.entries.first.
|
39
|
+
it "should say that it is not implemented" do
|
40
|
+
Interchangeable.entries.first.implemented.must_equal false
|
41
|
+
end
|
42
|
+
|
43
|
+
it "should say that it is not using the default implementation" do
|
44
|
+
Interchangeable.entries.first.default.must_equal false
|
45
|
+
end
|
46
|
+
|
47
|
+
it "should include the description" do
|
48
|
+
Interchangeable.entries.first.description.must_equal example.description
|
40
49
|
end
|
41
50
|
|
42
51
|
end
|
@@ -55,6 +64,14 @@ describe Interchangeable do
|
|
55
64
|
instance.send(example.method_name.to_sym).must_be_same_as the_return_value
|
56
65
|
end
|
57
66
|
|
67
|
+
it "should say that it is not implemented" do
|
68
|
+
Interchangeable.entries.first.implemented.must_equal true
|
69
|
+
end
|
70
|
+
|
71
|
+
it "should say that it is not using the default implementation" do
|
72
|
+
Interchangeable.entries.first.default.must_equal false
|
73
|
+
end
|
74
|
+
|
58
75
|
end
|
59
76
|
|
60
77
|
describe "accessing private members of the class" do
|
@@ -101,7 +118,7 @@ describe Interchangeable do
|
|
101
118
|
|
102
119
|
before do
|
103
120
|
eval("class #{example.class_name}
|
104
|
-
|
121
|
+
interchangeable_method(:#{example.method_name}) { \"#{default_value}\" }
|
105
122
|
end")
|
106
123
|
end
|
107
124
|
|
@@ -112,6 +129,37 @@ describe Interchangeable do
|
|
112
129
|
result.must_equal default_value
|
113
130
|
end
|
114
131
|
|
132
|
+
it "should say that it is implemented" do
|
133
|
+
Interchangeable.entries.first.implemented.must_equal true
|
134
|
+
end
|
135
|
+
|
136
|
+
it "should say that it is using the default implementation" do
|
137
|
+
Interchangeable.entries.first.default.must_equal true
|
138
|
+
end
|
139
|
+
|
140
|
+
describe "and a new implementation is define elsewhere" do
|
141
|
+
|
142
|
+
let(:new_value) { Object.new }
|
143
|
+
|
144
|
+
before do
|
145
|
+
nv = new_value
|
146
|
+
Interchangeable.define(eval(example.class_name), example.method_name.to_sym) do
|
147
|
+
nv
|
148
|
+
end
|
149
|
+
end
|
150
|
+
|
151
|
+
it "should return the new value" do
|
152
|
+
instance = eval(example.class_name).new
|
153
|
+
result = instance.send(example.method_name.to_sym)
|
154
|
+
result.must_be_same_as new_value
|
155
|
+
end
|
156
|
+
|
157
|
+
it "should reset the default flag to false" do
|
158
|
+
Interchangeable.entries.first.default.must_equal false
|
159
|
+
end
|
160
|
+
|
161
|
+
end
|
162
|
+
|
115
163
|
end
|
116
164
|
|
117
165
|
end
|
@@ -122,4 +170,19 @@ describe Interchangeable do
|
|
122
170
|
|
123
171
|
end
|
124
172
|
|
173
|
+
describe "one description, then two methods" do
|
174
|
+
before do
|
175
|
+
eval("class SomethingElse
|
176
|
+
interchangeable_describe \"That's a banana\"
|
177
|
+
interchangeable_method :banana
|
178
|
+
interchangeable_method :kiwi
|
179
|
+
end")
|
180
|
+
end
|
181
|
+
|
182
|
+
it "should put the description on the first, but not the second" do
|
183
|
+
Interchangeable.entries[0].description.must_equal "That's a banana"
|
184
|
+
Interchangeable.entries[1].description.nil?.must_equal true
|
185
|
+
end
|
186
|
+
end
|
187
|
+
|
125
188
|
end
|