jeffp-enumerated_attribute 0.1.2 → 0.1.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,244 @@
1
+ require 'test_in_memory'
2
+ require 'enumerated_attribute'
3
+ require 'active_record'
4
+ require 'race_car'
5
+
6
+ describe "RaceCar" do
7
+
8
+ it "should have dynamic predicate methods for :gear attribute" do
9
+ r=RaceCar.new
10
+ r.gear = :second
11
+ r.gear_is_in_second?.should be_true
12
+ r.gear_not_in_second?.should be_false
13
+ r.gear_is_nil?.should be_false
14
+ r.gear_is_not_nil?.should be_true
15
+ end
16
+
17
+ it "should have working dynamic predicate methods on retrieved objects" do
18
+ r=RaceCar.new
19
+ r.gear = :second
20
+ r.save!
21
+
22
+ s=RaceCar.find r.id
23
+ s.should_not be_nil
24
+ s.gear_is_in_second?.should be_true
25
+ s.gear_is_not_in_second?.should be_false
26
+ s.gear_is_nil?.should be_false
27
+ s.gear_is_not_nil?.should be_true
28
+ end
29
+
30
+ it "should find record by enumerated column :gear attributes" do
31
+ r=RaceCar.new
32
+ r.gear = :second
33
+ r.name = 'special'
34
+ r.save!
35
+
36
+ s=RaceCar.find_by_gear_and_name(:second, 'special')
37
+ s.should_not be_nil
38
+ s.id.should == r.id
39
+ end
40
+
41
+ it "should initialize according to enumerated attribute definitions" do
42
+ r = RaceCar.new
43
+ r.gear.should == :neutral
44
+ r.choke.should == :none
45
+ end
46
+
47
+ it "should create new instance using block" do
48
+ r = RaceCar.new do |r|
49
+ r.gear = :first
50
+ r.choke = :medium
51
+ r.lights = 'on'
52
+ end
53
+ r.gear.should == :first
54
+ r.lights.should == 'on'
55
+ r.choke.should == :medium
56
+ end
57
+
58
+ it "should initialize using parameter hash" do
59
+ r=RaceCar.new(:name=>'FastFurious', :gear=>:second, :lights=>'on', :choke=>:medium)
60
+ r.gear.should == :second
61
+ r.lights.should == 'on'
62
+ r.choke.should == :medium
63
+ end
64
+
65
+ it "should convert non-column enumerated attributes from string to symbols" do
66
+ r=RaceCar.new
67
+ r.choke = 'medium'
68
+ r.choke.should == :medium
69
+ r.save!
70
+ end
71
+
72
+ it "should convert enumerated column attributes from string to symbols" do
73
+ r=RaceCar.new
74
+ r.gear = 'second'
75
+ r.gear.should == :second
76
+ r.save!
77
+
78
+ s=RaceCar.find r.id
79
+ s.gear.should == :second
80
+ end
81
+
82
+ it "should not convert non-enumerated column attributes from string to symbols" do
83
+ r=RaceCar.new
84
+ r.lights = 'off'
85
+ r.lights.should == 'off'
86
+ r.save!
87
+
88
+ s=RaceCar.find r.id
89
+ s.lights.should == 'off'
90
+ end
91
+
92
+ it "should raise InvalidEnumeration when parametrically initialized with :gear=>:drive" do
93
+ r=RaceCar.new
94
+ lambda{ r.gear= :drive}.should raise_error(EnumeratedAttribute::InvalidEnumeration)
95
+ end
96
+
97
+ it "should raise InvalidEnumeration when parametrically initialized with :choke=>:all" do
98
+ r=RaceCar.new
99
+ lambda{ r.choke= :all}.should raise_error(EnumeratedAttribute::InvalidEnumeration)
100
+ end
101
+
102
+ it "should return non-column enumerated attributes from [] method" do
103
+ r = RaceCar.new
104
+ r[:choke].should == :none
105
+ end
106
+
107
+ it "should return enumerated column attributes from [] method" do
108
+ r=RaceCar.new
109
+ r.gear = :neutral
110
+ r[:gear].should == :neutral
111
+ end
112
+
113
+ it "should set non-column enumerated attributes with []= method" do
114
+ r=RaceCar.new
115
+ r[:choke] = :medium
116
+ r.choke.should == :medium
117
+ end
118
+
119
+ it "should set enumerated column attriubtes with []= method" do
120
+ r=RaceCar.new
121
+ r[:gear] = :second
122
+ r.gear.should == :second
123
+ end
124
+
125
+ it "should raise InvalidEnumeration when setting enumerated column attribute with []= method" do
126
+ r=RaceCar.new
127
+ lambda{ r[:gear]= :drive }.should raise_error(EnumeratedAttribute::InvalidEnumeration)
128
+ end
129
+
130
+ it "should set and retrieve string for non-enumerated column attributes with []=" do
131
+ r=RaceCar.new
132
+ r[:lights] = 'on'
133
+ r.lights.should == 'on'
134
+ r[:lights].should == 'on'
135
+ end
136
+
137
+ it "should set and retrieve symbol for non-enumerated column attributes with []=" do
138
+ r=RaceCar.new
139
+ r[:lights] = :on
140
+ r.lights.should == :on
141
+ r[:lights].should == :on
142
+ end
143
+
144
+ it "should raise InvalidEnumeration for invalid enum passed to attributes=" do
145
+ r=RaceCar.new
146
+ lambda { r.attributes = {:lights=>'off', :gear =>:drive} }.should raise_error(EnumeratedAttribute::InvalidEnumeration)
147
+ end
148
+
149
+ it "should update_attribute for enumerated column attribute" do
150
+ r=RaceCar.new
151
+ r.gear = :first
152
+ r.save!
153
+ r.update_attribute(:gear, :second)
154
+ r.gear.should == :second
155
+
156
+ s=RaceCar.find r.id
157
+ s.gear.should == :second
158
+ end
159
+
160
+ it "should update_attribute for non-enumerated column attribute" do
161
+ r=RaceCar.new
162
+ r.lights = 'on'
163
+ r.save!
164
+ r.update_attribute(:lights, 'off')
165
+ r.lights.should == 'off'
166
+
167
+ s=RaceCar.find r.id
168
+ s.lights.should == 'off'
169
+ end
170
+
171
+ it "should update_attributes for both non- and enumerated column attributes" do
172
+ r=RaceCar.new
173
+ r.gear = :first
174
+ r.lights = 'off'
175
+ r.save!
176
+ r.update_attributes({:gear=>:second, :lights=>'on'})
177
+ s=RaceCar.find r.id
178
+ s.gear.should == :second
179
+ s.lights.should == 'on'
180
+ s.update_attributes({:gear=>'over_drive', :lights=>'off'})
181
+ t=RaceCar.find s.id
182
+ t.gear.should == :over_drive
183
+ t.lights.should == 'off'
184
+ end
185
+
186
+ it "should provide symbol values for enumerated column attributes from the :attributes method" do
187
+ end
188
+
189
+ it "should provide normal values for non-enumerated column attributes from the :attributes method" do
190
+ end
191
+
192
+ it "should raise ArgumentError when setting invalid enumertion value with :attributes= method" do
193
+ end
194
+
195
+ it "should save and retrieve its name" do
196
+ r = RaceCar.new
197
+ r.name= 'Green Meanie'
198
+ r.save!
199
+
200
+ s = RaceCar.find r.id
201
+ s.should_not be_nil
202
+ s.name.should == 'Green Meanie'
203
+ end
204
+
205
+ it "should save and retrieve symbols for enumerated column attribute" do
206
+ r = RaceCar.new
207
+ r.gear = :over_drive
208
+ r.save!
209
+
210
+ s = RaceCar.find r.id
211
+ #s.should_not be_nil
212
+ s.gear.should == :over_drive
213
+ end
214
+
215
+ it "should not save values for non-column enumerated attributes" do
216
+ r=RaceCar.new
217
+ r.choke = :medium
218
+ r.save!
219
+
220
+ s=RaceCar.find r.id
221
+ s.choke.should == :none
222
+ end
223
+
224
+ it "should save string and retrieve string for non-enumerated column attributes" do
225
+ r =RaceCar.new
226
+ r.lights = 'on'
227
+ r.save!
228
+
229
+ s = RaceCar.find r.id
230
+ s.lights.should == 'on'
231
+ s[:lights].should == 'on'
232
+ end
233
+
234
+ it "should save symbol and retrieve string for non-enumerated column attributes" do
235
+ r =RaceCar.new
236
+ r.lights = :off
237
+ r.save!
238
+
239
+ s = RaceCar.find r.id
240
+ s.lights.should == "--- :off\n"
241
+ s[:lights].should == "--- :off\n"
242
+ end
243
+
244
+ end
@@ -0,0 +1,3 @@
1
+ require 'test_in_memory'
2
+ require '../../lib/enumerated_attribute'
3
+ require 'race_car'
@@ -0,0 +1,10 @@
1
+
2
+ class RaceCar < ActiveRecord::Base
3
+ enum_attr :gear, %w(reverse ^neutral first second over_drive)
4
+ enum_attr :choke, %w(^none medium full)
5
+ end
6
+
7
+ #gear = enumerated column attribute
8
+ #choke = enumerated non-column attribute
9
+ #lights = non-enumerated column attribute
10
+
@@ -0,0 +1,21 @@
1
+
2
+ require 'rubygems'
3
+
4
+ gem 'activerecord', ENV['AR_VERSION'] ? "=#{ENV['AR_VERSION']}" : '>=2.1.0'
5
+ require 'active_record'
6
+
7
+ ActiveRecord::Base.establish_connection({'adapter' => 'sqlite3', 'database' => ':memory:'})
8
+ ActiveRecord::Base.logger = Logger.new("#{File.dirname(__FILE__)}/active_record.log")
9
+
10
+ connection = ActiveRecord::Base.connection
11
+ connection.create_table(:race_cars, :force=>true) do |t|
12
+ t.string :name
13
+ t.string :gear
14
+ t.string :lights
15
+ end
16
+ connection.create_table(:bicycles, :force=>true) do |t|
17
+ t.string :name
18
+ t.string :speed
19
+ t.string :gear
20
+ end
21
+
data/spec/car.rb CHANGED
@@ -2,36 +2,66 @@ require 'enumerated_attribute'
2
2
 
