characterizable 0.0.6 → 0.0.7
Sign up to get free protection for your applications and to get access to all the features.
- data/VERSION +1 -1
- data/lib/characterizable.rb +47 -16
- data/test/test_characterizable.rb +90 -51
- metadata +3 -3
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.0.
|
1
|
+
0.0.7
|
data/lib/characterizable.rb
CHANGED
@@ -64,6 +64,20 @@ module Characterizable
|
|
64
64
|
super
|
65
65
|
take_snapshot
|
66
66
|
end
|
67
|
+
def take_snapshot
|
68
|
+
target.characterizable_base.characteristics.each do |_, c|
|
69
|
+
if c.known?(target)
|
70
|
+
if c.effective?(target)
|
71
|
+
self[c.name] = c.value(target)
|
72
|
+
elsif !c.untrumped?(target)
|
73
|
+
trumped_keys.push c.name
|
74
|
+
elsif !c.revealed?(target)
|
75
|
+
wasted_keys.push c.name
|
76
|
+
lacking_keys.push c.prerequisite
|
77
|
+
end
|
78
|
+
end
|
79
|
+
end
|
80
|
+
end
|
67
81
|
def target
|
68
82
|
survivor_args.first
|
69
83
|
end
|
@@ -71,18 +85,29 @@ module Characterizable
|
|
71
85
|
target.expire_snapshot!
|
72
86
|
super
|
73
87
|
end
|
74
|
-
def
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
88
|
+
def wasted_keys
|
89
|
+
@wasted_keys ||= Array.new
|
90
|
+
end
|
91
|
+
def trumped_keys
|
92
|
+
@trumped_keys ||= Array.new
|
93
|
+
end
|
94
|
+
def lacking_keys
|
95
|
+
@lacking_keys ||= Array.new
|
96
|
+
end
|
97
|
+
def effective
|
98
|
+
target.characterizable_base.characteristics.select { |_, c| c.effective?(self) }
|
80
99
|
end
|
81
|
-
def
|
82
|
-
target.characterizable_base.characteristics.select { |_, c| c.
|
100
|
+
def potential
|
101
|
+
target.characterizable_base.characteristics.select { |_, c| c.potential?(self) }
|
83
102
|
end
|
84
|
-
def
|
85
|
-
target.characterizable_base.characteristics.
|
103
|
+
def wasted
|
104
|
+
target.characterizable_base.characteristics.slice(*wasted_keys)
|
105
|
+
end
|
106
|
+
def lacking
|
107
|
+
target.characterizable_base.characteristics.slice(*(lacking_keys - wasted_keys))
|
108
|
+
end
|
109
|
+
def trumped
|
110
|
+
target.characterizable_base.characteristics.slice(*trumped_keys)
|
86
111
|
end
|
87
112
|
end
|
88
113
|
|
@@ -134,6 +159,9 @@ module Characterizable
|
|
134
159
|
@options = options
|
135
160
|
Blockenspiel.invoke block, self if block_given?
|
136
161
|
end
|
162
|
+
def inspect
|
163
|
+
"<Characterizable::Characteristic name=#{name.inspect} trumps=#{trumps.inspect} prerequisite=#{prerequisite.inspect} options=#{options.inspect}>"
|
164
|
+
end
|
137
165
|
def trumped_by
|
138
166
|
@_trumped_by ||= characteristics.select { |_, c| c.trumps.include? name }
|
139
167
|
end
|
@@ -146,21 +174,24 @@ module Characterizable
|
|
146
174
|
target.send name if target.respond_to?(name)
|
147
175
|
end
|
148
176
|
end
|
149
|
-
def
|
150
|
-
value(target).nil?
|
177
|
+
def known?(target)
|
178
|
+
!value(target).nil?
|
179
|
+
end
|
180
|
+
def potential?(target)
|
181
|
+
!known?(target) and revealed? target and untrumped? target
|
151
182
|
end
|
152
|
-
def
|
153
|
-
|
183
|
+
def effective?(target)
|
184
|
+
known?(target) and revealed? target and untrumped? target
|
154
185
|
end
|
155
186
|
def untrumped?(target)
|
156
187
|
return true if trumped_by.empty?
|
157
188
|
trumped_by.none? do |_, c|
|
158
|
-
c.
|
189
|
+
c.effective? target
|
159
190
|
end
|
160
191
|
end
|
161
192
|
def revealed?(target)
|
162
193
|
return true if prerequisite.nil?
|
163
|
-
characteristics[prerequisite].
|
194
|
+
characteristics[prerequisite].effective? target
|
164
195
|
end
|
165
196
|
include Blockenspiel::DSL
|
166
197
|
def reveals(other_name, other_options = {}, &block)
|
@@ -47,7 +47,7 @@ class SimpleAutomobile
|
|
47
47
|
end
|
48
48
|
|
49
49
|
class TestCharacterizable < Test::Unit::TestCase
|
50
|
-
should "let you define the
|
50
|
+
should "let you define the effective characteristics of a class" do
|
51
51
|
assert_nothing_raised do
|
52
52
|
class OnDemandAutomobile
|
53
53
|
include Characterizable
|
@@ -67,10 +67,10 @@ class TestCharacterizable < Test::Unit::TestCase
|
|
67
67
|
assert_equal Characterizable::SurvivorHash, SimpleAutomobile.characteristics.slice(:hello).class
|
68
68
|
assert_equal Characterizable::SurvivorHash, SimpleAutomobile.characteristics.merge({:hi => 'there'}).class
|
69
69
|
|
70
|
-
assert_equal Characterizable::SurvivorHash, a.characteristics.
|
71
|
-
assert_equal Characterizable::SurvivorHash, a.characteristics.
|
72
|
-
assert_equal Characterizable::SurvivorHash, a.characteristics.
|
73
|
-
assert_equal Characterizable::SurvivorHash, a.characteristics.
|
70
|
+
assert_equal Characterizable::SurvivorHash, a.characteristics.effective.class
|
71
|
+
assert_equal Characterizable::SurvivorHash, a.characteristics.effective.select { false }.class
|
72
|
+
assert_equal Characterizable::SurvivorHash, a.characteristics.effective.slice(:hello).class
|
73
|
+
assert_equal Characterizable::SurvivorHash, a.characteristics.effective.merge({:hi => 'there'}).class
|
74
74
|
|
75
75
|
assert_equal Characterizable::Snapshot, a.characteristics.class
|
76
76
|
assert_equal Characterizable::Snapshot, a.characteristics.select { false }.class
|
@@ -78,47 +78,86 @@ class TestCharacterizable < Test::Unit::TestCase
|
|
78
78
|
assert_equal Characterizable::Snapshot, a.characteristics.merge({:hi => 'there'}).class
|
79
79
|
end
|
80
80
|
|
81
|
-
should "tell you what characteristics are
|
81
|
+
should "tell you what characteristics are effective" do
|
82
82
|
a = SimpleAutomobile.new
|
83
83
|
a.make = 'Ford'
|
84
|
-
assert_same_contents [:make], a.characteristics.
|
84
|
+
assert_same_contents [:make], a.characteristics.effective.keys
|
85
85
|
end
|
86
86
|
|
87
|
-
should "tell you what characteristics are
|
87
|
+
should "tell you what characteristics are potential" do
|
88
88
|
a = SimpleAutomobile.new
|
89
89
|
a.make = 'Ford'
|
90
|
-
assert_same_contents [:model, :variant], a.characteristics.
|
90
|
+
assert_same_contents [:model, :variant], a.characteristics.potential.keys
|
91
91
|
end
|
92
92
|
|
93
|
-
should "present a concise set of
|
93
|
+
should "present a concise set of effective characteristics by getting rid of those that have been trumped" do
|
94
94
|
a = SimpleAutomobile.new
|
95
95
|
a.make = 'Ford'
|
96
96
|
a.model = 'Taurus'
|
97
97
|
a.variant = 'Taurus V6 DOHC'
|
98
|
-
assert_same_contents [:make, :variant], a.characteristics.
|
98
|
+
assert_same_contents [:make, :variant], a.characteristics.effective.keys
|
99
99
|
end
|
100
100
|
|
101
|
-
should "not mention a characteristic as
|
101
|
+
should "not mention a characteristic as potential if, in fact, it has been trumped" do
|
102
102
|
a = SimpleAutomobile.new
|
103
103
|
a.make = 'Ford'
|
104
104
|
a.variant = 'Taurus V6 DOHC'
|
105
|
-
assert_same_contents [], a.characteristics.
|
105
|
+
assert_same_contents [], a.characteristics.potential.keys
|
106
|
+
end
|
107
|
+
|
108
|
+
should "know what characteristics are wasted and why" do
|
109
|
+
a = Automobile.new
|
110
|
+
a.hybridity = 'Electric'
|
111
|
+
a.variant = 'Taurus V6 DOHC'
|
112
|
+
assert_equal 'Electric', a.characteristics[:hybridity]
|
113
|
+
assert_equal nil, a.characteristics[:variant]
|
114
|
+
assert_same_contents [:hybridity], a.characteristics.effective.keys
|
115
|
+
assert_same_contents [:variant], a.characteristics.wasted.keys
|
116
|
+
assert_same_contents [:model], a.characteristics.lacking.keys
|
117
|
+
assert_same_contents [], a.characteristics.trumped.keys
|
118
|
+
a.model = 'Taurus'
|
119
|
+
assert_equal 'Electric', a.characteristics[:hybridity]
|
120
|
+
assert_equal nil, a.characteristics[:variant]
|
121
|
+
assert_equal nil, a.characteristics[:model]
|
122
|
+
assert_same_contents [:hybridity], a.characteristics.effective.keys
|
123
|
+
assert_same_contents [:variant, :model], a.characteristics.wasted.keys
|
124
|
+
assert_same_contents [:model_year], a.characteristics.lacking.keys
|
125
|
+
assert_same_contents [], a.characteristics.trumped.keys
|
126
|
+
a.model_year = '2006'
|
127
|
+
assert_equal 'Electric', a.characteristics[:hybridity]
|
128
|
+
assert_equal nil, a.characteristics[:variant]
|
129
|
+
assert_equal nil, a.characteristics[:model]
|
130
|
+
assert_equal nil, a.characteristics[:model_year]
|
131
|
+
assert_same_contents [:hybridity], a.characteristics.effective.keys
|
132
|
+
assert_same_contents [:variant, :model, :model_year], a.characteristics.wasted.keys
|
133
|
+
assert_same_contents [:make], a.characteristics.lacking.keys
|
134
|
+
assert_same_contents [], a.characteristics.trumped.keys
|
135
|
+
a.make = 'Ford'
|
136
|
+
assert_equal nil, a.characteristics[:hybridity]
|
137
|
+
assert_equal 'Taurus V6 DOHC', a.characteristics[:variant]
|
138
|
+
assert_equal 'Taurus', a.characteristics[:model]
|
139
|
+
assert_equal '2006', a.characteristics[:model_year]
|
140
|
+
assert_equal 'Ford', a.characteristics[:make]
|
141
|
+
assert_same_contents [:variant, :model, :model_year, :make], a.characteristics.effective.keys
|
142
|
+
assert_same_contents [], a.characteristics.wasted.keys
|
143
|
+
assert_same_contents [], a.characteristics.lacking.keys
|
144
|
+
assert_same_contents [:hybridity], a.characteristics.trumped.keys
|
106
145
|
end
|
107
146
|
|
108
|
-
should "not mention a characteristic as
|
147
|
+
should "not mention a characteristic as potential if it is waiting on something else to be revealed" do
|
109
148
|
a = Automobile.new
|
110
|
-
assert !a.characteristics.
|
149
|
+
assert !a.characteristics.potential.keys.include?(:model_year)
|
111
150
|
end
|
112
151
|
|
113
152
|
should "make sure that trumping works even within revealed characteristics" do
|
114
153
|
a = Automobile.new
|
115
|
-
assert a.characteristics.
|
154
|
+
assert a.characteristics.potential.keys.include?(:size_class)
|
116
155
|
a.make = 'Ford'
|
117
156
|
a.model_year = 1999
|
118
157
|
a.model = 'Taurus'
|
119
158
|
a.size_class = 'mid-size'
|
120
|
-
assert_same_contents [:make, :model_year, :model], a.characteristics.
|
121
|
-
assert !a.characteristics.
|
159
|
+
assert_same_contents [:make, :model_year, :model], a.characteristics.effective.keys
|
160
|
+
assert !a.characteristics.potential.keys.include?(:size_class)
|
122
161
|
end
|
123
162
|
|
124
163
|
should "not enforce prerequisites by using an object's setter" do
|
@@ -127,7 +166,7 @@ class TestCharacterizable < Test::Unit::TestCase
|
|
127
166
|
a.model_year = 1999
|
128
167
|
a.make = nil
|
129
168
|
assert_equal 1999, a.model_year
|
130
|
-
assert_equal nil, a.characteristics.
|
169
|
+
assert_equal nil, a.characteristics.effective[:model_year]
|
131
170
|
end
|
132
171
|
|
133
172
|
should "keep user-defined options on a characteristic" do
|
@@ -149,32 +188,32 @@ class TestCharacterizable < Test::Unit::TestCase
|
|
149
188
|
assert_equal 'Ford', a.characteristics[:make]
|
150
189
|
end
|
151
190
|
|
152
|
-
should "know what is
|
191
|
+
should "know what is effective on a snapshot" do
|
153
192
|
a = Automobile.new
|
154
193
|
a.make = 'Ford'
|
155
|
-
assert_same_contents [:make], a.characteristics.
|
194
|
+
assert_same_contents [:make], a.characteristics.effective.keys
|
156
195
|
end
|
157
196
|
|
158
|
-
should "know what is
|
197
|
+
should "know what is potential on a snapshot" do
|
159
198
|
a = Automobile.new
|
160
199
|
a.make = 'Ford'
|
161
|
-
assert a.characteristics.
|
200
|
+
assert a.characteristics.potential.keys.include?(:model_year)
|
162
201
|
end
|
163
202
|
|
164
|
-
should "not reveal
|
203
|
+
should "not reveal potential characteristics in snapshots" do
|
165
204
|
a = Automobile.new
|
166
205
|
a.model_year = 1999
|
167
|
-
assert_same_contents [], a.characteristics.
|
206
|
+
assert_same_contents [], a.characteristics.effective.keys
|
168
207
|
assert_equal nil, a.characteristics[:model_year]
|
169
208
|
end
|
170
209
|
|
171
|
-
should "not reveal
|
210
|
+
should "not reveal potential characteristics in snapshots, even if it was previously revealed" do
|
172
211
|
a = Automobile.new
|
173
212
|
a.make = 'Ford'
|
174
213
|
a.model_year = 1999
|
175
|
-
assert_same_contents [:make, :model_year], a.characteristics.
|
214
|
+
assert_same_contents [:make, :model_year], a.characteristics.effective.keys
|
176
215
|
a.make = nil
|
177
|
-
assert_same_contents [], a.characteristics.
|
216
|
+
assert_same_contents [], a.characteristics.effective.keys
|
178
217
|
end
|
179
218
|
|
180
219
|
should "keep snapshots separately" do
|
@@ -182,22 +221,22 @@ class TestCharacterizable < Test::Unit::TestCase
|
|
182
221
|
a.make = 'Ford'
|
183
222
|
a.model_year = 1999
|
184
223
|
snapshot = a.characteristics
|
185
|
-
assert_same_contents [:make, :model_year], snapshot.
|
224
|
+
assert_same_contents [:make, :model_year], snapshot.effective.keys
|
186
225
|
a.make = nil
|
187
|
-
assert_same_contents [], a.characteristics.
|
188
|
-
assert_same_contents [:make, :model_year], snapshot.
|
226
|
+
assert_same_contents [], a.characteristics.effective.keys
|
227
|
+
assert_same_contents [:make, :model_year], snapshot.effective.keys
|
189
228
|
end
|
190
229
|
|
191
230
|
should "work when passed around as a snapshot" do
|
192
231
|
a = Automobile.new
|
193
232
|
a.make = 'Ford'
|
194
233
|
snapshot = a.characteristics
|
195
|
-
assert_same_contents [:make], snapshot.
|
196
|
-
assert snapshot.
|
234
|
+
assert_same_contents [:make], snapshot.effective.keys
|
235
|
+
assert snapshot.potential.keys.include?(:model_year)
|
197
236
|
snapshot[:model_year] = 1999
|
198
237
|
assert_equal 1999, snapshot[:model_year]
|
199
|
-
assert_same_contents [:make, :model_year], snapshot.
|
200
|
-
assert !snapshot.
|
238
|
+
assert_same_contents [:make, :model_year], snapshot.effective.keys
|
239
|
+
assert !snapshot.potential.keys.include?(:model_year)
|
201
240
|
assert_equal nil, a.model_year
|
202
241
|
assert_equal nil, a.characteristics[:model_year]
|
203
242
|
assert_equal 1999, snapshot[:model_year]
|
@@ -208,8 +247,8 @@ class TestCharacterizable < Test::Unit::TestCase
|
|
208
247
|
a.daily_distance_estimate = 15
|
209
248
|
snapshot = a.characteristics
|
210
249
|
snapshot[:daily_distance_oracle_estimate] = 20
|
211
|
-
assert_same_contents [:daily_distance_oracle_estimate], snapshot.
|
212
|
-
assert_same_contents [:daily_distance_estimate], a.characteristics.
|
250
|
+
assert_same_contents [:daily_distance_oracle_estimate], snapshot.effective.keys
|
251
|
+
assert_same_contents [:daily_distance_estimate], a.characteristics.effective.keys
|
213
252
|
end
|
214
253
|
|
215
254
|
# has :make do |make|
|
@@ -222,21 +261,21 @@ class TestCharacterizable < Test::Unit::TestCase
|
|
222
261
|
a.model_year = 1999
|
223
262
|
a.model = 'Taurus'
|
224
263
|
a.variant = 'Taurus 1999'
|
225
|
-
assert_same_contents [:make, :model_year, :model, :variant], a.characteristics.
|
264
|
+
assert_same_contents [:make, :model_year, :model, :variant], a.characteristics.effective.keys
|
226
265
|
a.make = nil
|
227
|
-
assert_same_contents [], a.characteristics.
|
266
|
+
assert_same_contents [], a.characteristics.effective.keys
|
228
267
|
a.make = 'Ford'
|
229
|
-
assert_same_contents [:make, :model_year, :model, :variant], a.characteristics.
|
268
|
+
assert_same_contents [:make, :model_year, :model, :variant], a.characteristics.effective.keys
|
230
269
|
a.model_year = nil
|
231
|
-
assert_same_contents [:make], a.characteristics.
|
270
|
+
assert_same_contents [:make], a.characteristics.effective.keys
|
232
271
|
a.model_year = 1999
|
233
|
-
assert_same_contents [:make, :model_year, :model, :variant], a.characteristics.
|
272
|
+
assert_same_contents [:make, :model_year, :model, :variant], a.characteristics.effective.keys
|
234
273
|
a.model = nil
|
235
|
-
assert_same_contents [:make, :model_year], a.characteristics.
|
274
|
+
assert_same_contents [:make, :model_year], a.characteristics.effective.keys
|
236
275
|
a.model = 'Taurus'
|
237
|
-
assert_same_contents [:make, :model_year, :model, :variant], a.characteristics.
|
276
|
+
assert_same_contents [:make, :model_year, :model, :variant], a.characteristics.effective.keys
|
238
277
|
a.variant = nil
|
239
|
-
assert_same_contents [:make, :model_year, :model], a.characteristics.
|
278
|
+
assert_same_contents [:make, :model_year, :model], a.characteristics.effective.keys
|
240
279
|
end
|
241
280
|
|
242
281
|
should "handle trumping on multiple levels" do
|
@@ -247,16 +286,16 @@ class TestCharacterizable < Test::Unit::TestCase
|
|
247
286
|
a.model_year = 1999
|
248
287
|
a.model = 'Taurus'
|
249
288
|
a.variant = 'Taurus 1999'
|
250
|
-
assert_same_contents [:make, :model_year, :model, :variant], a.characteristics.
|
289
|
+
assert_same_contents [:make, :model_year, :model, :variant], a.characteristics.effective.keys
|
251
290
|
a.variant = nil
|
252
|
-
assert_same_contents [:make, :model_year, :model, :hybridity], a.characteristics.
|
291
|
+
assert_same_contents [:make, :model_year, :model, :hybridity], a.characteristics.effective.keys
|
253
292
|
a.variant = 'Taurus 1999'
|
254
|
-
assert_same_contents [:make, :model_year, :model, :variant], a.characteristics.
|
293
|
+
assert_same_contents [:make, :model_year, :model, :variant], a.characteristics.effective.keys
|
255
294
|
a.model = nil # which reveals size class, but also hybridity!
|
256
|
-
assert_same_contents [:make, :model_year, :size_class, :hybridity], a.characteristics.
|
295
|
+
assert_same_contents [:make, :model_year, :size_class, :hybridity], a.characteristics.effective.keys
|
257
296
|
a.model = 'Taurus'
|
258
|
-
assert_same_contents [:make, :model_year, :model, :variant], a.characteristics.
|
297
|
+
assert_same_contents [:make, :model_year, :model, :variant], a.characteristics.effective.keys
|
259
298
|
a.make = nil
|
260
|
-
assert_same_contents [:size_class, :hybridity], a.characteristics.
|
299
|
+
assert_same_contents [:size_class, :hybridity], a.characteristics.effective.keys
|
261
300
|
end
|
262
301
|
end
|
metadata
CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
|
|
5
5
|
segments:
|
6
6
|
- 0
|
7
7
|
- 0
|
8
|
-
-
|
9
|
-
version: 0.0.
|
8
|
+
- 7
|
9
|
+
version: 0.0.7
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- Andy Rossmeissl
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2010-05-
|
18
|
+
date: 2010-05-27 00:00:00 -04:00
|
19
19
|
default_executable:
|
20
20
|
dependencies:
|
21
21
|
- !ruby/object:Gem::Dependency
|