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 CHANGED
@@ -7,6 +7,7 @@ task :default => :spec
7
7
 
8
8
  Spec::Rake::SpecTask.new do |t|
9
9
  t.spec_files = FileList['spec/**/*_spec.rb']
10
+ t.spec_opts = ["--color", "--format", "specdoc"]
10
11
  end
11
12
 
12
13
  desc 'Generate documentation for the booleanize plugin.'
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
 
@@ -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
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cassiomarques-booleanize
3
3
  version: !ruby/object:Gem::Version
4
- version: "0.1"
4
+ version: "0.2"
5
5
  platform: ruby
6
6
  authors:
7
7
  - "C\xC3\xA1ssio Marques"