characterizable 0.0.15 → 0.0.16

Sign up to get free protection for your applications and to get access to all the features.
data/Rakefile CHANGED
@@ -12,7 +12,6 @@ begin
12
12
  gem.authors = ["Andy Rossmeissl", "Seamus Abshere"]
13
13
  gem.add_dependency 'blockenspiel', '>=0.3.2'
14
14
  gem.add_dependency 'activesupport', '>=2.3.5'
15
- gem.add_development_dependency 'to_json_fix', '>=0.0.1'
16
15
  gem.add_development_dependency "shoulda", ">= 0"
17
16
  # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
18
17
  end
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.15
1
+ 0.0.16
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{characterizable}
8
- s.version = "0.0.15"
8
+ s.version = "0.0.16"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Andy Rossmeissl", "Seamus Abshere"]
@@ -45,18 +45,15 @@ Gem::Specification.new do |s|
45
45
  if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
46
46
  s.add_runtime_dependency(%q<blockenspiel>, [">= 0.3.2"])
47
47
  s.add_runtime_dependency(%q<activesupport>, [">= 2.3.5"])
48
- s.add_development_dependency(%q<to_json_fix>, [">= 0.0.1"])
49
48
  s.add_development_dependency(%q<shoulda>, [">= 0"])
50
49
  else
51
50
  s.add_dependency(%q<blockenspiel>, [">= 0.3.2"])
52
51
  s.add_dependency(%q<activesupport>, [">= 2.3.5"])
53
- s.add_dependency(%q<to_json_fix>, [">= 0.0.1"])
54
52
  s.add_dependency(%q<shoulda>, [">= 0"])
55
53
  end
56
54
  else
57
55
  s.add_dependency(%q<blockenspiel>, [">= 0.3.2"])
58
56
  s.add_dependency(%q<activesupport>, [">= 2.3.5"])
59
- s.add_dependency(%q<to_json_fix>, [">= 0.0.1"])
60
57
  s.add_dependency(%q<shoulda>, [">= 0"])
61
58
  end
62
59
  end
@@ -32,8 +32,8 @@ module Characterizable
32
32
  def to_hash
33
33
  Hash.new.replace self
34
34
  end
35
- def to_json(*)
36
- to_hash.to_json
35
+ def as_json(*)
36
+ to_hash
37
37
  end
38
38
  def reject(&block)
39
39
  inject(Characterizable::BetterHash.new) do |memo, ary|
@@ -65,19 +65,19 @@ module Characterizable
65
65
  end
66
66
 
67
67
  class Snapshot < BetterHash
68
- attr_reader :target
69
- def initialize(target)
70
- @target = target
68
+ attr_reader :universe
69
+ def initialize(universe)
70
+ @universe = universe
71
71
  _take_snapshot
72
72
  end
73
73
  def _take_snapshot
74
- target.characterizable_base.characteristics.each do |_, c|
75
- if c.known?(target)
76
- if c.effective?(target)
77
- self[c.name] = c.value(target)
78
- elsif !c.untrumped?(target)
74
+ universe.characterizable_base.characteristics.each do |_, c|
75
+ if c.known?(universe)
76
+ if c.effective?(universe)
77
+ self[c.name] = c.value(universe)
78
+ elsif c.trumped?(universe)
79
79
  trumped_keys.push c.name
80
- elsif !c.revealed?(target)
80
+ elsif !c.revealed?(universe)
81
81
  wasted_keys.push c.name
82
82
  lacking_keys.push c.prerequisite
83
83
  end
@@ -85,7 +85,7 @@ module Characterizable
85
85
  end
86
86
  end
87
87
  def []=(key, value)
88
- target.expire_snapshot!
88
+ universe.expire_snapshot!
89
89
  super
90
90
  end
91
91
  def wasted_keys
@@ -98,19 +98,19 @@ module Characterizable
98
98
  @lacking_keys ||= Array.new
99
99
  end
100
100
  def effective
101
- target.characterizable_base.characteristics.select { |_, c| c.effective?(self) }
101
+ universe.characterizable_base.characteristics.select { |_, c| c.effective?(self) }
102
102
  end
103
103
  def potential
104
- target.characterizable_base.characteristics.select { |_, c| c.potential?(self) }
104
+ universe.characterizable_base.characteristics.select { |_, c| c.potential?(self) }
105
105
  end
