Class Index [+]

Quicksearch

ActiveModel::AttributeMethods

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:

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.

Constants

NAME_COMPILABLE_REGEXP
CALL_COMPILABLE_REGEXP

Public Instance Methods

attribute_missing(match, *args, &block) click to toggle source

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.

     # File lib/active_model/attribute_methods.rb, line 415
415:     def attribute_missing(match, *args, &block)
416:       __send__(match.target, match.attr_name, *args, &block)
417:     end
method_missing(method, *args, &block) click to toggle source

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.

     # File lib/active_model/attribute_methods.rb, line 402
402:     def method_missing(method, *args, &block)
403:       if respond_to_without_attributes?(method, true)
404:         super
405:       else
406:         match = match_attribute_method?(method.to_s)
407:         match ? attribute_missing(match, *args, &block) : super
408:       end
409:     end
respond_to?(method, include_private_methods = false) click to toggle source
     # File lib/active_model/attribute_methods.rb, line 423
423:     def respond_to?(method, include_private_methods = false)
424:       if super
425:         true
426:       elsif !include_private_methods && super(method, true)
427:         # If we're here then we haven't found among non-private methods
428:         # but found among all methods. Which means that the given method is private.
429:         false
430:       else
431:         !match_attribute_method?(method.to_s).nil?
432:       end
433:     end
respond_to_without_attributes?(method, include_private_methods = false) click to toggle source

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.

Alias for: respond_to?

Protected Instance Methods

attribute_method?(attr_name) click to toggle source
     # File lib/active_model/attribute_methods.rb, line 436
436:       def attribute_method?(attr_name)
437:         respond_to_without_attributes?(:attributes) && attributes.include?(attr_name)
438:       end

Private Instance Methods

match_attribute_method?(method_name) click to toggle source

Returns a struct representing the matching attribute method. The struct’s attributes are prefix, base and suffix.

     # File lib/active_model/attribute_methods.rb, line 443
443:       def match_attribute_method?(method_name)
444:         match = self.class.send(:attribute_method_matcher, method_name)
445:         match && attribute_method?(match.attr_name) ? match : nil
446:       end
missing_attribute(attr_name, stack) click to toggle source
     # File lib/active_model/attribute_methods.rb, line 448
448:       def missing_attribute(attr_name, stack)
449:         raise ActiveModel::MissingAttributeError, "missing attribute: #{attr_name}", stack
450:       end

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.