Skip to content
This repository
Browse code

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

  • Loading branch information...
commit f975c4b641b4d410b7d2ef654bba549e1dfb652e 1 parent b5b350a
Francesco Rodríguez authored June 21, 2012
25  activemodel/lib/active_model/attribute_methods.rb
@@ -233,6 +233,31 @@ def define_attribute_methods(*attr_names)
233 233
         attr_names.flatten.each { |attr_name| define_attribute_method(attr_name) }
234 234
       end
235 235
 
  236
+      # Declares an attribute that should be prefixed and suffixed by
  237
+      # ActiveModel::AttributeMethods.
  238
+      #
  239
+      # To use, pass an attribute name (as string or symbol), be sure to declare
  240
+      # +define_attribute_method+ after you define any prefix, suffix or affix
  241
+      # method, or they will not hook in.
  242
+      #
  243
+      #   class Person
  244
+      #     include ActiveModel::AttributeMethods
  245
+      #
  246
+      #     attr_accessor :name
  247
+      #     attribute_method_suffix '_short?'
  248
+      #     define_attribute_method :name
  249
+      #
  250
+      #     private
  251
+      #
  252
+      #     def attribute_short?(attr)
  253
+      #       send(attr).length < 5
  254
+      #     end
  255
+      #   end
  256
+      #
  257
+      #   person = Person.new
  258
+      #   person.name = 'Bob'
  259
+      #   person.name        # => "Bob"
  260
+      #   person.name_short? # => true
236 261
       def define_attribute_method(attr_name)
237 262
         attribute_method_matchers.each do |matcher|
238 263
           method_name = matcher.method_name(attr_name)

0 notes on commit f975c4b

Please sign in to comment.
Something went wrong with that request. Please try again.