You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Ruby_Calisthenics/attr_accessor_with_history.rb

29 lines
725 B

# By alan snape
# All rights reserved.
class Class
def attr_accessor_with_history(attr_name)
attr_name = attr_name.to_s # make sure it's a string
attr_reader attr_name # create the attribute's getter
attr_reader attr_name+"_history" # create bar_history getter
class_eval %Q|
@#{attr_name} = nil
@#{attr_name}_history = nil
def #{attr_name}
@#{attr_name}
end
def #{attr_name}=(value)
if @#{attr_name} then
@#{attr_name}_history.push(@#{attr_name})
else
@#{attr_name}_history = [@#{attr_name}]
end
@#{attr_name} = value
end
def #{attr_name}_history
@#{attr_name}_history
end
|
end
end