characterizable 0.1.1 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.1
1
+ 0.1.2
@@ -1,47 +1,47 @@
1
1
  # Generated by jeweler
2
2
  # DO NOT EDIT THIS FILE DIRECTLY
3
- # Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
4
  # -*- encoding: utf-8 -*-
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{characterizable}
8
- s.version = "0.1.1"
8
+ s.version = "0.1.2"
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"]
12
- s.date = %q{2010-11-04}
12
+ s.date = %q{2010-11-18}
13
13
  s.description = %q{Characterize the relationship between "attributes" (getters/setters) of instances of a class}
14
14
  s.email = %q{seamus@abshere.net}
15
15
  s.extra_rdoc_files = [
16
16
  "LICENSE",
17
- "README.rdoc"
17
+ "README.rdoc"
18
18
  ]
19
19
  s.files = [
20
20
  ".document",
21
- ".gitignore",
22
- "LICENSE",
23
- "README.rdoc",
24
- "Rakefile",
25
- "VERSION",
26
- "characterizable.gemspec",
27
- "lib/characterizable.rb",
28
- "lib/characterizable/base.rb",
29
- "lib/characterizable/better_hash.rb",
30
- "lib/characterizable/characteristic.rb",
31
- "lib/characterizable/snapshot.rb",
32
- "test/characterizable/test_characteristic.rb",
33
- "test/helper.rb",
34
- "test/test_characterizable.rb"
21
+ "LICENSE",
22
+ "README.rdoc",
23
+ "Rakefile",
24
+ "VERSION",
25
+ "characterizable.gemspec",
26
+ "lib/characterizable.rb",
27
+ "lib/characterizable/base.rb",
28
+ "lib/characterizable/better_hash.rb",
29
+ "lib/characterizable/characteristic.rb",
30
+ "lib/characterizable/snapshot.rb",
31
+ "test/characterizable/test_characteristic.rb",
32
+ "test/helper.rb",
33
+ "test/support/complex_automobile.rb",
34
+ "test/test_characterizable.rb"
35
35
  ]
