Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.
AttributeMethods adds the ability for your class to have custom prefixes and suffixes on your methods. It is used by adding the definitions for the prefixes and suffixes, defining which methods on the object will use them, then implementing the common behavior for when those methods are called. An example implementation is as follows:
class Record
include ActiveModel::AttributeMethods
attribute_method_prefix 'reset_'
attribute_method_suffix '_highest?'
define_attribute_methods [ 'score' ]
attr_accessor :score
private
def reset_attribute(attribute)
send("#{attribute}=", nil)
end
def attribute_highest?(attribute)
attribute > 1000 ? true : false
end
end