method
reader_method
reader_method(name, class_name, mapping, allow_nil, constructor)
private
Hide 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