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 CHANGED
@@ -1 +1 @@
1
- 0.0.6
1
+ 0.0.7
@@ -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 take_snapshot
75
- target.characterizable_base.characteristics.each do |_, c|
76
- if c.relevant?(target)
77
- self[c.name] = c.value(target)
78
- end
79
- end
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 relevant
82
- target.characterizable_base.characteristics.select { |_, c| c.relevant?(self) }
100
+ def potential
101
+ target.characterizable_base.characteristics.select { |_, c| c.potential?(self) }
83
102
  end
84
- def irrelevant
85
- target.characterizable_base.characteristics.select { |_, c| c.irrelevant?(self) }
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 irrelevant?(target)
150
- value(target).nil? and revealed? target and untrumped? target
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 relevant?(target)
153
- !value(target).nil? and revealed? target and untrumped? target
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.relevant? target
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].relevant? target
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 relevant characteristics of a class" do
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.relevant.class
71
- assert_equal Characterizable::SurvivorHash, a.characteristics.relevant.select { false }.class
72
- assert_equal Characterizable::SurvivorHash, a.characteristics.relevant.slice(:hello).class
73
- assert_equal Characterizable::SurvivorHash, a.characteristics.relevant.merge({:hi => 'there'}).class
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 relevant" do
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.relevant.keys
84
+ assert_same_contents [:make], a.characteristics.effective.keys
85
85
  end
86
86
 
87
- should "tell you what characteristics are irrelevant" do
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.irrelevant.keys
90
+ assert_same_contents [:model, :variant], a.characteristics.potential.keys
91
91
  end
92
92
 
93
- should "present a concise set of relevant characteristics by getting rid of those that have been trumped" do
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.relevant.keys
98
+ assert_same_contents [:make, :variant], a.characteristics.effective.keys
99
99
  end
100
100
 
101
- should "not mention a characteristic as irrelevant if, in fact, it has been trumped" do
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.irrelevant.keys
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 irrelevant if it is waiting on something else to be revealed" do
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.irrelevant.keys.include?(:model_year)
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.irrelevant.keys.include?(:size_class)
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.relevant.keys
121
- assert !a.characteristics.irrelevant.keys.include?(:size_class)
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.relevant[:model_year]
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 relevant on a snapshot" do
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.relevant.keys
194
+ assert_same_contents [:make], a.characteristics.effective.keys
156
195
  end
157
196
 
158
- should "know what is irrelevant on a snapshot" do
197
+ should "know what is potential on a snapshot" do
159
198
  a = Automobile.new
160
199
  a.make = 'Ford'
161
- assert a.characteristics.irrelevant.keys.include?(:model_year)
200
+ assert a.characteristics.potential.keys.include?(:model_year)
162
201
  end
163
202
 
164
- should "not reveal irrelevant characteristics in snapshots" do
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.relevant.keys
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 irrelevant characteristics in snapshots, even if it was previously revealed" do
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.relevant.keys
214
+ assert_same_contents [:make, :model_year], a.characteristics.effective.keys
176
215
  a.make = nil
177
- assert_same_contents [], a.characteristics.relevant.keys
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.relevant.keys
224
+ assert_same_contents [:make, :model_year], snapshot.effective.keys
186
225
  a.make = nil
187
- assert_same_contents [], a.characteristics.relevant.keys
188
- assert_same_contents [:make, :model_year], snapshot.relevant.keys
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.relevant.keys
196
- assert snapshot.irrelevant.keys.include?(:model_year)
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.relevant.keys
200
- assert !snapshot.irrelevant.keys.include?(:model_year)
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.relevant.keys
212
- assert_same_contents [:daily_distance_estimate], a.characteristics.relevant.keys
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.relevant.keys
264
+ assert_same_contents [:make, :model_year, :model, :variant], a.characteristics.effective.keys
226
265
  a.make = nil
227
- assert_same_contents [], a.characteristics.relevant.keys
266
+ assert_same_contents [], a.characteristics.effective.keys
228
267
  a.make = 'Ford'
229
- assert_same_contents [:make, :model_year, :model, :variant], a.characteristics.relevant.keys
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.relevant.keys
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.relevant.keys
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.relevant.keys
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.relevant.keys
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.relevant.keys
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.relevant.keys
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.relevant.keys
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.relevant.keys
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.relevant.keys
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.relevant.keys
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.relevant.keys
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
- - 6
9
- version: 0.0.6
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-26 00:00:00 -04:00
18
+ date: 2010-05-27 00:00:00 -04:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency