From 6cd6cd9dc03be39810eb195f4b4cf1806dc6d9d5 Mon Sep 17 00:00:00 2001 From: Adam Meehan Date: Fri, 5 Dec 2008 18:45:28 +1100 Subject: [PATCH] made restriction_value instance method as it relies on validator instance attribute type removed old type_cast_method class method --- .../rails/matchers/validate_timeliness.rb | 2 +- lib/validates_timeliness/validator.rb | 18 ++++------------ spec/validator_spec.rb | 21 ++++++++++--------- 3 files changed, 16 insertions(+), 25 deletions(-) diff --git a/lib/validates_timeliness/spec/rails/matchers/validate_timeliness.rb b/lib/validates_timeliness/spec/rails/matchers/validate_timeliness.rb index 45d8b6d..ae1ffc4 100644 --- a/lib/validates_timeliness/spec/rails/matchers/validate_timeliness.rb +++ b/lib/validates_timeliness/spec/rails/matchers/validate_timeliness.rb @@ -70,7 +70,7 @@ module Spec end def parse_and_cast(value) - value = ValidatesTimeliness::Validator.send(:restriction_value, value, record, options[:type]) + value = validator.send(:restriction_value, value, record) validator.send(:type_cast_value, value) end diff --git a/lib/validates_timeliness/validator.rb b/lib/validates_timeliness/validator.rb index e128176..8b7e489 100644 --- a/lib/validates_timeliness/validator.rb +++ b/lib/validates_timeliness/validator.rb @@ -39,8 +39,6 @@ module ValidatesTimeliness def validate_restrictions(record, attr_name, value) restriction_methods = {:before => '<', :after => '>', :on_or_before => '<=', :on_or_after => '>='} - type_cast_method = self.class.restriction_type_cast_method(type) - display = ValidatesTimeliness.error_value_formats[type] value = type_cast_value(value) @@ -48,7 +46,7 @@ module ValidatesTimeliness restriction_methods.each do |option, method| next unless restriction = configuration[option] begin - compare = self.class.restriction_value(restriction, record, type) + compare = restriction_value(restriction, record) next if compare.nil? compare = type_cast_value(compare) @@ -85,27 +83,19 @@ module ValidatesTimeliness @custom_error_messages = configuration.inject({}) {|h, (k, v)| h[$1.to_sym] = v if k.to_s =~ /(.*)_message$/;h } end - def self.restriction_value(restriction, record, type) + def restriction_value(restriction, record) case restriction when Time, Date, DateTime restriction when Symbol - restriction_value(record.send(restriction), record, type) + restriction_value(record.send(restriction), record) when Proc - restriction_value(restriction.call(record), record, type) + restriction_value(restriction.call(record), record) else record.class.parse_date_time(restriction, type, false) end end - def self.restriction_type_cast_method(type) - case type - when :time then :to_dummy_time - when :date then :to_date - when :datetime then :to_time - end - end - def type_cast_value(value) case type when :time diff --git a/spec/validator_spec.rb b/spec/validator_spec.rb index 9183098..0295f54 100644 --- a/spec/validator_spec.rb +++ b/spec/validator_spec.rb @@ -18,33 +18,34 @@ describe ValidatesTimeliness::Validator do describe "restriction_value" do it "should return Time object when restriction is Time object" do - restriction_value(Time.now, person, :datetime).should be_kind_of(Time) + restriction_value(Time.now, :datetime).should be_kind_of(Time) end it "should return Time object when restriction is string" do - restriction_value("2007-01-01 12:00", person, :datetime).should be_kind_of(Time) + restriction_value("2007-01-01 12:00", :datetime).should be_kind_of(Time) end - it "should return Time object when restriction is method symbol which returns Time object" do + it "should return Time object when restriction is method and method returns Time object" do person.stub!(:datetime_attr).and_return(Time.now) - restriction_value(:datetime_attr, person, :datetime).should be_kind_of(Time) + restriction_value(:datetime_attr, :datetime).should be_kind_of(Time) end - it "should return Time object when restriction is method symbol which returns string" do + it "should return Time object when restriction is method and method returns string" do person.stub!(:datetime_attr).and_return("2007-01-01 12:00") - restriction_value(:datetime_attr, person, :datetime).should be_kind_of(Time) + restriction_value(:datetime_attr, :datetime).should be_kind_of(Time) end it "should return Time object when restriction is proc which returns Time object" do - restriction_value(lambda { Time.now }, person, :datetime).should be_kind_of(Time) + restriction_value(lambda { Time.now }, :datetime).should be_kind_of(Time) end it "should return Time object when restriction is proc which returns string" do - restriction_value(lambda {"2007-01-01 12:00"}, person, :datetime).should be_kind_of(Time) + restriction_value(lambda {"2007-01-01 12:00"}, :datetime).should be_kind_of(Time) end - def restriction_value(*args) - ValidatesTimeliness::Validator.send(:restriction_value, *args) + def restriction_value(restriction, type) + configure_validator(:type => type) + validator.send(:restriction_value, restriction, person) end end