Module: ActiveModel::AttributeMethods

Extended by:
ActiveSupport::Concern
Included in:
Dirty, ActiveRecord::AttributeMethods
Defined in:
activemodel/lib/active_model/attribute_methods.rb

Overview

Active Model Attribute Methods

ActiveModel::AttributeMethods provides a way to add prefixes and suffixes to your methods as well as handling the creation of Active Record like class methods such as table_name.

The requirements to implement ActiveModel::AttributeMethods are to:

  • include ActiveModel::AttributeMethods in your object

  • Call each Attribute Method module method you want to add, such as attribute_method_suffix or attribute_method_prefix

  • Call define_attribute_methods after the other methods are called.

  • Define the various generic _attribute methods that you have declared

A minimal implementation could be:

class Person
  include ActiveModel::AttributeMethods

  attribute_method_affix  :prefix => 'reset_', :suffix => '_to_default!'
  attribute_method_suffix '_contrived?'
  attribute_method_prefix 'clear_'
  define_attribute_methods ['name']

  attr_accessor :name

  private

  def attribute_contrived?(attr)
    true
  end

  def clear_attribute(attr)
    send("#{attr}=", nil)
  end

  def reset_attribute_to_default!(attr)
    send("#{attr}=", "Default Name")
  end
end

Note that whenever you include ActiveModel::AttributeMethods in your class, it requires you to implement an attributes method which returns a hash with each attribute name in your model as hash key and the attribute value as hash value.

Hash keys must be strings.

Defined Under Namespace

Modules: ClassMethods

Constant Summary collapse

NAME_COMPILABLE_REGEXP =
/\A[a-zA-Z_]\w*[!?=]?\z/
CALL_COMPILABLE_REGEXP =
/\A[a-zA-Z_]\w*[!?]?\z/

Instance Method Summary collapse

Methods included from ActiveSupport::Concern

append_features, extended, included

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object

Allows access to the object attributes, which are held in the @attributes hash, as though they were first-class methods. So a Person class with a name attribute can use Person#name and Person#name= and never directly use the attributes hash – except for multiple assigns with ActiveRecord#attributes=. A Milestone class can also ask Milestone#completed? to test that the completed attribute is not nil or 0.

It’s also possible to instantiate related objects, so a Client class belonging to the clients table with a master_id foreign key can instantiate master through Client#master.



402
403
404
405
406
407
408
409
# File 'activemodel/lib/active_model/attribute_methods.rb', line 402

def method_missing(method, *args, &block)
  if respond_to_without_attributes?(method, true)
    super
  else
    match = match_attribute_method?(method.to_s)
    match ? attribute_missing(match, *args, &block) : super
  end
end

Instance Method Details

#attribute_missing(match, *args, &block) ⇒ Object

attribute_missing is like method_missing, but for attributes. When method_missing is called we check to see if there is a matching attribute method. If so, we call attribute_missing to dispatch the attribute. This method can be overloaded to customise the behaviour.



415
416
417
# File 'activemodel/lib/active_model/attribute_methods.rb', line 415

def attribute_missing(match, *args, &block)
  __send__(match.target, match.attr_name, *args, &block)
end

#respond_to?(method, include_private_methods = false) ⇒ Boolean

Returns:

  • (Boolean)


423
424
425
426
427
428
429
430
431
432
433
# File 'activemodel/lib/active_model/attribute_methods.rb', line 423

def respond_to?(method, include_private_methods = false)
  if super
    true
  elsif !include_private_methods && super(method, true)
    # If we're here then we haven't found among non-private methods
    # but found among all methods. Which means that the given method is private.
    false
  else
    !match_attribute_method?(method.to_s).nil?
  end
end

#respond_to_without_attributes?Object

A Person object with a name attribute can ask person.respond_to?(:name), person.respond_to?(:name=), and person.respond_to?(:name?) which will all return true.



422
# File 'activemodel/lib/active_model/attribute_methods.rb', line 422

alias :respond_to_without_attributes? :respond_to?