fixed a bug with a default value being a proc

This commit is contained in:
Matt Aimonetti 2009-02-09 12:08:55 -08:00
parent 621f5565e9
commit e07e5b468f
2 changed files with 10 additions and 3 deletions

View file

@ -25,7 +25,7 @@ module CouchRest
# 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.class == Proc
self[key] = v.call
self[key] = property.default.call
else
self[key] = Marshal.load(Marshal.dump(property.default))
end

View file

@ -2,7 +2,8 @@ require File.dirname(__FILE__) + '/../../spec_helper'
class WithDefaultValues < CouchRest::ExtendedDocument
use_database TEST_SERVER.default_database
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}, :type => 'Time'
end
describe "ExtendedDocument" do
@ -12,9 +13,15 @@ describe "ExtendedDocument" do
@obj = WithDefaultValues.new
end
it "should have the default value set an initalization" do
it "should have the default value set at initalization" do
@obj.preset.should == {:right => 10, :top_align => false}
end
it "should automatically call a proc default at initialization" do
@obj.set_by_proc.should be_an_instance_of(Time)
@obj.set_by_proc.should == @obj.set_by_proc
@obj.set_by_proc.should < Time.now
end
end
end