Added ActiveRecord like after_initialize callback to ExtendedDocument

This commit is contained in:
Sam Lown 2010-03-03 20:01:27 +00:00
parent d992f44365
commit ca23e186f4
2 changed files with 20 additions and 0 deletions

View file

@ -58,6 +58,7 @@ module CouchRest
unless self['_id'] && self['_rev'] unless self['_id'] && self['_rev']
self['couchrest-type'] = self.class.to_s self['couchrest-type'] = self.class.to_s
end end
after_initialize if respond_to?(:after_initialize)
end end
# Defines an instance and save it directly to the database # Defines an instance and save it directly to the database

View file

@ -105,6 +105,18 @@ describe "ExtendedDocument" do
end end
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 before(:each) do
@obj = WithDefaultValues.new @obj = WithDefaultValues.new
end end
@ -699,6 +711,13 @@ describe "ExtendedDocument" do
end end
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 describe "recursive validation on an extended document" do
before :each do before :each do
reset_test_db! reset_test_db!