From 8c1ba048df505f8cc35b182ad47f21706c0eb0ab Mon Sep 17 00:00:00 2001 From: Adam Meehan Date: Mon, 6 Feb 2012 22:47:08 +1100 Subject: [PATCH] Refactor timeliness method definition --- lib/validates_timeliness/attribute_methods.rb | 8 +++-- lib/validates_timeliness/orm/active_record.rb | 36 +++++++++---------- 2 files changed, 24 insertions(+), 20 deletions(-) diff --git a/lib/validates_timeliness/attribute_methods.rb b/lib/validates_timeliness/attribute_methods.rb index d23f496..34c9647 100644 --- a/lib/validates_timeliness/attribute_methods.rb +++ b/lib/validates_timeliness/attribute_methods.rb @@ -30,11 +30,15 @@ module ValidatesTimeliness def define_timeliness_methods(before_type_cast=false) return if timeliness_validated_attributes.blank? timeliness_validated_attributes.each do |attr_name| - define_timeliness_write_method(attr_name) - define_timeliness_before_type_cast_method(attr_name) if before_type_cast + define_attribute_timeliness_methods(attr_name, before_type_cast) end end + def define_attribute_timeliness_methods(attr_name, before_type_cast=false) + define_timeliness_write_method(attr_name) + define_timeliness_before_type_cast_method(attr_name) if before_type_cast + end + def define_timeliness_write_method(attr_name) method_body, line = <<-EOV, __LINE__ + 1 def #{attr_name}=(value) diff --git a/lib/validates_timeliness/orm/active_record.rb b/lib/validates_timeliness/orm/active_record.rb index 00f2bbb..eb235dc 100644 --- a/lib/validates_timeliness/orm/active_record.rb +++ b/lib/validates_timeliness/orm/active_record.rb @@ -17,6 +17,17 @@ module ValidatesTimeliness end module ClassMethods + public + + 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 + def define_attribute_methods super.tap do |attribute_methods_generated| use_before_type_cast = ValidatesTimeliness::ORM::ActiveRecord.use_plugin_cache? @@ -24,16 +35,14 @@ module ValidatesTimeliness end end - def define_timeliness_methods(before_type_cast=false) - return if timeliness_validated_attributes.blank? + protected - timeliness_validated_attributes.each do |attr_name| - if before_type_cast - define_timeliness_write_method(attr_name) - define_timeliness_before_type_cast_method(attr_name) - elsif ValidatesTimeliness.use_plugin_parser - define_timeliness_write_method_without_cache(attr_name) - end + def define_attribute_timeliness_methods(attr_name, before_type_cast=false) + if before_type_cast + define_timeliness_write_method(attr_name) + define_timeliness_before_type_cast_method(attr_name) + elsif ValidatesTimeliness.use_plugin_parser + define_timeliness_write_method_without_cache(attr_name) end end @@ -49,15 +58,6 @@ module ValidatesTimeliness generated_timeliness_methods.module_eval(method_body, __FILE__, line) 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 - def timeliness_type_cast_code(attr_name, var_name) type = timeliness_attribute_type(attr_name)