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
class ExtendedDocument < Document
VERSION = "1.0.3"
VERSION = "1.0.4"
include CouchRest::Mixins::Callbacks
include CouchRest::Mixins::DocumentQueries

View file

@ -122,7 +122,7 @@ module CouchRest
if ['boolean', TrueClass.to_s.downcase].include?(property.type.to_s.downcase)
class_eval <<-EOS, __FILE__, __LINE__
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
else
true

View file

@ -578,6 +578,11 @@ describe "ExtendedDocument properties" do
end
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|
it "returns true when value is #{value.inspect}" do
@course.active = value
@ -603,6 +608,18 @@ describe "ExtendedDocument properties" do
@course.active = 'false'
@course.active?.should be_false
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
describe 'when type primitive is a TrueClass' do