mcommons-values_for 0.0.7 → 0.0.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/lib/values_for.rb +11 -7
- data/spec/values_for_spec.rb +37 -0
- data/values_for.gemspec +1 -1
- metadata +1 -1
data/lib/values_for.rb
CHANGED
@@ -26,6 +26,9 @@ module EnumFor
|
|
26
26
|
|
27
27
|
prefix = opts.has_key?(:prefix) ? opts[:prefix] : attribute_s
|
28
28
|
|
29
|
+
# We don't accept the case where an empty string is a valid value, but we should provide a useful error message
|
30
|
+
raise ArgumentError, "Can't use values_for with an empty string" if opts[:has].any?{|v| v.respond_to?(:empty?) && v.empty? }
|
31
|
+
|
29
32
|
# Valid values are most likely Symbols anyway, but coerce them to be safe.
|
30
33
|
valid_symbols = opts[:has].map{|v| v.to_sym }
|
31
34
|
|
@@ -46,7 +49,6 @@ module EnumFor
|
|
46
49
|
read_attribute(attribute) == val_s # read_attribute(:foo) == 'foo'
|
47
50
|
end # end
|
48
51
|
end
|
49
|
-
|
50
52
|
end
|
51
53
|
|
52
54
|
# Accepts assignment both from String and Symbol form of valid values.
|
@@ -54,15 +56,17 @@ module EnumFor
|
|
54
56
|
merge(:in => valid_symbols | valid_symbols.map{|s| s.to_s } )
|
55
57
|
|
56
58
|
# Custom reader method presents attribute value in Symbol form.
|
57
|
-
define_method(attribute_s) do
|
58
|
-
self[attribute].
|
59
|
-
|
59
|
+
define_method(attribute_s) do # def foo
|
60
|
+
unless self[attribute].nil? || self[attribute].empty? # unless self[:foo].nil? || self[:foo].empty?
|
61
|
+
self[attribute].to_sym # self[:foo].to_sym unless self[:foo].nil?
|
62
|
+
end # end
|
63
|
+
end # end
|
60
64
|
|
61
65
|
# Custom setter method casting all attribute input to String, allows
|
62
66
|
# assignment from Symbol form.
|
63
|
-
define_method(attribute_s + '=') do |other|
|
64
|
-
self[attribute] = other.to_s
|
65
|
-
end
|
67
|
+
define_method(attribute_s + '=') do |other| # def foo=(other)
|
68
|
+
self[attribute] = other.nil? ? nil : other.to_s # self[foo] = other unless other.nil?
|
69
|
+
end # end
|
66
70
|
|
67
71
|
# Make collection of all valid attribute Symbols available to user
|
68
72
|
# from plural name of attribute as class method.
|
data/spec/values_for_spec.rb
CHANGED
@@ -130,6 +130,17 @@ describe EnumFor do
|
|
130
130
|
end
|
131
131
|
end
|
132
132
|
|
133
|
+
describe "when a user tries to set an empty String as a valid value" do
|
134
|
+
it "should raise an IllegalArgumentError" do
|
135
|
+
lambda {
|
136
|
+
class Food2 < ActiveRecord::Base
|
137
|
+
set_table_name "tacos"
|
138
|
+
values_for :state, :has => ['stuff', '']
|
139
|
+
end
|
140
|
+
}.should raise_error(ArgumentError)
|
141
|
+
end
|
142
|
+
end
|
143
|
+
|
133
144
|
describe "behavior with existing methods of same name" do
|
134
145
|
class Balloon < ActiveRecord::Base
|
135
146
|
set_table_name "tacos"
|
@@ -160,6 +171,32 @@ describe EnumFor do
|
|
160
171
|
end
|
161
172
|
end
|
162
173
|
|
174
|
+
describe "getting and setting nil values" do
|
175
|
+
class Food2 < ActiveRecord::Base
|
176
|
+
set_table_name "tacos"
|
177
|
+
values_for :state, :has => ['stuff', 'morestuff'], :prefix => nil, :allow_nil => true, :message => "is great!"
|
178
|
+
end
|
179
|
+
|
180
|
+
before do
|
181
|
+
@f = Food2.new
|
182
|
+
@f.state = nil
|
183
|
+
end
|
184
|
+
|
185
|
+
it "should return a nil value without error" do
|
186
|
+
lambda {
|
187
|
+
@f.state
|
188
|
+
}.should_not raise_error
|
189
|
+
end
|
190
|
+
|
191
|
+
it "should return nil when nil is set" do
|
192
|
+
@f.state.should == nil
|
193
|
+
end
|
194
|
+
|
195
|
+
it "should store nil when nil is set" do
|
196
|
+
@f.state_before_type_cast.should == nil
|
197
|
+
end
|
198
|
+
end
|
199
|
+
|
163
200
|
describe "named scope creation" do
|
164
201
|
describe "when not added" do
|
165
202
|
it "should not create named scopes" do
|
data/values_for.gemspec
CHANGED