Support providing an initialization block when creating new models

This commit is contained in:
Peter Williams 2011-05-11 13:53:28 -06:00
parent 1309fa7082
commit c9d2611bb7
2 changed files with 9 additions and 0 deletions

View file

@ -49,6 +49,8 @@ module CouchRest
# * :directly_set_attributes: true when data comes directly from database
# * :database: provide an alternative database
#
# If a block is provided the new model will be passed into the
# block so that it can be populated.
def initialize(doc = {}, options = {})
doc = prepare_all_attributes(doc, options)
# set the instances database, if provided
@ -57,6 +59,8 @@ module CouchRest
unless self['_id'] && self['_rev']
self[self.model_type_key] = self.class.to_s
end
yield self if block_given?
after_initialize if respond_to?(:after_initialize)
end

View file

@ -44,6 +44,11 @@ describe "Model Base" do
@obj.database.should eql('database')
end
it "should support initialization block" do
@obj = Basic.new {|b| b.database = 'database'}
@obj.database.should eql('database')
end
end
describe "ActiveModel compatability Basic" do