From ca23e186f4ed17fb92ed9fd1b87e386b91203d43 Mon Sep 17 00:00:00 2001 From: Sam Lown Date: Wed, 3 Mar 2010 20:01:27 +0000 Subject: [PATCH] Added ActiveRecord like after_initialize callback to ExtendedDocument --- lib/couchrest/more/extended_document.rb | 1 + spec/couchrest/more/extended_doc_spec.rb | 19 +++++++++++++++++++ 2 files changed, 20 insertions(+) diff --git a/lib/couchrest/more/extended_document.rb b/lib/couchrest/more/extended_document.rb index fc266ed..28f5359 100644 --- a/lib/couchrest/more/extended_document.rb +++ b/lib/couchrest/more/extended_document.rb @@ -58,6 +58,7 @@ module CouchRest unless self['_id'] && self['_rev'] self['couchrest-type'] = self.class.to_s end + after_initialize if respond_to?(:after_initialize) end # Defines an instance and save it directly to the database diff --git a/spec/couchrest/more/extended_doc_spec.rb b/spec/couchrest/more/extended_doc_spec.rb index 6d558ce..f4ac00c 100644 --- a/spec/couchrest/more/extended_doc_spec.rb +++ b/spec/couchrest/more/extended_doc_spec.rb @@ -104,6 +104,18 @@ describe "ExtendedDocument" do self.other_arg = "foo-#{value}" end end + + class WithAfterInitializeMethod < CouchRest::ExtendedDocument + use_database TEST_SERVER.default_database + + property :some_value + + def after_initialize + self.some_value ||= "value" + end + + end + before(:each) do @obj = WithDefaultValues.new @@ -698,6 +710,13 @@ describe "ExtendedDocument" do @doc.other_arg.should == "foo-foo" end end + + describe "initialization" do + it "should call after_initialize method if available" do + @doc = WithAfterInitializeMethod.new + @doc['some_value'].should eql('value') + end + end describe "recursive validation on an extended document" do before :each do