Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

add docs to AM::AttributeMethods#define_attribute_method [ci skip]

  • Loading branch information...
commit f975c4b641b4d410b7d2ef654bba549e1dfb652e 1 parent b5b350a
@frodsan frodsan authored
Showing with 25 additions and 0 deletions.
  1. +25 −0 activemodel/lib/active_model/attribute_methods.rb
View
25 activemodel/lib/active_model/attribute_methods.rb
@@ -233,6 +233,31 @@ def define_attribute_methods(*attr_names)
attr_names.flatten.each { |attr_name| define_attribute_method(attr_name) }
end
+ # Declares an attribute that should be prefixed and suffixed by
+ # ActiveModel::AttributeMethods.
+ #
+ # To use, pass an attribute name (as string or symbol), be sure to declare
+ # +define_attribute_method+ after you define any prefix, suffix or affix
+ # method, or they will not hook in.
+ #
+ # class Person
+ # include ActiveModel::AttributeMethods
+ #
+ # attr_accessor :name
+ # attribute_method_suffix '_short?'
+ # define_attribute_method :name
+ #
+ # private
+ #
+ # def attribute_short?(attr)
+ # send(attr).length < 5
+ # end
+ # end
+ #
+ # person = Person.new
+ # person.name = 'Bob'
+ # person.name # => "Bob"
+ # person.name_short? # => true
def define_attribute_method(attr_name)
attribute_method_matchers.each do |matcher|
method_name = matcher.method_name(attr_name)
Please sign in to comment.
Something went wrong with that request. Please try again.