Make "property :foo, :default => false" work

Before a default value of false was treated like a default of nil,
  which is not the same.

Signed-off-by: Matt Aimonetti <mattaimonetti@gmail.com>
This commit is contained in:
Mutwin Kraus 2009-04-26 10:31:19 +08:00 committed by Matt Aimonetti
parent 75a5018b12
commit f7bbee8243
3 changed files with 9 additions and 3 deletions

View file

@ -24,7 +24,7 @@ module CouchRest
self.class.properties.each do |property| self.class.properties.each do |property|
key = property.name.to_s key = property.name.to_s
# let's make sure we have a default and we can assign the value # let's make sure we have a default and we can assign the value
if property.default && (self.respond_to?("#{key}=") || self.key?(key)) if !property.default.nil? && (self.respond_to?("#{key}=") || self.key?(key))
if property.default.class == Proc if property.default.class == Proc
self[key] = property.default.call self[key] = property.default.call
else else

View file

@ -30,11 +30,11 @@ module CouchRest
@validation_format = options.delete(:format) if options[:format] @validation_format = options.delete(:format) if options[:format]
@read_only = options.delete(:read_only) if options[:read_only] @read_only = options.delete(:read_only) if options[:read_only]
@alias = options.delete(:alias) if options[:alias] @alias = options.delete(:alias) if options[:alias]
@default = options.delete(:default) if options[:default] @default = options.delete(:default) unless options[:default].nil?
@casted = options[:casted] ? true : false @casted = options[:casted] ? true : false
@init_method = options[:send] ? options.delete(:send) : 'new' @init_method = options[:send] ? options.delete(:send) : 'new'
@options = options @options = options
end end
end end
end end

View file

@ -10,6 +10,7 @@ describe "ExtendedDocument" do
property :preset, :default => {:right => 10, :top_align => false} property :preset, :default => {:right => 10, :top_align => false}
property :set_by_proc, :default => Proc.new{Time.now}, :cast_as => 'Time' property :set_by_proc, :default => Proc.new{Time.now}, :cast_as => 'Time'
property :tags, :default => [] property :tags, :default => []
property :false_default, :default => false
property :name property :name
timestamps! timestamps!
end end
@ -157,6 +158,11 @@ describe "ExtendedDocument" do
obj = WithDefaultValues.new(:tags => ['spec']) obj = WithDefaultValues.new(:tags => ['spec'])
obj.tags.should == ['spec'] obj.tags.should == ['spec']
end end
it "should work with a default value of false" do
obj = WithDefaultValues.new
obj.false_default.should == false
end
end end
describe "a doc with template values (CR::Model spec)" do describe "a doc with template values (CR::Model spec)" do