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 method_body, line = <<-EOV, __LINE__ + 1
def #{attr_name}=(value) def #{attr_name}=(value)
@attributes_cache ||= {} @timeliness_cache ||= {}
@attributes_cache["_#{attr_name}_before_type_cast"] = value @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 } #{ "value = ValidatesTimeliness::Parser.parse(value, :#{type}, :timezone_aware => #{timezone_aware}) if value.is_a?(String)" if ValidatesTimeliness.use_plugin_parser }
super super
end end
@ -53,7 +53,11 @@ module ValidatesTimeliness
module InstanceMethods module InstanceMethods
def _timeliness_raw_value_for(attr_name) 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
end end

View File

@ -1,21 +1,38 @@
class ActiveRecord::Base module ValidatesTimeliness
include ValidatesTimeliness::HelperMethods module ORM
include ValidatesTimeliness::AttributeMethods module ActiveRecord
extend ActiveSupport::Concern
class << self module ClassMethods
def define_attribute_methods def define_attribute_methods
super super
# Define write method and before_type_cast method # Define write method and before_type_cast method
define_timeliness_methods(true) define_timeliness_methods(true)
end end
def timeliness_attribute_timezone_aware?(attr_name) def timeliness_attribute_timezone_aware?(attr_name)
attr_name = attr_name.to_s attr_name = attr_name.to_s
create_time_zone_conversion_attribute?(attr_name, columns_hash[attr_name]) create_time_zone_conversion_attribute?(attr_name, columns_hash[attr_name])
end 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 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) type = timeliness_attribute_type(attr_name)
method_body, line = <<-EOV, __LINE__ + 1 method_body, line = <<-EOV, __LINE__ + 1
def #{attr_name}=(value) def #{attr_name}=(value)
@attributes_cache ||= {} @timeliness_cache ||= {}
@attributes_cache["_#{attr_name}_before_type_cast"] = value @timeliness_cache["#{attr_name}"] = value
#{ "value = ValidatesTimeliness::Parser.parse(value, :#{type}) if value.is_a?(String)" if ValidatesTimeliness.use_plugin_parser } #{ "value = ValidatesTimeliness::Parser.parse(value, :#{type}) if value.is_a?(String)" if ValidatesTimeliness.use_plugin_parser }
write_attribute(:#{attr_name}, value) write_attribute(:#{attr_name}, value)
end end
@ -48,7 +48,7 @@ module Mongoid::Document
include ValidatesTimeliness::ORM::Mongoid include ValidatesTimeliness::ORM::Mongoid
def reload_with_timeliness def reload_with_timeliness
@attributes_cache = {} _clear_timeliness_cache
reload_without_timeliness reload_without_timeliness
end end
alias_method_chain :reload, :timeliness alias_method_chain :reload, :timeliness