3
3
  #used to test that method_missing chaining plays nice in inheritance situations
4
4
 
5
- class Vehicle
6
-
7
- attr_accessor :vehicle_method_hit
8
-
5
+ class Vehicle
6
+ def vehicle_method_missing_called?; @vehicle_method_missing_called; end
7
+ def self.vehicle_new_called?; @@vehicle_new_called; end
8
+ def self.reset; @@vehicle_new_called = false; end
9
+
10
+ @@vehicle_new_called = false
9
11
  def initialize
10
- @vehicle_method_hit = false
12
+ @vehicle_method_missing_called = false
13
+ super
11
14
  end
12
15
 
13
16
  def method_missing(methId, *args)
14
- @vehicle_method_hit = true
17
+ @vehicle_method_missing_called = true
15
18
  #end here
16
19
  end
20
+
21
+ def self.new
22
+ @@vehicle_new_called = true
23
+ super
24
+ end
17
25
 
18
- alias :vmh :vehicle_method_hit
19
-
20
26
  end
21
27
 
22
- class Car < Vehicle
23
- attr_accessor :car_method_hit
24
-
28
+ class CarWithMethods < Vehicle
29
+ def car_method_missing_called?; @car_method_missing_called; end
30
+ def self.car_new_called?; @@car_new_called; end
31
+ def self.reset; @@car_new_called = false; super; end
32
+
33
+ def self.new
34
+ @@car_new_called = true
35
+ super
36
+ end
37
+
38
+ def method_missing(methId, *args)
39
+ @car_method_missing_called = true
40
+ super
41
+ end
42
+
43
+ enum_attr :gear, %w(reverse ^neutral drive)
44
+
45
+ @@car_new_called = false
25
46
  def initialize