106
106
  def wasted
107
- target.characterizable_base.characteristics.slice(*wasted_keys)
107
+ universe.characterizable_base.characteristics.slice(*wasted_keys)
108
108
  end
109
109
  def lacking
110
- target.characterizable_base.characteristics.slice(*(lacking_keys - wasted_keys))
110
+ universe.characterizable_base.characteristics.slice(*(lacking_keys - wasted_keys))
111
111
  end
112
112
  def trumped
113
- target.characterizable_base.characteristics.slice(*trumped_keys)
113
+ universe.characterizable_base.characteristics.slice(*trumped_keys)
114
114
  end
115
115
  end
116
116
 
@@ -126,8 +126,6 @@ module Characterizable
126
126
 
127
127
  class CharacteristicAlreadyDefined < ArgumentError
128
128
  end
129
- class CyclicalTrumping < ArgumentError
130
- end
131
129
 
132
130
  class Base
133
131
  attr_reader :klass
@@ -169,50 +167,49 @@ module Characterizable
169
167
  @prerequisite = options.delete(:prerequisite)
170
168
  @options = options
171
169
  Blockenspiel.invoke block, self if block_given?
172
- trumps.each do |trump|
173
- if c = characteristics[trump] and c.trumps.include? name
174
- raise CyclicalTrumping, "On #{base.klass}, '#{c.name}' and '#{name}' trump each other"
175
- end
176
- end
177
170
  end
178
- def to_json(*)
179
- { :name => name, :trumps => trumps, :prerequisite => prerequisite, :options => options }.to_json
171
+ def as_json(*)
172
+ { :name => name, :trumps => trumps, :prerequisite => prerequisite, :options => options }
180
173
  end
181
174
  def inspect
182
175
  "<Characterizable::Characteristic name=#{name.inspect} trumps=#{trumps.inspect} prerequisite=#{prerequisite.inspect} options=#{options.inspect}>"
183
176
  end
184
- def trumped_by
185
- characteristics.select { |_, c| c.trumps.include? name }
186
- end
187
177
  def characteristics
188
178
  base.characteristics
189
179
  end
190
- def value(target)
191
- case target
180
+ def value(universe)
181
+ case universe
192
182
  when Hash
193
- target[name]
183
+ universe[name]
194
184
  else
195
- target.send name if target.respond_to?(name)
185
+ universe.send name if universe.respond_to?(name)
196
186
  end
197
187
  end
198
- def known?(target)
199
- !value(target).nil?
188
+ def known?(universe)
189
+ not value(universe).nil?
200
190
  end
201
- def potential?(target)
202
- !known?(target) and revealed? target and untrumped? target
191
+ def potential?(universe)
192
+ not known?(universe) and revealed? universe and not trumped? universe
203
193
  end
204
- def effective?(target)
205
- known?(target) and revealed? target and untrumped? target
194
+ def effective?(universe, ignoring = nil)
195
+ known?(universe) and revealed? universe and not trumped? universe, ignoring
206
196
  end
207
- def untrumped?(target)
208
- return true if trumped_by.empty?
209
- trumped_by.none? do |_, c|
210
- c.effective? target
197
+ def trumped?(universe, ignoring = nil)
198
+ characteristics.each do |_, other|
199
+ if other.trumps.include? name and not ignoring == other.name
200
+ if trumps.include? other.name
201
+ # special case: mutual trumping. current characteristic is trumped if its friend is otherwise effective and it is not otherwise effective
202
+ return true if other.effective? universe, name and not effective? universe, other.name
203
+ else
204
+ return true if other.effective? universe
205
+ end
206
+ end
211
207
  end
208
+ false
212
209
  end
213
- def revealed?(target)
210
+ def revealed?(universe)
214
211
  return true if prerequisite.nil?
215
- characteristics[prerequisite].effective? target
212
+ characteristics[prerequisite].effective? universe
216
213
  end
217
214
  include Blockenspiel::DSL
218
215
  def reveals(other_name, other_options = {}, &block)
@@ -2,7 +2,6 @@ require 'helper'
2
2
 
3
3
  # just to see if it hurts
4
4
  require 'active_support/json'
5
- require 'to_json_fix'
6
5
 
7
6
  class Characterizable::Characteristic
8
7
  def hidden?
