refactored raw value cache to be more explicit

This commit is contained in:
Adam Meehan 2010-09-24 14:19:25 +10:00
parent 37593b63ba
commit ff555eb293
3 changed files with 42 additions and 21 deletions

View File

@ -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

View File

@ -1,8 +1,9 @@
class ActiveRecord::Base
include ValidatesTimeliness::HelperMethods
include ValidatesTimeliness::AttributeMethods
module ValidatesTimeliness
module ORM
module ActiveRecord
extend ActiveSupport::Concern
class << self
module ClassMethods
def define_attribute_methods
super
# Define write method and before_type_cast method
@ -18,4 +19,20 @@ class ActiveRecord::Base
columns_hash[attr_name.to_s].type
end
end
module InstanceMethods
def reload(*args)
super
_clear_timeliness_cache
end
end
end
end
end
class ActiveRecord::Base
include ValidatesTimeliness::HelperMethods
include ValidatesTimeliness::AttributeMethods
include ValidatesTimeliness::ORM::ActiveRecord
end

View File

@ -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