validates_timeliness 3.0.0.beta → 3.0.0.beta.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README.rdoc +2 -2
- data/lib/validates_timeliness/attribute_methods.rb +1 -0
- data/lib/validates_timeliness/helper_methods.rb +15 -17
- data/lib/validates_timeliness/parser.rb +7 -11
- data/lib/validates_timeliness/version.rb +1 -1
- data/lib/validates_timeliness.rb +1 -1
- data/spec/spec_helper.rb +1 -0
- data/spec/validates_timeliness/attribute_methods_spec.rb +29 -2
- data/validates_timeliness.gemspec +2 -2
- metadata +4 -3
data/README.rdoc
CHANGED
@@ -29,10 +29,10 @@ As plugin (from master)
|
|
29
29
|
|
30
30
|
rails plugin install git://github.com/adzap/validates_timeliness.git
|
31
31
|
|
32
|
-
As gem (
|
32
|
+
As gem (in beta)
|
33
33
|
|
34
34
|
# in Gemfile
|
35
|
-
gem 'validates_timeliness'
|
35
|
+
gem 'validates_timeliness', '3.0.0.beta'
|
36
36
|
|
37
37
|
# Run bundler
|
38
38
|
$ bundle install
|
@@ -18,6 +18,7 @@ module ValidatesTimeliness
|
|
18
18
|
def #{attr_name}=(value)
|
19
19
|
@attributes_cache ||= {}
|
20
20
|
@attributes_cache["_#{attr_name}_before_type_cast"] = value
|
21
|
+
#{ "value = ValidatesTimeliness::Parser.parse(value, :#{type}) if value.is_a?(String)" if ValidatesTimeliness.use_plugin_parser }
|
21
22
|
super
|
22
23
|
end
|
23
24
|
EOV
|
@@ -10,30 +10,28 @@ module ValidatesTimeliness
|
|
10
10
|
end
|
11
11
|
|
12
12
|
module ValidationMethods
|
13
|
-
def validates_timeliness_of(*attr_names)
|
14
|
-
options = _merge_attributes(attr_names)
|
15
|
-
attributes = options[:attributes].inject({}) {|validated, attr_name|
|
16
|
-
attr_name = attr_name.to_s
|
17
|
-
validated[attr_name] = options[:type]
|
18
|
-
validated
|
19
|
-
}
|
20
|
-
self.timeliness_validated_attributes = attributes
|
21
|
-
validates_with Validator, options
|
22
|
-
end
|
23
|
-
|
24
13
|
def validates_date(*attr_names)
|
25
|
-
|
26
|
-
validates_timeliness_of *(attr_names << options.merge(:type => :date))
|
14
|
+
timeliness_validation_for attr_names, :date
|
27
15
|
end
|
28
16
|
|
29
17
|
def validates_time(*attr_names)
|
30
|
-
|
31
|
-
validates_timeliness_of *(attr_names << options.merge(:type => :time))
|
18
|
+
timeliness_validation_for attr_names, :time
|
32
19
|
end
|
33
20
|
|
34
21
|
def validates_datetime(*attr_names)
|
35
|
-
|
36
|
-
|
22
|
+
timeliness_validation_for attr_names, :datetime
|
23
|
+
end
|
24
|
+
|
25
|
+
def timeliness_validation_for(attr_names, type)
|
26
|
+
options = _merge_attributes(attr_names)
|
27
|
+
options[:type] = type
|
28
|
+
attributes = attr_names.inject({}) {|validated, attr_name|
|
29
|
+
attr_name = attr_name.to_s
|
30
|
+
validated[attr_name] = type
|
31
|
+
validated
|
32
|
+
}
|
33
|
+
self.timeliness_validated_attributes = attributes
|
34
|
+
validates_with Validator, options
|
37
35
|
end
|
38
36
|
|
39
37
|
end
|
@@ -10,15 +10,7 @@ module ValidatesTimeliness
|
|
10
10
|
# string values.
|
11
11
|
#
|
12
12
|
class Parser
|
13
|
-
|
14
|
-
:date_formats,
|
15
|
-
:datetime_formats,
|
16
|
-
:time_expressions,
|
17
|
-
:date_expressions,
|
18
|
-
:datetime_expressions,
|
19
|
-
:format_tokens,
|
20
|
-
:format_proc_args
|
21
|
-
|
13
|
+
cattr_reader :time_expressions, :date_expressions, :datetime_expressions
|
22
14
|
|
23
15
|
# Set the threshold value for a two digit year to be considered last century
|
24
16
|
#
|
@@ -73,6 +65,7 @@ module ValidatesTimeliness
|
|
73
65
|
# by the next lowest length valid repeating token (e.g. yyy will be
|
74
66
|
# replaced with yy)
|
75
67
|
|
68
|
+
cattr_accessor :time_formats
|
76
69
|
@@time_formats = [
|
77
70
|
'hh:nn:ss',
|
78
71
|
'hh-nn-ss',
|
@@ -87,6 +80,7 @@ module ValidatesTimeliness
|
|
87
80
|
'h_ampm'
|
88
81
|
]
|
89
82
|
|
83
|
+
cattr_accessor :date_formats
|
90
84
|
@@date_formats = [
|
91
85
|
'yyyy-mm-dd',
|
92
86
|
'yyyy/mm/dd',
|
@@ -101,6 +95,7 @@ module ValidatesTimeliness
|
|
101
95
|
'd mmm yy'
|
102
96
|
]
|
103
97
|
|
98
|
+
cattr_accessor :datetime_formats
|
104
99
|
@@datetime_formats = [
|
105
100
|
'yyyy-mm-dd hh:nn:ss',
|
106
101
|
'yyyy-mm-dd h:nn',
|
@@ -132,11 +127,11 @@ module ValidatesTimeliness
|
|
132
127
|
# is required. The first capture group will be considered a literal and put
|
133
128
|
# into the validation regexp string as-is. This is a hack.
|
134
129
|
#
|
130
|
+
cattr_accessor :format_tokens
|
135
131
|
@@format_tokens = {
|
136
132
|
'ddd' => [ '\w{3,9}' ],
|
137
133
|
'dd' => [ '\d{2}', :day ],
|
138
134
|
'd' => [ '\d{1,2}', :day ],
|
139
|
-
'ampm' => [ '[aApP]\.?[mM]\.?', :meridian ],
|
140
135
|
'mmm' => [ '\w{3,9}', :month ],
|
141
136
|
'mm' => [ '\d{2}', :month ],
|
142
137
|
'm' => [ '\d{1,2}', :month ],
|
@@ -149,6 +144,7 @@ module ValidatesTimeliness
|
|
149
144
|
'ss' => [ '\d{2}', :sec ],
|
150
145
|
's' => [ '\d{1,2}', :sec ],
|
151
146
|
'u' => [ '\d{1,6}', :usec ],
|
147
|
+
'ampm' => [ '[aApP]\.?[mM]\.?', :meridian ],
|
152
148
|
'zo' => [ '[+-]\d{2}:?\d{2}', :offset ],
|
153
149
|
'tz' => [ '[A-Z]{1,4}' ],
|
154
150
|
'_' => [ '\s?' ]
|
@@ -163,6 +159,7 @@ module ValidatesTimeliness
|
|
163
159
|
# The code can be used to manipulate the arg value if required, otherwise
|
164
160
|
# should just be the arg name.
|
165
161
|
#
|
162
|
+
cattr_accessor :format_proc_args
|
166
163
|
@@format_proc_args = {
|
167
164
|
:year => [0, 'y', 'unambiguous_year(y)'],
|
168
165
|
:month => [1, 'm', 'month_index(m)'],
|
@@ -175,7 +172,6 @@ module ValidatesTimeliness
|
|
175
172
|
:meridian => [nil, 'md', nil]
|
176
173
|
}
|
177
174
|
|
178
|
-
|
179
175
|
@@type_wrapper = {
|
180
176
|
:date => [/\A/, nil],
|
181
177
|
:time => [nil , /\Z/],
|
data/lib/validates_timeliness.rb
CHANGED
@@ -9,6 +9,7 @@ require 'active_support/core_ext/date_time/acts_like'
|
|
9
9
|
require 'active_support/core_ext/date_time/conversions'
|
10
10
|
|
11
11
|
module ValidatesTimeliness
|
12
|
+
autoload :Parser, 'validates_timeliness/parser'
|
12
13
|
autoload :VERSION, 'validates_timeliness/version'
|
13
14
|
|
14
15
|
# Add plugin to supported ORMs (only :active_record for now)
|
@@ -45,7 +46,6 @@ module ValidatesTimeliness
|
|
45
46
|
end
|
46
47
|
end
|
47
48
|
|
48
|
-
require 'validates_timeliness/parser'
|
49
49
|
require 'validates_timeliness/conversion'
|
50
50
|
require 'validates_timeliness/validator'
|
51
51
|
require 'validates_timeliness/helper_methods'
|
data/spec/spec_helper.rb
CHANGED
@@ -56,6 +56,7 @@ class Person
|
|
56
56
|
define_attribute_methods model_attributes
|
57
57
|
end
|
58
58
|
|
59
|
+
ActiveRecord::Base.time_zone_aware_attributes = true
|
59
60
|
ActiveRecord::Base.establish_connection({:adapter => 'sqlite3', :database => ':memory:'})
|
60
61
|
ActiveRecord::Migration.verbose = false
|
61
62
|
ActiveRecord::Schema.define(:version => 1) do
|
@@ -6,16 +6,43 @@ describe ValidatesTimeliness::AttributeMethods do
|
|
6
6
|
end
|
7
7
|
|
8
8
|
context "attribute write method" do
|
9
|
-
class
|
9
|
+
class EmployeeWithCache < ActiveRecord::Base
|
10
10
|
set_table_name 'employees'
|
11
11
|
validates_datetime :birth_datetime
|
12
12
|
end
|
13
13
|
|
14
14
|
it 'should cache attribute raw value' do
|
15
|
-
r =
|
15
|
+
r = EmployeeWithCache.new
|
16
16
|
r.birth_datetime = date_string = '2010-01-01'
|
17
17
|
r._timeliness_raw_value_for(:birth_datetime).should == date_string
|
18
18
|
end
|
19
|
+
|
20
|
+
context "with plugin parser" do
|
21
|
+
class EmployeeWithParser < ActiveRecord::Base
|
22
|
+
set_table_name 'employees'
|
23
|
+
validates_datetime :birth_date
|
24
|
+
end
|
25
|
+
|
26
|
+
before :all do
|
27
|
+
ValidatesTimeliness.use_plugin_parser = true
|
28
|
+
end
|
29
|
+
|
30
|
+
it 'should parse a string value' do
|
31
|
+
ValidatesTimeliness::Parser.should_receive(:parse)
|
32
|
+
r = EmployeeWithParser.new
|
33
|
+
r.birth_date = '2010-01-01'
|
34
|
+
end
|
35
|
+
|
36
|
+
it 'should be strict on day values' do
|
37
|
+
r = EmployeeWithParser.new
|
38
|
+
r.birth_date = '2010-02-31'
|
39
|
+
r.birth_date.should be_nil
|
40
|
+
end
|
41
|
+
|
42
|
+
after :all do
|
43
|
+
ValidatesTimeliness.use_plugin_parser = false
|
44
|
+
end
|
45
|
+
end
|
19
46
|
end
|
20
47
|
|
21
48
|
context "before_type_cast method" do
|
@@ -2,12 +2,12 @@
|
|
2
2
|
|
3
3
|
Gem::Specification.new do |s|
|
4
4
|
s.name = %q{validates_timeliness}
|
5
|
-
s.version = "3.0.0.beta"
|
5
|
+
s.version = "3.0.0.beta.2"
|
6
6
|
|
7
7
|
s.required_rubygems_version = Gem::Requirement.new("> 1.3.1") if s.respond_to? :required_rubygems_version=
|
8
8
|
s.authors = ["Adam Meehan"]
|
9
9
|
s.autorequire = %q{validates_timeliness}
|
10
|
-
s.date = %q{2010-09-
|
10
|
+
s.date = %q{2010-09-21}
|
11
11
|
s.description = %q{Date and time validation plugin for Rails which allows custom formats}
|
12
12
|
s.email = %q{adam.meehan@gmail.com}
|
13
13
|
s.extra_rdoc_files = ["README.rdoc", "LICENSE", "CHANGELOG"]
|
metadata
CHANGED
@@ -1,14 +1,15 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: validates_timeliness
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 62196423
|
5
5
|
prerelease: true
|
6
6
|
segments:
|
7
7
|
- 3
|
8
8
|
- 0
|
9
9
|
- 0
|
10
10
|
- beta
|
11
|
-
|
11
|
+
- 2
|
12
|
+
version: 3.0.0.beta.2
|
12
13
|
platform: ruby
|
13
14
|
authors:
|
14
15
|
- Adam Meehan
|
@@ -16,7 +17,7 @@ autorequire: validates_timeliness
|
|
16
17
|
bindir: bin
|
17
18
|
cert_chain: []
|
18
19
|
|
19
|
-
date: 2010-09-
|
20
|
+
date: 2010-09-21 00:00:00 +10:00
|
20
21
|
default_executable:
|
21
22
|
dependencies: []
|
22
23
|
|