47
+ @car_method_missing_called = false
26
48
  super
27
- @car_method_hit = false
28
49
  end
29
50
 
30
- def method_missing(methId, *args)
31
- @car_method_hit = true
51
+ end
52
+
53
+ class CarWithoutMethods < Vehicle
54
+ def car_method_missing_called?; @car_method_missing_called; end
55
+ def self.car_new_called?; @@car_new_called; end
56
+ def self.reset; @@car_new_called = false; super; end
57
+
58
+ enum_attr :gear, %w(reverse ^neutral drive)
59
+
60
+ @@car_new_called = false
61
+ def initialize
62
+ @car_method_missing_called = false
32
63
  super
33
64
  end
34
65
 
35
- enum_attr :gear, %w(reverse ^neutral drive)
36
- alias :cmt :car_method_hit
37
66
  end
67
+
@@ -0,0 +1,73 @@
1
+ require 'car'
2
+
3
+ #used to test that method_missing chaining plays nice in inheritance situation
4
+
5
+ describe "CarWithMethods" do
6
+
7
+ it "should initialize Car and Vehicle _method_missing_called? to false" do
8
+ c= CarWithMethods.new
9
+ c.car_method_missing_called?.should be_false
10
+ c.vehicle_method_missing_called?.should be_false
11
+ end
12
+
13
+ it "should initialize Car and Vehicle _new_called? to false" do
14
+ CarWithMethods.reset
15
+ CarWithMethods.car_new_called?.should be_false
16
+ CarWithMethods.vehicle_new_called?.should be_false
17
+ end
18
+
19
+ it "should initialize :gear to :neutral" do
20
+ c = CarWithMethods.new
21
+ c.gear.should == :neutral
22
+ end
23
+
24
+ it "should hit both new methods for Car and Vehicle on instantiation" do
25
+ CarWithMethods.reset
26
+ CarWithMethods.new
27
+ CarWithMethods.car_new_called?.should be_true
28
+ CarWithMethods.vehicle_new_called?.should be_true
29
+ end
30
+
31
+ it "should not hit method_missing when calling dynamic predicate method :gear_is_in_reverse?" do
32
+ c = CarWithMethods.new
33
+ c.gear_is_in_reverse?
34
+ c.car_method_missing_called?.should be_false
35
+ c.vehicle_method_missing_called?.should be_false
36
+ end
37
+
38
+ it "should hit Car and Vehicle method_missing when calling unsupported dynamic predicate method" do
39
+ c = CarWithMethods.new
40
+ c.parking_break_is_on?
41
+ c.car_method_missing_called?.should be_true
42
+ c.vehicle_method_missing_called?.should be_true
43
+ end
44
+ end
45
+
46
+ describe "CarWithoutMethods" do
47
+
48
+ it "should set Car and Vehicle _new_called? to false when calling :reset" do
49
+ CarWithoutMethods.reset
50
+ CarWithoutMethods.car_new_called?.should be_false
51
+ CarWithoutMethods.vehicle_new_called?.should be_false
52
+ end
53
+
54
+ it "should hit only Vehicle _new_called? on instantiation" do
55
+ CarWithoutMethods.reset
56
+ CarWithoutMethods.new
57
+ CarWithoutMethods.car_new_called?.should be_false
58
+ CarWithoutMethods.vehicle_new_called?.should be_true
59
+ end
60
+
61
+ it "should not hit Vehicle method_missing when calling dynamic predicate method :gear_is_in_first?" do
62
+ c = CarWithoutMethods.new
63
+ c.gear_is_in_drive?
64
+ c.vehicle_method_missing_called?.should be_false
65
+ end
66
+
67
+ it "should hit Vehicle method_missing when calling unsupported predicate method" do
68
+ c = CarWithoutMethods.new
69
+ c.parking_break2_is_on?
70
+ c.vehicle_method_missing_called?.should be_true
71
+ end
72
+
73
+ end
data/spec/plural.rb ADDED
@@ -0,0 +1,8 @@
1
+ require 'enumerated_attribute'
2
+
3
+ class Plural
4
+ enum_attr :box, %w(small medium large)
5
+ enum_attr :batch, %w(none daily weekly)
6
+ enum_attr :cherry, %w(red green yellow)
7
+ enum_attr :guy, %w(handsome funny cool)
8
+ end