add example to ActiveModel::AttributeMethods#undefine_attribute_methods [ci skip]
This commit is contained in:
parent
f975c4b641
commit
95a938a5e6
@ -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) }
|
||||
|
Loading…
Reference in New Issue
Block a user