@@ -50,6 +49,16 @@ class SimpleAutomobile
50
49
  end
51
50
  end
52
51
 
52
+ class Universe
53
+ include Characterizable
54
+ attr_accessor :matter
55
+ attr_accessor :antimatter
56
+ characterize do
57
+ has :matter, :trumps => :antimatter
58
+ has :antimatter, :trumps => :matter
59
+ end
60
+ end
61
+
53
62
  class TestCharacterizable < Test::Unit::TestCase
54
63
  should "let you define the effective characteristics of a class" do
55
64
  assert_nothing_raised do
@@ -410,19 +419,31 @@ class TestCharacterizable < Test::Unit::TestCase
410
419
  end
411
420
  end
412
421
  end
413
-
414
- # this used to cause infinite loops
415
- should 'not let two characteristics trump each other' do
416
- assert_raises ::Characterizable::CyclicalTrumping do
417
- class Universe
418
- include Characterizable
419
- attr_accessor :matter
420
- attr_accessor :antimatter
421
- characterize do
422
- has :matter, :trumps => :antimatter
423
- has :antimatter, :trumps => :matter
424
- end
425
- end
426
- end
422
+
423
+ should "have mutual trumping cancel itself out instead of causing an infinite loop" do
424
+ a = Universe.new
425
+ assert_equal [], a.characteristics.effective.keys
426
+ assert_equal [:matter, :antimatter], a.characteristics.potential.keys
427
+ assert_equal [], a.characteristics.wasted.keys
428
+ assert_equal [], a.characteristics.lacking.keys
429
+ assert_equal [], a.characteristics.trumped.keys
430
+ a.matter = :foo
431
+ assert_equal [:matter], a.characteristics.effective.keys
432
+ assert_equal [], a.characteristics.potential.keys
433
+ assert_equal [], a.characteristics.wasted.keys
434
+ assert_equal [], a.characteristics.lacking.keys
435
+ assert_equal [], a.characteristics.trumped.keys
436
+ a.antimatter = :bar
437
+ assert_equal [:matter, :antimatter], a.characteristics.effective.keys
438
+ assert_equal [], a.characteristics.potential.keys
439
+ assert_equal [], a.characteristics.wasted.keys
440
+ assert_equal [], a.characteristics.lacking.keys
441
+ assert_equal [], a.characteristics.trumped.keys
442
+ a.matter = nil
443
+ assert_equal [:antimatter], a.characteristics.effective.keys
444
+ assert_equal [], a.characteristics.potential.keys
445
+ assert_equal [], a.characteristics.wasted.keys
446
+ assert_equal [], a.characteristics.lacking.keys
447
+ assert_equal [], a.characteristics.trumped.keys
427
448
  end
428
449
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: characterizable
3
3
  version: !ruby/object:Gem::Version
4
- hash: 1
4
+ hash: 63
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 15
10
- version: 0.0.15
9
+ - 16
10
+ version: 0.0.16
11
11
  platform: ruby
12
12
  authors:
13
13
  - Andy Rossmeissl
@@ -51,26 +51,10 @@ dependencies:
51
51
  version: 2.3.5
52
52
  type: :runtime
53
53
  version_requirements: *id002
54
- - !ruby/object:Gem::Dependency
55
- name: to_json_fix
56
- prerelease: false
57
- requirement: &id003 !ruby/object:Gem::Requirement
58
- none: false
59
- requirements:
60
- - - ">="
61
- - !ruby/object:Gem::Version
62
- hash: 29
63
- segments:
64
- - 0
65
- - 0
66
- - 1
67
- version: 0.0.1
68
- type: :development
69
- version_requirements: *id003
70
54
  - !ruby/object:Gem::Dependency
71
55
  name: shoulda
72
56
  prerelease: false
73
- requirement: &id004 !ruby/object:Gem::Requirement
57
+ requirement: &id003 !ruby/object:Gem::Requirement
74
58
  none: false
75
59
  requirements:
76
60
  - - ">="
@@ -80,7 +64,7 @@ dependencies:
80
64
  - 0
81
65
  version: "0"
82
66
  type: :development
83
- version_requirements: *id004
67
+ version_requirements: *id003
84
68
  description: Characterize the relationship between "attributes" (getters/setters) of instances of a class
85
69
  email: seamus@abshere.net
86
70
  executables: []