From 5b048c228087b7d3d0483a13ec0a9ec89e0d72be Mon Sep 17 00:00:00 2001 From: Sam Lown Date: Sat, 22 May 2010 00:17:33 +0200 Subject: [PATCH] More trueness testing and removing string comparison for booleans --- lib/couchrest/extended_document.rb | 2 +- lib/couchrest/mixins/properties.rb | 2 +- spec/couchrest/property_spec.rb | 17 +++++++++++++++++ 3 files changed, 19 insertions(+), 2 deletions(-) diff --git a/lib/couchrest/extended_document.rb b/lib/couchrest/extended_document.rb index fe882fe..765fe3f 100644 --- a/lib/couchrest/extended_document.rb +++ b/lib/couchrest/extended_document.rb @@ -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 diff --git a/lib/couchrest/mixins/properties.rb b/lib/couchrest/mixins/properties.rb index a8a5b0f..be07cdd 100644 --- a/lib/couchrest/mixins/properties.rb +++ b/lib/couchrest/mixins/properties.rb @@ -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 diff --git a/spec/couchrest/property_spec.rb b/spec/couchrest/property_spec.rb index 16b14e3..85d36aa 100644 --- a/spec/couchrest/property_spec.rb +++ b/spec/couchrest/property_spec.rb @@ -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