aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record
diff options
context:
space:
mode:
authorAaron Patterson <aaron.patterson@gmail.com>2010-09-30 12:13:12 -0700
committerAaron Patterson <aaron.patterson@gmail.com>2010-09-30 12:13:12 -0700
commit505b532605bd3b9b8a444be10911f0864f1d42ba (patch)
tree55bd6084b8beff5f9b5e4ef383d88470e4d086fd /activerecord/lib/active_record
parentef8ce78ba1d37c7d4246f0c6b2cf13140c2898f8 (diff)
downloadrails-505b532605bd3b9b8a444be10911f0864f1d42ba.tar.gz
rails-505b532605bd3b9b8a444be10911f0864f1d42ba.tar.bz2
rails-505b532605bd3b9b8a444be10911f0864f1d42ba.zip
speeding up object instantiation by eliminating instance_eval
Diffstat (limited to 'activerecord/lib/active_record')
-rw-r--r--activerecord/lib/active_record/base.rb28
1 files changed, 21 insertions, 7 deletions
diff --git a/activerecord/lib/active_record/base.rb b/activerecord/lib/active_record/base.rb
index 2157a0aded..9204295d8c 100644
--- a/activerecord/lib/active_record/base.rb
+++ b/activerecord/lib/active_record/base.rb
@@ -884,13 +884,9 @@ module ActiveRecord #:nodoc:
# single-table inheritance model that makes it possible to create
# objects of different types from the same table.
def instantiate(record)
- find_sti_class(record[inheritance_column]).allocate.instance_eval do
- @attributes, @attributes_cache, @previously_changed, @changed_attributes = record, {}, {}, {}
- @new_record = @readonly = @destroyed = @marked_for_destruction = false
- _run_find_callbacks
- _run_initialize_callbacks
- self
- end
+ model = find_sti_class(record[inheritance_column]).allocate
+ model.init_with('attributes' => record)
+ model
end
def find_sti_class(type_name)
@@ -1416,6 +1412,24 @@ MSG
populate_with_current_scope_attributes
end
+ # Initialize an empty model object from +coder+. +coder+ must contain
+ # the attributes necessary for initializing an empty model object. For
+ # example:
+ #
+ # class Post < ActiveRecord::Base
+ # end
+ #
+ # post = Post.allocate
+ # post.init_with('attributes' => { 'title' => 'hello world' })
+ # post.title # => 'hello world'
+ def init_with(coder)
+ @attributes = coder['attributes']
+ @attributes_cache, @previously_changed, @changed_attributes = {}, {}, {}
+ @new_record = @readonly = @destroyed = @marked_for_destruction = false
+ _run_find_callbacks
+ _run_initialize_callbacks
+ end
+
# Returns a String, which Action Pack uses for constructing an URL to this
# object. The default implementation returns this record's id as a String,
# or nil if this record's unsaved.