instance method define_method_attribute

Ruby on Rails 3.2.22.5

Since v3.0.20 Last seen in v4.2.9

Available in: v3.0.20 v3.1.12 v3.2.22.5 v4.0.13 v4.1.16 v4.2.9

Signature

define_method_attribute(attr_name)

We want to generate the methods via module_eval rather than define_method, because define_method is slower on dispatch and uses more memory (because it creates a closure).

But sometimes the database might return columns with characters that are not allowed in normal method names (like ‘my_column(omg)’. So to work around this we first define with the __temp__ identifier, and then use alias method to rename it to what we want.

Parameters

attr_name req
Source
# File activerecord/lib/active_record/attribute_methods/read.rb, line 69
          def define_method_attribute(attr_name)
            generated_attribute_methods.module_eval <<-STR, __FILE__, __LINE__ + 1
              def __temp__
                #{internal_attribute_access_code(attr_name, attribute_cast_code(attr_name))}
              end
              alias_method '#{attr_name}', :__temp__
              undef_method :__temp__
            STR
          end

Defined in activerecord/lib/active_record/attribute_methods/read.rb line 69 · View on GitHub · Improve this page · Find usages on GitHub

Defined in ActiveRecord::AttributeMethods::Read::ClassMethods

Type at least 2 characters to search.

↑↓ navigate · open · esc close