From ff555eb293c7c560b43a3e16bcb30443ee18cde0 Mon Sep 17 00:00:00 2001 From: Adam Meehan Date: Fri, 24 Sep 2010 14:19:25 +1000 Subject: [PATCH] refactored raw value cache to be more explicit --- lib/validates_timeliness/attribute_methods.rb | 10 ++-- lib/validates_timeliness/orm/active_record.rb | 47 +++++++++++++------ lib/validates_timeliness/orm/mongoid.rb | 6 +-- 3 files changed, 42 insertions(+), 21 deletions(-) diff --git a/lib/validates_timeliness/attribute_methods.rb b/lib/validates_timeliness/attribute_methods.rb index 8e0350e..98408ad 100644 --- a/lib/validates_timeliness/attribute_methods.rb +++ b/lib/validates_timeliness/attribute_methods.rb @@ -20,8 +20,8 @@ module ValidatesTimeliness method_body, line = <<-EOV, __LINE__ + 1 def #{attr_name}=(value) - @attributes_cache ||= {} - @attributes_cache["_#{attr_name}_before_type_cast"] = value + @timeliness_cache ||= {} + @timeliness_cache["#{attr_name}"] = value #{ "value = ValidatesTimeliness::Parser.parse(value, :#{type}, :timezone_aware => #{timezone_aware}) if value.is_a?(String)" if ValidatesTimeliness.use_plugin_parser } super end @@ -53,7 +53,11 @@ module ValidatesTimeliness module InstanceMethods def _timeliness_raw_value_for(attr_name) - @attributes_cache && @attributes_cache["_#{attr_name}_before_type_cast"] + @timeliness_cache && @timeliness_cache[attr_name.to_s] + end + + def _clear_timeliness_cache + @timeliness_cache = {} end end diff --git a/lib/validates_timeliness/orm/active_record.rb b/lib/validates_timeliness/orm/active_record.rb index 8220548..1c4dd1f 100644 --- a/lib/validates_timeliness/orm/active_record.rb +++ b/lib/validates_timeliness/orm/active_record.rb @@ -1,21 +1,38 @@ -class ActiveRecord::Base - include ValidatesTimeliness::HelperMethods - include ValidatesTimeliness::AttributeMethods +module ValidatesTimeliness + module ORM + module ActiveRecord + extend ActiveSupport::Concern - class << self - def define_attribute_methods - super - # Define write method and before_type_cast method - define_timeliness_methods(true) - end + module ClassMethods + def define_attribute_methods + super + # Define write method and before_type_cast method + define_timeliness_methods(true) + end - def timeliness_attribute_timezone_aware?(attr_name) - attr_name = attr_name.to_s - create_time_zone_conversion_attribute?(attr_name, columns_hash[attr_name]) - end + def timeliness_attribute_timezone_aware?(attr_name) + attr_name = attr_name.to_s + create_time_zone_conversion_attribute?(attr_name, columns_hash[attr_name]) + end + + def timeliness_attribute_type(attr_name) + columns_hash[attr_name.to_s].type + end + end + + module InstanceMethods + def reload(*args) + super + _clear_timeliness_cache + end + end - def timeliness_attribute_type(attr_name) - columns_hash[attr_name.to_s].type end end end + +class ActiveRecord::Base + include ValidatesTimeliness::HelperMethods + include ValidatesTimeliness::AttributeMethods + include ValidatesTimeliness::ORM::ActiveRecord +end diff --git a/lib/validates_timeliness/orm/mongoid.rb b/lib/validates_timeliness/orm/mongoid.rb index 850b24e..62d1834 100644 --- a/lib/validates_timeliness/orm/mongoid.rb +++ b/lib/validates_timeliness/orm/mongoid.rb @@ -20,8 +20,8 @@ module ValidatesTimeliness type = timeliness_attribute_type(attr_name) method_body, line = <<-EOV, __LINE__ + 1 def #{attr_name}=(value) - @attributes_cache ||= {} - @attributes_cache["_#{attr_name}_before_type_cast"] = value + @timeliness_cache ||= {} + @timeliness_cache["#{attr_name}"] = value #{ "value = ValidatesTimeliness::Parser.parse(value, :#{type}) if value.is_a?(String)" if ValidatesTimeliness.use_plugin_parser } write_attribute(:#{attr_name}, value) end @@ -48,7 +48,7 @@ module Mongoid::Document include ValidatesTimeliness::ORM::Mongoid def reload_with_timeliness - @attributes_cache = {} + _clear_timeliness_cache reload_without_timeliness end alias_method_chain :reload, :timeliness