cassiomarques-booleanize 0.1 → 0.2
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/Rakefile +1 -0
- data/lib/booleanize.rb +15 -2
- data/spec/booleanize_spec.rb +32 -0
- metadata +1 -1
data/Rakefile
CHANGED
data/lib/booleanize.rb
CHANGED
@@ -51,6 +51,19 @@
|
|
51
51
|
#
|
52
52
|
module Booleanize
|
53
53
|
|
54
|
+
class Config
|
55
|
+
def self.default_strings(options = {})
|
56
|
+
error = "Wrong configuration parameters for booleanize: You should pass something like {:true => \"Yes\", :false => \"No\" }"
|
57
|
+
raise error unless options.is_a?(Hash) and [:true, :false].all? { |k| options.has_key? k }
|
58
|
+
@@default_for_true = options[:true]
|
59
|
+
@@default_for_false = options[:false]
|
60
|
+
end
|
61
|
+
|
62
|
+
protected
|
63
|
+
def self.default_for_true; @@default_for_true rescue nil; end
|
64
|
+
def self.default_for_false; @@default_for_false rescue nil; end
|
65
|
+
end
|
66
|
+
|
54
67
|
def booleanize(*params)
|
55
68
|
params.each do |param|
|
56
69
|
case param
|
@@ -73,8 +86,8 @@ module Booleanize
|
|
73
86
|
end
|
74
87
|
|
75
88
|
def create_humanize_method(attr_name, true_str, false_str)
|
76
|
-
true_str = (true_str.nil? ? "True" : true_str.to_s)
|
77
|
-
false_str = (false_str.nil? ? "False" : false_str.to_s)
|
89
|
+
true_str = (true_str.nil? ? (Config.default_for_true.nil? ? "True" : Config.default_for_true) : true_str.to_s)
|
90
|
+
false_str = (false_str.nil? ? (Config.default_for_false.nil? ? "False" : Config.default_for_false) : false_str.to_s)
|
78
91
|
class_eval("def #{attr_name}_humanize; #{attr_name} ? #{true_str.inspect} : #{false_str.inspect}; end")
|
79
92
|
end
|
80
93
|
|
data/spec/booleanize_spec.rb
CHANGED
@@ -198,6 +198,38 @@ describe "booleanize" do
|
|
198
198
|
User.not_deleted.should have(6).items
|
199
199
|
end
|
200
200
|
end
|
201
|
+
|
202
|
+
describe "with global configuration" do
|
203
|
+
before do
|
204
|
+
Booleanize::Config.default_strings :true => "Oh yes!", :false => "Nooo"
|
205
|
+
class User
|
206
|
+
booleanize :active
|
207
|
+
booleanize :smart => ["Too smart", "Duh!"]
|
208
|
+
end
|
209
|
+
end
|
210
|
+
|
211
|
+
it "should use the globally defined string for true" do
|
212
|
+
User.create(:active => true).active_humanize.should == "Oh yes!"
|
213
|
+
end
|
214
|
+
|
215
|
+
it "should use the globally defined string for false" do
|
216
|
+
User.create(:active => false).active_humanize.should == "Nooo"
|
217
|
+
end
|
218
|
+
|
219
|
+
it "should use the string for yes specified in the class definition" do
|
220
|
+
User.create(:smart => true).smart_humanize.should == "Too smart"
|
221
|
+
end
|
222
|
+
|
223
|
+
it "should use the string for no specified in the class definition" do
|
224
|
+
User.create(:smart => false).smart_humanize.should == "Duh!"
|
225
|
+
end
|
226
|
+
|
227
|
+
it "should raise an exception if the config parameters are not inside a two pairs hash" do
|
228
|
+
["hello", {:bla => :foo}, ["bla", "ble"]].each do |params|
|
229
|
+
lambda { Booleanize::Config.default_strings params }.should raise_error
|
230
|
+
end
|
231
|
+
end
|
232
|
+
end
|
201
233
|
end
|
202
234
|
|
203
235
|
|