method
reader_method
v3.0.9 -
Show latest stable
- Class:
ActiveRecord::Aggregations::ClassMethods
reader_method(name, class_name, mapping, allow_nil, constructor)private
No documentation available.
# File activerecord/lib/active_record/aggregations.rb, line 224
def reader_method(name, class_name, mapping, allow_nil, constructor)
module_eval do
define_method(name) do |*args|
force_reload = args.first || false
unless instance_variable_defined?("@#{name}")
instance_variable_set("@#{name}", nil)
end
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