0
# ActiveRecord::Base.generate_read_methods is set to true.
0
def define_read_methods
0
self.class.columns_hash.each do |name, column|
0
- unless self.class.serialized_attributes[name] || respond_to_without_attributes?(name)
0
- define_read_method(name.to_sym, name, column)
0
+ unless self.class.serialized_attributes[name]
0
+ define_read_method(name.to_sym, name, column) unless respond_to_without_attributes?(name)
0
+ define_question_method(name) unless respond_to_without_attributes?("#{name}?")
0
def define_read_method(symbol, attr_name, column)
0
cast_code = column.type_cast_code('v') if column
0
access_code = cast_code ? "(v=@attributes['#{attr_name}']) && #{cast_code}" : "@attributes['#{attr_name}']"
0
unless attr_name.to_s == self.class.primary_key.to_s
0
access_code = access_code.insert(0, "raise NoMethodError, 'missing attribute: #{attr_name}', caller unless @attributes.has_key?('#{attr_name}'); ")
0
self.class.read_methods << attr_name
0
+ evaluate_read_method attr_name, "def #{symbol}; #{access_code}; end"
0
+ # Define an attribute ? method.
0
+ def define_question_method(attr_name)
0
+ unless attr_name.to_s == self.class.primary_key.to_s
0
self.class.read_methods << "#{attr_name}?"
0
+ evaluate_read_method attr_name, "def #{attr_name}?; query_attribute('#{attr_name}'); end"
0
+ # Evaluate the definition for an attribute reader or ? method
0
+ def evaluate_read_method(attr_name, method_definition)
0
- self.class.class_eval("def #{symbol}; #{access_code}; end")
0
- self.class.class_eval("def #{symbol}?; query_attribute('#{attr_name}'); end")
0
+ self.class.class_eval(method_definition)
0
rescue SyntaxError => err
0
self.class.read_methods.delete(attr_name)
0
- self.class.read_methods.delete("#{attr_name}?")
0
logger.warn "Exception occured during reader method compilation."
0
logger.warn "Maybe #{attr_name} is not a valid Ruby identifier?"
Comments
No one has commented yet.