mirror of
https://github.com/ditkrg/validates_timeliness.git
synced 2026-01-22 22:06:45 +00:00
refactored raw value cache to be more explicit
This commit is contained in:
parent
37593b63ba
commit
ff555eb293
@ -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
|
||||
|
||||
@ -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
|
||||
|
||||
@ -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
|
||||
|
||||
Loading…
Reference in New Issue
Block a user