mirror of
https://github.com/ditkrg/validates_timeliness.git
synced 2026-01-23 06:16:44 +00:00
moved formats to root module and retrieve from there in validations
added benchmarks for alternates
This commit is contained in:
parent
0dcc255901
commit
69ed88a086
42
benchmark.rb
Normal file
42
benchmark.rb
Normal file
@ -0,0 +1,42 @@
|
||||
require 'date'
|
||||
require 'parsedate'
|
||||
require 'benchmark'
|
||||
require 'rubygems'
|
||||
require 'rails/version'
|
||||
require 'active_support'
|
||||
require 'active_record'
|
||||
require 'action_controller'
|
||||
require 'action_view'
|
||||
$: << 'lib'
|
||||
require 'lib/validates_timeliness'
|
||||
|
||||
n = 10000
|
||||
Benchmark.bm do |x|
|
||||
x.report('time') {
|
||||
n.times do
|
||||
"2000-01-04 12:12:12" =~ /\A(\d{4})-(\d{2})-(\d{2}) (\d{2})[\. :](\d{2})([\. :](\d{2}))?\Z/
|
||||
Time.mktime($1, $2, $3, $3, $5, $6)
|
||||
end
|
||||
}
|
||||
|
||||
x.report('parsedate') {
|
||||
n.times do
|
||||
arr = ParseDate.parsedate("2000-01-04 12:12:12")
|
||||
Date.new(*arr[0..2])
|
||||
Time.mktime(*arr)
|
||||
end
|
||||
}
|
||||
|
||||
x.report('timeliness') {
|
||||
n.times do
|
||||
ActiveRecord::Base.timeliness_date_time_parse("2000-01-04 12:12:12", :datetime)
|
||||
end
|
||||
}
|
||||
|
||||
x.report('datetime') {
|
||||
n.times do
|
||||
DateTime.strptime("2000-01-04 12:12:12", '%Y-%m-%d %H:%M:%s')
|
||||
end
|
||||
}
|
||||
end
|
||||
|
||||
@ -1,12 +1,60 @@
|
||||
module ValidatesTimeliness
|
||||
mattr_accessor :valid_time_formats
|
||||
mattr_accessor :valid_date_formats
|
||||
mattr_accessor :valid_datetime_formats
|
||||
|
||||
# The if you want to combine a time regexp with a date regexp then you
|
||||
# should not use line begin or end anchors in the expression. Pre and post
|
||||
# match strings are still checked for validity, and fail the match if they
|
||||
# are not empty.
|
||||
#
|
||||
# The proc object should return an array with 1-3 elements with values
|
||||
# ordered like so [hour, minute, second]. The proc should have as many
|
||||
# arguments as groups in the regexp or you will get an error.
|
||||
@@valid_time_formats = {
|
||||
:hhnnss_colons => /(\d{2}):(\d{2}):(\d{2})/,
|
||||
:hhnnss_dashes => /(\d{2})-(\d{2})-(\d{2})/,
|
||||
:hhnn_colons => /(\d{2}):(\d{2})/,
|
||||
:hnn_dots => /(\d{1,2})\.(\d{2})/,
|
||||
:hnn_spaces => /(\d{1,2})\s(\d{2})/,
|
||||
:hnn_dashes => /(\d{1,2})-(\d{2})/,
|
||||
:hnn_ampm_colons => [ /(\d{1,2}):(\d{2})\s?((?:a|p)\.?m\.?)/i, lambda {|h, n, md| [full_hour(h, md), n, 0] } ],
|
||||
:hnn_ampm_dots => [ /(\d{1,2})\.(\d{2})\s?((?:a|p)\.?m\.?)/i, lambda {|h, n, md| [full_hour(h, md), n, 0] } ],
|
||||
:hnn_ampm_spaces => [ /(\d{1,2})\s(\d{2})\s?((?:a|p)\.?m\.?)/i, lambda {|h, n, md| [full_hour(h, md), n, 0] } ],
|
||||
:hnn_ampm_dashes => [ /(\d{1,2})-(\d{2})\s?((?:a|p)\.?m\.?)/i, lambda {|h, n, md| [full_hour(h, md), n, 0] } ],
|
||||
:h_ampm => [ /(\d{1,2})\s?((?:a|p)\.?m\.?)/i, lambda {|h, md| [full_hour(h, md), 0, 0] } ]
|
||||
}
|
||||
|
||||
# The proc object should return an array with 3 elements with values
|
||||
# ordered like so year, month, day. The proc should have as many
|
||||
# arguments as groups in the regexp or you will get an error.
|
||||
@@valid_date_formats = {
|
||||
:yyyymmdd_slashes => /(\d{4})\/(\d{2})\/(\d{2})/,
|
||||
:yyyymmdd_dashes => /(\d{4})-(\d{2})-(\d{2})/,
|
||||
:yyyymmdd_slashes => /(\d{4})\.(\d{2})\.(\d{2})/,
|
||||
:mdyyyy_slashes => [ /(\d{1,2})\/(\d{1,2})\/(\d{4})/, lambda {|m, d, y| [y, m, d] } ],
|
||||
:dmyyyy_slashes => [ /(\d{1,2})\/(\d{1,2})\/(\d{4})/, lambda {|d, m ,y| [y, m, d] } ],
|
||||
:dmyyyy_dashes => [ /(\d{1,2})-(\d{1,2})-(\d{4})/, lambda {|d, m ,y| [y, m, d] } ],
|
||||
:dmyyyy_dots => [ /(\d{1,2})\.(\d{1,2})\.(\d{4})/, lambda {|d, m ,y| [y, m, d] } ],
|
||||
:mdyy_slashes => [ /(\d{1,2})\/(\d{1,2})\/(\d{2})/, lambda {|m, d ,y| [unambiguous_year(y), m, d] } ],
|
||||
:dmyy_slashes => [ /(\d{1,2})\/(\d{1,2})\/(\d{2})/, lambda {|d, m ,y| [unambiguous_year(y), m, d] } ],
|
||||
:dmyy_dashes => [ /(\d{1,2})-(\d{1,2})-(\d{2})/, lambda {|d, m ,y| [unambiguous_year(y), m, d] } ],
|
||||
:dmyy_dots => [ /(\d{1,2})\.(\d{1,2})\.(\d{2})/, lambda {|d, m ,y| [unambiguous_year(y), m, d] } ],
|
||||
:d_mmm_yyyy => [ /(\d{1,2}) (\w{3,9}) (\d{4})/, lambda {|d, m ,y| [y, m, d] } ],
|
||||
:d_mmm_yy => [ /(\d{1,2}) (\w{3,9}) (\d{2})/, lambda {|d, m ,y| [unambiguous_year(y), m, d] } ]
|
||||
}
|
||||
|
||||
@@valid_datetime_formats = {
|
||||
:yyyymmdd_dashes_hhnnss_colons => /#{valid_date_formats[:yyyymmdd_dashes]}\s#{valid_time_formats[:hhnnss_colons]}/,
|
||||
:yyyymmdd_dashes_hhnn_colons => /#{valid_date_formats[:yyyymmdd_dashes]}\s#{valid_time_formats[:hhnn_colons]}/,
|
||||
:iso8601 => /#{valid_date_formats[:yyyymmdd_dashes]}T#{valid_time_formats[:hhnnss_colons]}(?:Z|[-+](\d{2}):(\d{2}))?/
|
||||
}
|
||||
|
||||
|
||||
# Adds ActiveRecord validation methods for date, time and datetime validation.
|
||||
# The validity of values can be restricted to be before and/or certain dates
|
||||
# or times.
|
||||
module Validations
|
||||
mattr_accessor :valid_time_formats
|
||||
mattr_accessor :valid_date_formats
|
||||
mattr_accessor :valid_datetime_formats
|
||||
|
||||
# Error messages added to AR defaults to allow global override if you need.
|
||||
def self.included(base)
|
||||
@ -20,64 +68,8 @@ module ValidatesTimeliness
|
||||
:on_or_after => "must be on or after %s"
|
||||
}
|
||||
ActiveRecord::Errors.default_error_messages.update(error_messages)
|
||||
|
||||
base.class_inheritable_hash :valid_time_formats
|
||||
base.class_inheritable_hash :valid_date_formats
|
||||
base.class_inheritable_hash :valid_datetime_formats
|
||||
|
||||
base.valid_time_formats = self.valid_time_formats
|
||||
base.valid_date_formats = self.valid_date_formats
|
||||
base.valid_datetime_formats = self.valid_datetime_formats
|
||||
end
|
||||
|
||||
|
||||
# The if you want to combine a time regexp with a date regexp then you
|
||||
# should not use line begin or end anchors in the expression. Pre and post
|
||||
# match strings are still checked for validity, and fail the match if they
|
||||
# are not empty.
|
||||
#
|
||||
# The proc object should return an array with 1-3 elements with values
|
||||
# ordered like so [hour, minute, second]. The proc should have as many
|
||||
# arguments as groups in the regexp or you will get an error.
|
||||
self.valid_time_formats = {
|
||||
:hhnnss_colons => /(\d{2}):(\d{2}):(\d{2})/,
|
||||
:hhnnss_dashes => /(\d{2})-(\d{2})-(\d{2})/,
|
||||
:hhnn_colons => /(\d{2}):(\d{2})/,
|
||||
:hnn_dots => /(\d{1,2})\.(\d{2})/,
|
||||
:hnn_spaces => /(\d{1,2})\s(\d{2})/,
|
||||
:hnn_dashes => /(\d{1,2})-(\d{2})/,
|
||||
:hnn_ampm_colons => [ /(\d{1,2}):(\d{2})\s?((?:a|p)\.?m\.?)/i, lambda {|h, n, md| [full_hour(h, md), n, 0] } ],
|
||||
:hnn_ampm_dots => [ /(\d{1,2})\.(\d{2})\s?((?:a|p)\.?m\.?)/i, lambda {|h, n, md| [full_hour(h, md), n, 0] } ],
|
||||
:hnn_ampm_spaces => [ /(\d{1,2})\s(\d{2})\s?((?:a|p)\.?m\.?)/i, lambda {|h, n, md| [full_hour(h, md), n, 0] } ],
|
||||
:hnn_ampm_dashes => [ /(\d{1,2})-(\d{2})\s?((?:a|p)\.?m\.?)/i, lambda {|h, n, md| [full_hour(h, md), n, 0] } ],
|
||||
:h_ampm => [ /(\d{1,2})\s?((?:a|p)\.?m\.?)/i, lambda {|h, md| [full_hour(h, md), 0, 0] } ]
|
||||
}
|
||||
|
||||
# The proc object should return an array with 3 elements with values
|
||||
# ordered like so year, month, day. The proc should have as many
|
||||
# arguments as groups in the regexp or you will get an error.
|
||||
self.valid_date_formats = {
|
||||
:yyyymmdd_slashes => /(\d{4})\/(\d{2})\/(\d{2})/,
|
||||
:yyyymmdd_dashes => /(\d{4})-(\d{2})-(\d{2})/,
|
||||
:yyyymmdd_slashes => /(\d{4})\.(\d{2})\.(\d{2})/,
|
||||
:mdyyyy_slashes => [ /(\d{1,2})\/(\d{1,2})\/(\d{4})/, lambda {|m, d, y| [y, m, d] } ],
|
||||
:dmyyyy_slashes => [ /(\d{1,2})\/(\d{1,2})\/(\d{4})/, lambda {|d, m ,y| [y, m, d] } ],
|
||||
:dmyyyy_dashes => [ /(\d{1,2})-(\d{1,2})-(\d{4})/, lambda {|d, m ,y| [y, m, d] } ],
|
||||
:dmyyyy_dots => [ /(\d{1,2})\.(\d{1,2})\.(\d{4})/, lambda {|d, m ,y| [y, m, d] } ],
|
||||
:mdyy_slashes => [ /(\d{1,2})\/(\d{1,2})\/(\d{2})/, lambda {|m, d ,y| [unambiguous_year(y), m, d] } ],
|
||||
:dmyy_slashes => [ /(\d{1,2})\/(\d{1,2})\/(\d{2})/, lambda {|d, m ,y| [unambiguous_year(y), m, d] } ],
|
||||
:dmyy_dashes => [ /(\d{1,2})-(\d{1,2})-(\d{2})/, lambda {|d, m ,y| [unambiguous_year(y), m, d] } ],
|
||||
:dmyy_dots => [ /(\d{1,2})\.(\d{1,2})\.(\d{2})/, lambda {|d, m ,y| [unambiguous_year(y), m, d] } ],
|
||||
:d_mmm_yyyy => [ /(\d{1,2}) (\w{3,9}) (\d{4})/, lambda {|d, m ,y| [y, m, d] } ],
|
||||
:d_mmm_yy => [ /(\d{1,2}) (\w{3,9}) (\d{2})/, lambda {|d, m ,y| [unambiguous_year(y), m, d] } ]
|
||||
}
|
||||
|
||||
self.valid_datetime_formats = {
|
||||
:yyyymmdd_dashes_hhnnss_colons => /#{valid_date_formats[:yyyymmdd_dashes]}\s#{valid_time_formats[:hhnnss_colons]}/,
|
||||
:yyyymmdd_dashes_hhnn_colons => /#{valid_date_formats[:yyyymmdd_dashes]}\s#{valid_time_formats[:hhnn_colons]}/,
|
||||
:iso8601 => /#{valid_date_formats[:yyyymmdd_dashes]}T#{valid_time_formats[:hhnnss_colons]}(?:Z|[-+](\d{2}):(\d{2}))?/
|
||||
}
|
||||
|
||||
module ClassMethods
|
||||
|
||||
def full_hour(hour, meridian)
|
||||
@ -121,7 +113,7 @@ module ValidatesTimeliness
|
||||
def timeliness_date_time_parse(raw_value, type, strict=true)
|
||||
return raw_value.to_time if raw_value.acts_like?(:time) || raw_value.is_a?(Date)
|
||||
|
||||
time_array = extract_date_time_values(raw_value, self.send("valid_#{type}_formats".to_sym), strict)
|
||||
time_array = extract_date_time_values(raw_value, ValidatesTimeliness.send("valid_#{type}_formats".to_sym), strict)
|
||||
raise if time_array.nil?
|
||||
|
||||
if type == :time
|
||||
|
||||
Loading…
Reference in New Issue
Block a user