cassiomarques-booleanize 0.2 → 0.3
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/CHANGELOG +5 -0
- data/lib/booleanize.rb +15 -8
- data/spec/booleanize_spec.rb +2 -1
- metadata +1 -1
data/CHANGELOG
CHANGED
data/lib/booleanize.rb
CHANGED
@@ -49,27 +49,33 @@
|
|
49
49
|
# active_users = User.active #=> same as named_scope :active, :conditions => {:active => true}
|
50
50
|
# disabled_users = User.not_active #=> same as named_scope :not_active, :conditions => {:active => false}
|
51
51
|
#
|
52
|
+
require 'singleton'
|
53
|
+
|
52
54
|
module Booleanize
|
53
55
|
|
54
56
|
class Config
|
57
|
+
include Singleton
|
58
|
+
|
59
|
+
attr_accessor :default_for_true, :default_for_false
|
60
|
+
|
55
61
|
def self.default_strings(options = {})
|
56
62
|
error = "Wrong configuration parameters for booleanize: You should pass something like {:true => \"Yes\", :false => \"No\" }"
|
57
63
|
raise error unless options.is_a?(Hash) and [:true, :false].all? { |k| options.has_key? k }
|
58
|
-
|
59
|
-
|
64
|
+
instance.default_for_true = options[:true]
|
65
|
+
instance.default_for_false = options[:false]
|
60
66
|
end
|
61
67
|
|
62
68
|
protected
|
63
|
-
def self.default_for_true;
|
64
|
-
def self.default_for_false;
|
69
|
+
def self.default_for_true; instance.default_for_true rescue nil; end
|
70
|
+
def self.default_for_false; instance.default_for_false rescue nil; end
|
65
71
|
end
|
66
72
|
|
67
73
|
def booleanize(*params)
|
68
74
|
params.each do |param|
|
69
75
|
case param
|
70
|
-
when Symbol
|
71
|
-
when Array
|
72
|
-
when Hash
|
76
|
+
when Symbol; create_methods_for_symbol(param)
|
77
|
+
when Array; create_methods_for_array(param)
|
78
|
+
when Hash; create_methods_for_hash(param)
|
73
79
|
else raise_error
|
74
80
|
end
|
75
81
|
end
|
@@ -89,6 +95,7 @@ module Booleanize
|
|
89
95
|
true_str = (true_str.nil? ? (Config.default_for_true.nil? ? "True" : Config.default_for_true) : true_str.to_s)
|
90
96
|
false_str = (false_str.nil? ? (Config.default_for_false.nil? ? "False" : Config.default_for_false) : false_str.to_s)
|
91
97
|
class_eval("def #{attr_name}_humanize; #{attr_name} ? #{true_str.inspect} : #{false_str.inspect}; end")
|
98
|
+
|
92
99
|
end
|
93
100
|
|
94
101
|
def create_methods(attr_name, true_str = nil, false_str = nil)
|
@@ -126,4 +133,4 @@ module Booleanize
|
|
126
133
|
|
127
134
|
end
|
128
135
|
|
129
|
-
ActiveRecord::Base.extend Booleanize
|
136
|
+
ActiveRecord::Base.send(:extend, Booleanize)
|
data/spec/booleanize_spec.rb
CHANGED
@@ -202,7 +202,8 @@ describe "booleanize" do
|
|
202
202
|
describe "with global configuration" do
|
203
203
|
before do
|
204
204
|
Booleanize::Config.default_strings :true => "Oh yes!", :false => "Nooo"
|
205
|
-
|
205
|
+
|
206
|
+
User.class_eval do
|
206
207
|
booleanize :active
|
207
208
|
booleanize :smart => ["Too smart", "Duh!"]
|
208
209
|
end
|