Permalink
Browse files

Add Boolean field type

  • Loading branch information...
1 parent de5eba9 commit fc142007ac52a72b301ccf402fc3666ae35762fe @dolzenko dolzenko committed Nov 29, 2011
View
2 lib/bsm/constrainable/field.rb
@@ -9,6 +9,7 @@ module Bsm::Constrainable::Field
autoload :Timestamp,'bsm/constrainable/field/common'
autoload :Datetime, 'bsm/constrainable/field/common'
autoload :Date, 'bsm/constrainable/field/common'
+ autoload :Boolean, 'bsm/constrainable/field/common'
register self::Number
register self::Integer
@@ -17,4 +18,5 @@ module Bsm::Constrainable::Field
register self::Timestamp
register self::Datetime
register self::Date
+ register self::Boolean
end
View
14 lib/bsm/constrainable/field/common.rb
@@ -48,4 +48,18 @@ def _convert(v)
v.to_date rescue nil
end
end
+
+ class Boolean < Base
+ self.operators = [:eq, :not_eq]
+
+ TRUE_VALUES = ["true", "1"]
+
+ protected
+
+ def _convert(v)
+ result = super
+ result.blank? ? nil : TRUE_VALUES.include?(result)
+ end
+ end
+
end
View
2 spec/bsm/constrainable/field/base_spec.rb
@@ -64,7 +64,7 @@ def merge(name, operator, value)
end
it 'should store unrecognized options' do
- Bsm::Constrainable::Field::String.new("some", :allow_blank => true).options.should == {:allow_blank=>true}
+ Bsm::Constrainable::Field::String.new("some", :allow_blank => true).options.should == { :allow_blank => true }
end
end
View
10 spec/bsm/constrainable/field/common_spec.rb
@@ -55,5 +55,15 @@
it { subject.convert("2011-11-11 11:11").should == Date.civil(2011, 11, 11) }
end
+ describe Bsm::Constrainable::Field::Boolean do
+ it { subject.class.should have(2).operators }
+ it { subject.convert("true").should == true }
+ it { subject.convert("1").should == true }
+ it { subject.convert("false").should == false }
+ it { subject.convert("0").should == false }
+ it { subject.convert("").should == nil }
+ it { subject.convert("invalid").should == nil }
+ end
+
end

0 comments on commit fc14200

Please sign in to comment.