From e07e5b468fce855a746ae1b62772c714f11e01f5 Mon Sep 17 00:00:00 2001 From: Matt Aimonetti Date: Mon, 9 Feb 2009 12:08:55 -0800 Subject: [PATCH] fixed a bug with a default value being a proc --- lib/couchrest/mixins/properties.rb | 2 +- spec/couchrest/more/extended_doc_spec.rb | 11 +++++++++-- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/lib/couchrest/mixins/properties.rb b/lib/couchrest/mixins/properties.rb index 1801361..a426670 100644 --- a/lib/couchrest/mixins/properties.rb +++ b/lib/couchrest/mixins/properties.rb @@ -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 diff --git a/spec/couchrest/more/extended_doc_spec.rb b/spec/couchrest/more/extended_doc_spec.rb index 0b10ba6..7096293 100644 --- a/spec/couchrest/more/extended_doc_spec.rb +++ b/spec/couchrest/more/extended_doc_spec.rb @@ -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 \ No newline at end of file