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
|
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
|
||||||
|
|||||||
@ -1,8 +1,9 @@
|
|||||||
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
|
||||||
@ -18,4 +19,20 @@ class ActiveRecord::Base
|
|||||||
columns_hash[attr_name.to_s].type
|
columns_hash[attr_name.to_s].type
|
||||||
end
|
end
|
||||||
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
|
end
|
||||||
|
|||||||
@ -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
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user