Permalink
Browse files

add example to ActiveModel::AttributeMethods#undefine_attribute_metho…

…ds [ci skip]
  • Loading branch information...
Francesco Rodriguez
Francesco Rodriguez committed Jun 21, 2012
1 parent f975c4b commit 95a938a5e69a0513a3666f5979be2460fa717885
Showing with 26 additions and 0 deletions.
  1. +26 −0 activemodel/lib/active_model/attribute_methods.rb
@@ -245,6 +245,10 @@ def define_attribute_methods(*attr_names)
#
# attr_accessor :name
# attribute_method_suffix '_short?'
+ #
+ # # Call to define_attribute_method must appear after the
+ # # attribute_method_prefix, attribute_method_suffix or
+ # # attribute_method_affix declares.
# define_attribute_method :name
#
# private
@@ -276,6 +280,28 @@ def define_attribute_method(attr_name)
end
# Removes all the previously dynamically defined methods from the class
+ #
+ # 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_short? # => true
+ #
+ # Person.undefine_attribute_methods
+ #
+ # person.name_short? # => NoMethodError
def undefine_attribute_methods
generated_attribute_methods.module_eval do
instance_methods.each { |m| undef_method(m) }

0 comments on commit 95a938a

Please sign in to comment.