validates_timeliness/lib/validates_timeliness/attribute_methods.rb
Adam Meehan 34a2d4b558 methods for validated attributes for write cache and before type cast
cache raw value in @attributes_cache which is AR convention but should
work fine with non-AR ORM
before type cast method for reading back cached raw value
2010-08-03 15:01:57 +10:00

44 lines
1.1 KiB
Ruby

module ValidatesTimeliness
module AttributeMethods
extend ActiveSupport::Concern
included do
attribute_method_suffix "_before_type_cast"
end
module ClassMethods
protected
def define_method_attribute=(attr_name)
if timeliness_validated_attributes.include?(attr_name)
method_body, line = <<-EOV, __LINE__ + 1
def #{attr_name}=(value)
@attributes_cache ||= {}
@attributes_cache["_#{attr_name}_before_type_cast"] = value
super
end
EOV
class_eval(method_body, __FILE__, line)
else
super
end
end
def define_method_attribute_before_type_cast(attr_name)
if timeliness_validated_attributes.include?(attr_name)
method_body, line = <<-EOV, __LINE__ + 1
def #{attr_name}_before_type_cast
@attributes_cache && @attributes_cache["_#{attr_name}_before_type_cast"]
end
EOV
class_eval(method_body, __FILE__, line)
else
super rescue(NoMethodError)
end
end
end
end
end