36
36
  s.homepage = %q{http://github.com/seamusabshere/characterizable}
37
- s.rdoc_options = ["--charset=UTF-8"]
38
37
  s.require_paths = ["lib"]
39
38
  s.rubygems_version = %q{1.3.7}
40
39
  s.summary = %q{Characterize instances of a class}
41
40
  s.test_files = [
42
41
  "test/characterizable/test_characteristic.rb",
43
- "test/helper.rb",
44
- "test/test_characterizable.rb"
42
+ "test/helper.rb",
43
+ "test/support/complex_automobile.rb",
44
+ "test/test_characterizable.rb"
45
45
  ]
46
46
 
47
47
  if s.respond_to? :specification_version then
@@ -46,24 +46,36 @@ module Characterizable
46
46
  not known?(universe) and revealed? universe and not trumped? universe
47
47
  end
48
48
 
49
- def effective?(universe, ignoring = nil)
50
- known?(universe) and revealed? universe and not trumped? universe, ignoring
49
+ def effective?(universe, ignoring = [])
50
+ known?(universe) and
51
+ revealed?(universe) and not
52
+ trumped?(universe, ignoring)
51
53
  end
52
54
 
53
- def trumped?(universe, ignoring = nil)
55
+ def trumped?(universe, ignoring = [])
54
56
  characteristics.each do |_, other|
55
- if other.trumps.include? name and not ignoring == other.name
56
- if trumps.include? other.name
57
- # special case: mutual trumping. current characteristic is trumped if its friend is otherwise effective and it is not otherwise effective
58
- return true if other.effective? universe, name and not effective? universe, other.name
59
- else
60
- return true if other.effective? universe
57
+ next if ignoring.include?(other.name)
58
+
59
+ if other.can_trump? self
60
+ if can_trump?(other)
61
+ return mutually_trumped?(universe, other, ignoring)
62
+ elsif other.effective?(universe, ignoring + [name])
63
+ return true
61
64
  end
62
65
  end
63
66
  end
64
67
  false
65
68
  end
66
69
 
70
+ def can_trump?(other)
71
+ trumps.include?(other.name)
72
+ end
73
+
74
+ def mutually_trumped?(universe, other, ignoring)
75
+ # special case: mutual trumping. current characteristic is trumped if its friend is otherwise effective and it is not otherwise effective
76
+ other.effective?(universe, ignoring + [name]) and not effective?(universe, ignoring + [other.name])
77
+ end
78
+
67
79
  def revealed?(universe)
68
80
  return true if prerequisite.nil?
69
81
  characteristics[prerequisite].effective? universe
@@ -5,19 +5,160 @@ class Character
5
5
  end
6
6
 
7
7
  class Characterizable::CharacteristicTest < Test::Unit::TestCase
8
- context 'display' do
8
+ context Characterizable::Characteristic do
9
9
  setup do
10
- @universe = { :charisma => 'hearty' }
10
+ @universe = ComplexAutomobile.new
11
11
  end
12
12
 
13
- should 'not display a custom format if display option not given' do
14
- char = Characterizable::Characteristic.new(Character, :charisma, {})
15
- assert_nil char.display(@universe)
13
+ context '#display' do
14
+ setup do
15
+ @universe = { :charisma => 'hearty' }
16
+ end
17
+
18
+ should 'not display a custom format if display option not given' do
19
+ char = Characterizable::Characteristic.new(Character, :charisma, {})
20
+ assert_nil char.display(@universe)
21
+ end
22
+ should 'display a custom format if display option is given' do
23
+ char = Characterizable::Characteristic.new(Character, :charisma,
24
+ { :display => lambda { |c| "Level: #{c}" } }) {}
25
+ assert_equal 'Level: hearty', char.display(@universe)
26
+ end
27
+ end
28
+
29
+ context '#value' do
30
+ context 'universe is a hash' do
31
+ setup do
32
+ @universe = { :daily_duration => 1 }
33
+ end
34
+ should 'return the value for a characteristic' do
35
+ characteristic = Characterizable::Characteristic.new(ComplexAutomobile, :daily_duration, {})
36
+ assert_equal 1, characteristic.value(@universe)
37
+ end
38
+ should 'return nil for a nonexistent characteristic' do
39
+ characteristic = Characterizable::Characteristic.new(ComplexAutomobile, :monthly_duration, {})
40
+ assert_nil characteristic.value(@universe)
41
+ end
42
+ end
43
+ context 'universe is a non-hash' do
44
+ setup do
45
+ @universe.daily_duration = 1
46
+ end
47
+ should 'return the value for a characteristic' do
48
+ characteristic = Characterizable::Characteristic.new(ComplexAutomobile, :daily_duration, {})
49
+ assert_equal 1, characteristic.value(@universe)
50
+ end
51
+ should 'return nil for a nonexistent characteristic' do
52
+ characteristic = Characterizable::Characteristic.new(ComplexAutomobile, :na, {})
53
+ assert_nil characteristic.value(@universe)
54
+ end
55
+ end
56
+ end
57
+
58
+ context '#known?' do
59
+ setup do
60
+ @characteristic = @universe.characterizable_base.characteristics[:daily_duration]
61
+ end
62
+ should 'return true if the characteristic is not nil' do
63
+ @universe.daily_duration = 1
64
+ assert @characteristic.known?(@universe)
65
+ end
66
+ should 'return false if the characteristic is nil' do
67
+ assert !@characteristic.known?(@universe)
68
+ end
16
69
  end
17
- should 'display a custom format if display option is given' do
18
- char = Characterizable::Characteristic.new(Character, :charisma,
19
- { :display => lambda { |c| "Level: #{c}" } }) {}
20
- assert_equal 'Level: hearty', char.display(@universe)
70
+
71
+ context '#revealed?' do
72
+ should 'return true if there are no prerequisites' do
73
+ characteristic = @universe.characterizable_base.characteristics[:size_class]
74
+ assert characteristic.revealed?(@universe)
75
+ end
76
+ should 'return true if its prerequisite is effective' do
77
+ @universe.acquisition = '1'
78
+ characteristic = @universe.characterizable_base.characteristics[:retirement]
79
+ assert characteristic.revealed?(@universe)
80
+ end
81
+ should 'return false if its prerequisite is not effective' do
82
+ characteristic = @universe.characterizable_base.characteristics[:retirement]
83
+ assert !characteristic.revealed?(@universe)
84
+ end
85
+ end
86
+
87
+ context '#trumped?' do
88
+ context 'ignoring is empty' do
89
+ should 'return true if trumped by another effective characteristic' do
90
+ @universe.fuel_efficiency = '5'
91
+ char = @universe.characterizable_base.characteristics[:urbanity]
92
+ assert char.trumped?(@universe)
93
+ end
94
+ should 'return false if trumped by another ineffective characteristic' do
95
+ char = @universe.characterizable_base.characteristics[:weekly_distance_estimate]
96
+ assert !char.trumped?(@universe)
97
+ end
98
+ should 'return true if mutually trumped, other is effective, and currently not effective' do
99
+ @universe.daily_distance_estimate = '5'
100
+ char = @universe.characterizable_base.characteristics[:weekly_distance_estimate]
101
+ assert char.trumped?(@universe)
102
+ end
103
+ should 'return false if mutually trumped, other is effective, and currently am effective' do
104
+ @universe.daily_distance_estimate = '5'
105
+ @universe.weekly_distance_estimate = '6'
106
+ char = @universe.characterizable_base.characteristics[:weekly_distance_estimate]
107
+ assert !char.trumped?(@universe)
108
+ end
109
+ should 'return false if mutually trumped, other is not effective' do
110
+ char = @universe.characterizable_base.characteristics[:weekly_distance_estimate]
111
+ assert !char.trumped?(@universe)
112
+ end
113
+ end
114
+ context 'ignoring is set' do
115
+ should 'return false if trumped by another effective characteristic that should be ignored' do
116
+ @universe.daily_distance_estimate = '5'
117
+ char = @universe.characterizable_base.characteristics[:weekly_distance_estimate]
118
+ assert !char.trumped?(@universe, [:daily_distance_estimate])
119
+ end
120
+ end
121
+ end
122
+
123
+ # has :daily_distance_estimate, :trumps => [:weekly_distance_estimate, :annual_distance_estimate, :daily_duration], :measures => :length
124
+ # has :daily_duration, :trumps => [:annual_distance_estimate, :weekly_distance_estimate, :daily_distance_estimate], :measures => :time
125
+ # has :weekly_distance_estimate, :trumps => [:annual_distance_estimate, :daily_distance_estimate, :daily_duration], :measures => :length
126
+ # has :annual_distance_estimate, :trumps => [:weekly_distance_estimate, :daily_distance_estimate, :daily_duration], :measures => :length
127
+ context '#effective?' do
128
+ should 'return true if known, revealed, and not trumped' do
129
+ characteristic = @universe.characterizable_base.characteristics[:daily_duration]
130
+ @universe.daily_duration = '1'
131
+ assert characteristic.effective?(@universe)
132
+ end
133
+ should 'return false if known, reavaled, and trumped' do
134
+ @universe.annual_distance_estimate = "33796.2"
135
+ @universe.daily_duration = "3.0"
136
+ char = @universe.characterizable_base.
137
+ characteristics[:daily_duration]
138
+ assert_nothing_raised do
139
+ char.effective?(@universe)
140
+ end
141
+ end
142
+ should 'return false if known, but not revealed' do
143
+ @universe.model_year = '2007'
144
+ char = @universe.characterizable_base.
145
+ characteristics[:model_year]
146
+ assert !char.effective?(@universe)
147
+ end
148
+ should 'return false if not known' do
149
+ char = @universe.characterizable_base.
150
+ characteristics[:model_year]
151
+ assert !char.effective?(@universe)
152
+ end
153
+ should 'not infinitely recurse if there is a 3-way mutual trumping' do
154
+ @universe.annual_distance_estimate = "33796.2"
155
+ @universe.daily_duration = "3.0"
156
+ @universe.weekly_distance_estimate = "804.672"
157
+ char = @universe.characterizable_base.characteristics[:daily_duration]
158
+ assert_nothing_raised do
159
+ char.effective?(@universe)
160
+ end
161
+ end
21
162
  end
22
163
  end
23
164
  end
@@ -13,3 +13,7 @@ class Test::Unit::TestCase
13
13
  assert_equal Set.new(a), Set.new(b)
14
14
  end
15
15
  end
16
+
17
+ Dir.glob(File.expand_path('support/**/*', File.dirname(__FILE__))).each do |file|
18
+ require file
19
+ end
@@ -0,0 +1,35 @@
1
+ class ComplexAutomobile
2
+ attr_accessor :make, :model_year, :model, :variant
3
+ attr_accessor :fuel_type, :fuel_efficiency, :urbanity
4
+ attr_accessor :hybridity
5
+ attr_accessor :daily_distance_estimate
6
+ attr_accessor :daily_duration
7
+ attr_accessor :weekly_distance_estimate
8
+ attr_accessor :annual_distance_estimate
9
+ attr_accessor :acquisition
10
+ attr_accessor :retirement
11
+ attr_accessor :size_class
12
+ attr_accessor :timeframe
13
+ include Characterizable
14
+ characterize do
15
+ has :make do |make|
16
+ make.reveals :model_year do |model_year|
17
+ model_year.reveals :model, :trumps => :size_class do |model|
18
+ model.reveals :variant, :trumps => :hybridity
19
+ end
20
+ end
21
+ end
22
+ has :fuel_type
23
+ has :fuel_efficiency, :trumps => [:urbanity, :hybridity], :measures => :length_per_volume
24
+ has :urbanity, :measures => :percentage
25
+ has :hybridity
26
+ has :daily_distance_estimate, :trumps => [:weekly_distance_estimate, :annual_distance_estimate, :daily_duration], :measures => :length
27
+ has :daily_duration, :trumps => [:annual_distance_estimate, :weekly_distance_estimate, :daily_distance_estimate], :measures => :time
28
+ has :weekly_distance_estimate, :trumps => [:annual_distance_estimate, :daily_distance_estimate, :daily_duration], :measures => :length
29
+ has :annual_distance_estimate, :trumps => [:weekly_distance_estimate, :daily_distance_estimate, :daily_duration], :measures => :length
30
+ has :acquisition
31
+ has :retirement, :prerequisite => :acquisition
32
+ has :size_class
33
+ end
34
+ end
35
+
@@ -23,17 +23,9 @@ class Automobile
23
23
  end
24
24
  has :record_creation_date, :hidden => true
25
25
  has :size_class
26
- # has :fuel_type
27
- # has :fuel_efficiency, :trumps => [:urbanity, :hybridity], :measures => :length_per_volume
28
- # has :urbanity, :measures => :percentage
29
26
  has :hybridity
30
27
  has :daily_distance_estimate, :trumps => [:weekly_distance_estimate, :annual_distance_estimate, :daily_duration], :measures => :length #, :weekly_fuel_cost, :annual_fuel_cost]
31
28
  has :daily_distance_oracle_estimate, :trumps => :daily_distance_estimate
32
- # has :daily_duration, :trumps => [:annual_distance_estimate, :weekly_distance_estimate, :daily_distance_estimate], :measures => :time #, :weekly_fuel_cost, :annual_fuel_cost]
33
- # has :weekly_distance_estimate, :trumps => [:annual_distance_estimate, :daily_distance_estimate, :daily_duration], :measures => :length #, :weekly_fuel_cost, :annual_fuel_cost]
34
- # has :annual_distance_estimate, :trumps => [:weekly_distance_estimate, :daily_distance_estimate, :daily_duration], :measures => :length #, :weekly_fuel_cost, :annual_fuel_cost]
35
- # has :acquisition
36
- # has :retirement
37
29
  end
38
30
  end
39
31
 
@@ -455,4 +447,17 @@ class TestCharacterizable < Test::Unit::TestCase
455
447
  sa.variant = 'Extreme Edition'
456
448
  assert_equal 'Featuring Extreme Edition', sa.display_characteristic(:variant)
457
449
  end
450
+
451
+ context 'infinite recursion' do
452
+ should 'not happen' do
453
+ c = ComplexAutomobile.new
454
+ c.annual_distance_estimate = "33796.2"
455
+ c.daily_duration = "3.0"
456
+ c.timeframe = "2010-01-01/2011-01-01"
457
+ c.weekly_distance_estimate = "804.672"
458
+ assert_nothing_raised do
459
+ c.characteristics
460
+ end
461
+ end
462
+ end
458
463
  end
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 1
8
- - 1
9
- version: 0.1.1
8
+ - 2
9
+ version: 0.1.2
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-11-04 00:00:00 -04:00
18
+ date: 2010-11-18 00:00:00 -05:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
@@ -72,7 +72,6 @@ extra_rdoc_files:
72
72
  - README.rdoc
73
73
  files:
74
74
  - .document
75
- - .gitignore
76
75
  - LICENSE
77
76
  - README.rdoc
78
77
  - Rakefile
@@ -85,14 +84,15 @@ files:
85
84
  - lib/characterizable/snapshot.rb
86
85
  - test/characterizable/test_characteristic.rb
87
86
  - test/helper.rb
87
+ - test/support/complex_automobile.rb
88
88
  - test/test_characterizable.rb
89
89
  has_rdoc: true
90
90
  homepage: http://github.com/seamusabshere/characterizable
91
91
  licenses: []
92
92
 
93
93
  post_install_message:
94
- rdoc_options:
95
- - --charset=UTF-8
94
+ rdoc_options: []
95
+
96
96
  require_paths:
97
97
  - lib
98
98
  required_ruby_version: !ruby/object:Gem::Requirement
@@ -121,4 +121,5 @@ summary: Characterize instances of a class
121
121
  test_files:
122
122
  - test/characterizable/test_characteristic.rb
123
123
  - test/helper.rb
124
+ - test/support/complex_automobile.rb
124
125
  - test/test_characterizable.rb
data/.gitignore DELETED
@@ -1,21 +0,0 @@
1
- ## MAC OS
2
- .DS_Store
3
-
4
- ## TEXTMATE
5
- *.tmproj
6
- tmtags
7
-
8
- ## EMACS
9
- *~
10
- \#*
11
- .\#*
12
-
13
- ## VIM
14
- *.swp
15
-
16
- ## PROJECT::GENERAL
17
- coverage
18
- rdoc
19
- pkg
20
-
21
- ## PROJECT::SPECIFIC