instance method
reader_method
Ruby on Rails 2.2.3
Since v2.2.3 PrivateSignature
reader_method(name, class_name, mapping, allow_nil, constructor)
No documentation comment.
Parameters
-
namereq -
class_namereq -
mappingreq -
allow_nilreq -
constructorreq
Source
# File activerecord/lib/active_record/aggregations.rb, line 214
def reader_method(name, class_name, mapping, allow_nil, constructor)
module_eval do
define_method(name) do |*args|
force_reload = args.first || false
if (instance_variable_get("@#{name}").nil? || force_reload) && (!allow_nil || mapping.any? {|pair| !read_attribute(pair.first).nil? })
attrs = mapping.collect {|pair| read_attribute(pair.first)}
object = case constructor
when Symbol
class_name.constantize.send(constructor, *attrs)
when Proc, Method
constructor.call(*attrs)
else
raise ArgumentError, 'Constructor must be a symbol denoting the constructor method to call or a Proc to be invoked.'
end
instance_variable_set("@#{name}", object)
end
instance_variable_get("@#{name}")
end
end
end
Defined in activerecord/lib/active_record/aggregations.rb line 214
· View on GitHub
· Improve this page
· Find usages on GitHub
Defined in ActiveRecord::Aggregations::ClassMethods