From f975c4b641b4d410b7d2ef654bba549e1dfb652e Mon Sep 17 00:00:00 2001 From: Francesco Rodriguez Date: Thu, 21 Jun 2012 15:51:37 -0500 Subject: add docs to AM::AttributeMethods#define_attribute_method [ci skip] --- activemodel/lib/active_model/attribute_methods.rb | 25 +++++++++++++++++++++++ 1 file changed, 25 insertions(+) (limited to 'activemodel/lib') diff --git a/activemodel/lib/active_model/attribute_methods.rb b/activemodel/lib/active_model/attribute_methods.rb index 85e2f89931..96d44e3b0d 100644 --- a/activemodel/lib/active_model/attribute_methods.rb +++ b/activemodel/lib/active_model/attribute_methods.rb @@ -233,6 +233,31 @@ module ActiveModel 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) -- cgit v1.2.3