More trueness testing and removing string comparison for booleans

This commit is contained in:
Sam Lown 2010-05-22 00:17:33 +02:00
parent 3fa8c4b215
commit 5b048c2280
3 changed files with 19 additions and 2 deletions

View file

@ -8,7 +8,7 @@ module CouchRest
# Same as CouchRest::Document but with properties and validations # Same as CouchRest::Document but with properties and validations
class ExtendedDocument < Document class ExtendedDocument < Document
VERSION = "1.0.3" VERSION = "1.0.4"
include CouchRest::Mixins::Callbacks include CouchRest::Mixins::Callbacks
include CouchRest::Mixins::DocumentQueries include CouchRest::Mixins::DocumentQueries

View file

@ -122,7 +122,7 @@ module CouchRest
if ['boolean', TrueClass.to_s.downcase].include?(property.type.to_s.downcase) if ['boolean', TrueClass.to_s.downcase].include?(property.type.to_s.downcase)
class_eval <<-EOS, __FILE__, __LINE__ class_eval <<-EOS, __FILE__, __LINE__
def #{property.name}? def #{property.name}?
if self['#{property.name}'].nil? || self['#{property.name}'] == false || self['#{property.name}'].to_s.downcase == 'false' if self['#{property.name}'].nil? || self['#{property.name}'] == false
false false
else else
true true

View file

@ -578,6 +578,11 @@ describe "ExtendedDocument properties" do
end end
describe 'when type primitive is a Boolean' do describe 'when type primitive is a Boolean' do
class RootBeerFloat < CouchRest::ExtendedDocument
use_database DB
property :tasty, TrueClass
end
[ true, 'true', 'TRUE', '1', 1, 't', 'T' ].each do |value| [ true, 'true', 'TRUE', '1', 1, 't', 'T' ].each do |value|
it "returns true when value is #{value.inspect}" do it "returns true when value is #{value.inspect}" do
@course.active = value @course.active = value
@ -603,6 +608,18 @@ describe "ExtendedDocument properties" do
@course.active = 'false' @course.active = 'false'
@course.active?.should be_false @course.active?.should be_false
end end
it "should add an accessor with a '?' for boolean attributes that returns true or false" do
RootBeerFloat.new(:tasty => true).tasty?.should == true
RootBeerFloat.new(:tasty => 'you bet').tasty?.should == true
RootBeerFloat.new(:tasty => 123).tasty?.should == true
RootBeerFloat.new(:tasty => false).tasty?.should == false
RootBeerFloat.new(:tasty => 'false').tasty?.should == false
RootBeerFloat.new(:tasty => 'FaLsE').tasty?.should == false
RootBeerFloat.new(:tasty => nil).tasty?.should == false
end
end end
describe 'when type primitive is a TrueClass' do describe 'when type primitive is a TrueClass' do