validates_timeliness 7.0.0.beta1 → 7.0.0.beta2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/validates_timeliness/orm/active_model.rb +30 -6
- data/lib/validates_timeliness/version.rb +1 -1
- data/spec/spec_helper.rb +1 -4
- data/spec/support/model_helpers.rb +15 -8
- data/spec/support/test_model.rb +4 -52
- data/spec/validates_timeliness/attribute_methods_spec.rb +4 -0
- data/spec/validates_timeliness/validator/format_spec.rb +57 -0
- data/validates_timeliness.gemspec +1 -0
- metadata +5 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 291ec2c50666a8ce39654fb9c065ca22a27cc91413039a41faa1045e5143ee7e
|
4
|
+
data.tar.gz: 8ec042268f1524503c5daf4e0038769d64b0be9b0329281137a569579a5d991a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 0f7fd09d93f725dcbda325d6d681f1f68fd765ea75a5ae8d3206fe369fd9647f1ae6cb1be36d353c790482e310bd4f8cd4cc749fbb75a8aafc8b4653f7e76025
|
7
|
+
data.tar.gz: 433b16c8be0034ca7e9d8f6349ee9423a1437df4183e47efd623116b634287833f7b57f7f7a456bff06a1bbb9eb19df14c5cf73a0a6bb1e46d52f3ff112522df
|
@@ -6,30 +6,54 @@ module ValidatesTimeliness
|
|
6
6
|
module ClassMethods
|
7
7
|
public
|
8
8
|
|
9
|
+
# Hook into bulk method definitions for non-attribute based methods validated.
|
9
10
|
def define_attribute_methods(*attr_names)
|
10
|
-
super.tap {
|
11
|
+
super.tap {
|
12
|
+
define_timeliness_methods
|
13
|
+
}
|
14
|
+
end
|
15
|
+
|
16
|
+
# Called when `attribute` methods is called and the timeliness overrides are defined here
|
17
|
+
def define_attribute_method(attr_name)
|
18
|
+
super.tap {
|
19
|
+
define_attribute_timeliness_methods(attr_name)
|
20
|
+
}
|
11
21
|
end
|
12
22
|
|
13
23
|
def undefine_attribute_methods
|
14
|
-
super.tap {
|
24
|
+
super.tap {
|
25
|
+
undefine_timeliness_attribute_methods
|
26
|
+
}
|
15
27
|
end
|
16
28
|
|
17
29
|
def define_timeliness_methods(before_type_cast=false)
|
18
30
|
return if timeliness_validated_attributes.blank?
|
31
|
+
|
19
32
|
timeliness_validated_attributes.each do |attr_name|
|
20
|
-
|
33
|
+
unless timeliness_method_already_implemented?(attr_name)
|
34
|
+
define_attribute_timeliness_methods(attr_name, before_type_cast)
|
35
|
+
end
|
21
36
|
end
|
22
37
|
end
|
23
38
|
|
39
|
+
def define_timeliness_method(attr_name, before_type_cast=false)
|
40
|
+
define_attribute_timeliness_methods(attr_name, before_type_cast)
|
41
|
+
end
|
42
|
+
|
43
|
+
# Lazy instantiate module as container of timeliness methods included in the model
|
24
44
|
def generated_timeliness_methods
|
25
45
|
@generated_timeliness_methods ||= Module.new { |m|
|
26
46
|
extend Mutex_m
|
27
47
|
}.tap { |mod| include mod }
|
28
48
|
end
|
29
49
|
|
50
|
+
def timeliness_method_already_implemented?(method_name)
|
51
|
+
generated_timeliness_methods.method_defined?(method_name)
|
52
|
+
end
|
53
|
+
|
30
54
|
def undefine_timeliness_attribute_methods
|
31
55
|
generated_timeliness_methods.module_eval do
|
32
|
-
|
56
|
+
undef_method(*instance_methods)
|
33
57
|
end
|
34
58
|
end
|
35
59
|
|
@@ -44,7 +68,7 @@ module ValidatesTimeliness
|
|
44
68
|
@timeliness_cache ||= {}
|
45
69
|
@timeliness_cache['#{attr_name}'] = value
|
46
70
|
|
47
|
-
|
71
|
+
super
|
48
72
|
end
|
49
73
|
STR
|
50
74
|
end
|
@@ -59,7 +83,7 @@ module ValidatesTimeliness
|
|
59
83
|
end
|
60
84
|
|
61
85
|
def read_timeliness_attribute_before_type_cast(attr_name)
|
62
|
-
@timeliness_cache && @timeliness_cache[attr_name] || @attributes[attr_name]
|
86
|
+
@timeliness_cache && @timeliness_cache[attr_name] || @attributes[attr_name].value_before_type_cast
|
63
87
|
end
|
64
88
|
|
65
89
|
def _clear_timeliness_cache
|
data/spec/spec_helper.rb
CHANGED
@@ -33,8 +33,6 @@ I18n.available_locales = ['en', 'es']
|
|
33
33
|
# Extend TestModel as you would another ORM/ODM module
|
34
34
|
module TestModelShim
|
35
35
|
extend ActiveSupport::Concern
|
36
|
-
include ValidatesTimeliness::AttributeMethods
|
37
|
-
include ValidatesTimeliness::ORM::ActiveModel
|
38
36
|
|
39
37
|
module ClassMethods
|
40
38
|
# Hook into native time zone handling check, if any
|
@@ -46,11 +44,10 @@ end
|
|
46
44
|
|
47
45
|
class Person
|
48
46
|
include TestModel
|
47
|
+
|
49
48
|
attribute :birth_date, :date
|
50
49
|
attribute :birth_time, :time
|
51
50
|
attribute :birth_datetime, :datetime
|
52
|
-
|
53
|
-
define_attribute_methods model_attributes.keys
|
54
51
|
end
|
55
52
|
|
56
53
|
class PersonWithShim < Person
|
@@ -1,22 +1,29 @@
|
|
1
1
|
module ModelHelpers
|
2
2
|
|
3
3
|
# Some test helpers from Rails source
|
4
|
-
def invalid!(attr_name, values, error = nil)
|
5
|
-
|
6
|
-
expect(record).
|
4
|
+
def invalid!(attr_name, values, error = nil, model_class = Person)
|
5
|
+
with_each_model_value(attr_name, values, model_class) do |record, value|
|
6
|
+
expect(record).to_not be_valid
|
7
7
|
expect(record.errors[attr_name].size).to be >= 1
|
8
|
-
|
8
|
+
|
9
|
+
return unless error
|
10
|
+
|
11
|
+
if error.is_a?(Regexp)
|
12
|
+
expect(record.errors[attr_name].first).to match(error)
|
13
|
+
else
|
14
|
+
expect(record.errors[attr_name].first).to eq(error)
|
15
|
+
end
|
9
16
|
end
|
10
17
|
end
|
11
18
|
|
12
|
-
def valid!(attr_name, values)
|
13
|
-
|
19
|
+
def valid!(attr_name, values, model_class = Person)
|
20
|
+
with_each_model_value(attr_name, values, model_class) do |record, value|
|
14
21
|
expect(record).to be_valid
|
15
22
|
end
|
16
23
|
end
|
17
24
|
|
18
|
-
def
|
19
|
-
record =
|
25
|
+
def with_each_model_value(attr_name, values, model_class)
|
26
|
+
record = model_class.new
|
20
27
|
Array.wrap(values).each do |value|
|
21
28
|
record.send("#{attr_name}=", value)
|
22
29
|
yield record, value
|
data/spec/support/test_model.rb
CHANGED
@@ -1,61 +1,13 @@
|
|
1
1
|
module TestModel
|
2
2
|
extend ActiveSupport::Concern
|
3
|
-
|
3
|
+
include ActiveModel::Model
|
4
|
+
include ActiveModel::Attributes
|
4
5
|
include ActiveModel::Validations
|
5
|
-
include
|
6
|
+
include ValidatesTimeliness::AttributeMethods
|
7
|
+
include ValidatesTimeliness::ORM::ActiveModel
|
6
8
|
|
7
9
|
included do
|
8
10
|
attribute_method_suffix "="
|
9
|
-
cattr_accessor :model_attributes
|
10
|
-
end
|
11
|
-
|
12
|
-
module ClassMethods
|
13
|
-
def attribute(name, type)
|
14
|
-
self.model_attributes ||= {}
|
15
|
-
self.model_attributes[name] = type
|
16
|
-
end
|
17
|
-
|
18
|
-
def define_method_attribute=(attr_name, owner: nil)
|
19
|
-
generated_attribute_methods.module_eval("def #{attr_name}=(new_value); @attributes['#{attr_name}']=self.class.type_cast('#{attr_name}', new_value); end", __FILE__, __LINE__)
|
20
|
-
end
|
21
|
-
|
22
|
-
def define_method_attribute(attr_name, owner: nil)
|
23
|
-
generated_attribute_methods.module_eval("def #{attr_name}; @attributes['#{attr_name}']; end", __FILE__, __LINE__)
|
24
|
-
end
|
25
|
-
|
26
|
-
def type_cast(attr_name, value)
|
27
|
-
return value unless value.is_a?(String)
|
28
|
-
type_name = model_attributes[attr_name.to_sym]
|
29
|
-
type = ActiveModel::Type.lookup(type_name)
|
30
|
-
type.cast(value)
|
31
|
-
end
|
32
|
-
end
|
33
|
-
|
34
|
-
def initialize(attributes = nil)
|
35
|
-
@attributes = self.class.model_attributes.keys.inject({}) do |hash, column|
|
36
|
-
hash[column.to_s] = nil
|
37
|
-
hash
|
38
|
-
end
|
39
|
-
self.attributes = attributes unless attributes.nil?
|
40
|
-
end
|
41
|
-
|
42
|
-
def attributes
|
43
|
-
@attributes
|
44
|
-
end
|
45
|
-
|
46
|
-
def attributes=(new_attributes={})
|
47
|
-
new_attributes.each do |key, value|
|
48
|
-
send "#{key}=", value
|
49
|
-
end
|
50
|
-
end
|
51
|
-
|
52
|
-
def method_missing(method_id, *args, &block)
|
53
|
-
if !matched_attribute_method(method_id.to_s).nil?
|
54
|
-
self.class.define_attribute_methods self.class.model_attributes.keys
|
55
|
-
send(method_id, *args, &block)
|
56
|
-
else
|
57
|
-
super
|
58
|
-
end
|
59
11
|
end
|
60
12
|
end
|
61
13
|
|
@@ -44,9 +44,11 @@ RSpec.describe ValidatesTimeliness::AttributeMethods do
|
|
44
44
|
class PersonWithParser
|
45
45
|
include TestModel
|
46
46
|
include TestModelShim
|
47
|
+
|
47
48
|
attribute :birth_date, :date
|
48
49
|
attribute :birth_time, :time
|
49
50
|
attribute :birth_datetime, :datetime
|
51
|
+
|
50
52
|
validates_date :birth_date
|
51
53
|
validates_time :birth_time
|
52
54
|
validates_datetime :birth_datetime
|
@@ -54,8 +56,10 @@ RSpec.describe ValidatesTimeliness::AttributeMethods do
|
|
54
56
|
|
55
57
|
it 'should parse a string value' do
|
56
58
|
expect(Timeliness::Parser).to receive(:parse)
|
59
|
+
|
57
60
|
r = PersonWithParser.new
|
58
61
|
r.birth_date = '2010-01-01'
|
62
|
+
r.birth_date # parsing happens on read not write
|
59
63
|
end
|
60
64
|
|
61
65
|
end
|
@@ -0,0 +1,57 @@
|
|
1
|
+
RSpec.describe ValidatesTimeliness::Validator, ":format option" do
|
2
|
+
with_config(:use_plugin_parser, true)
|
3
|
+
|
4
|
+
describe "for date type" do
|
5
|
+
before do
|
6
|
+
Person.validates_date :birth_date, format: "yyyy-mm-dd"
|
7
|
+
end
|
8
|
+
|
9
|
+
it "should not be valid for string given in the wrong format" do
|
10
|
+
invalid!(:birth_date, '23/12/2023', /is not a valid date/)
|
11
|
+
end
|
12
|
+
|
13
|
+
it "should be valid for string given in the right format" do
|
14
|
+
valid!(:birth_date, '2023-12-23')
|
15
|
+
end
|
16
|
+
|
17
|
+
it "should be valid for date instance" do
|
18
|
+
valid!(:birth_date, Date.new(2022,12,23))
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
describe "for time type" do
|
23
|
+
before do
|
24
|
+
Person.validates_time :birth_time, format: "hh:nn:ss"
|
25
|
+
end
|
26
|
+
|
27
|
+
it "should not be valid for string given in the wrong format" do
|
28
|
+
invalid!(:birth_time, "00-00-00", /is not a valid time/)
|
29
|
+
end
|
30
|
+
|
31
|
+
it "should be valid for string given in the right format" do
|
32
|
+
valid!(:birth_time, "00:00:00")
|
33
|
+
end
|
34
|
+
|
35
|
+
it "should be valid for date instance" do
|
36
|
+
valid!(:birth_time, Time.new(2010, 1, 1, 0, 0, 0))
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
describe "for datetime type" do
|
41
|
+
before do
|
42
|
+
Person.validates_datetime :birth_datetime, format: "yyyy-mm-dd hh:nn:ss"
|
43
|
+
end
|
44
|
+
|
45
|
+
it "should not be valid for string given in the wrong format" do
|
46
|
+
invalid!(:birth_datetime, "01-01-2010 00-00-00", /is not a valid datetime/)
|
47
|
+
end
|
48
|
+
|
49
|
+
it "should be valid for string given in the right format" do
|
50
|
+
valid!(:birth_datetime, "2010-01-01 00:00:00")
|
51
|
+
end
|
52
|
+
|
53
|
+
it "should be valid for date instance" do
|
54
|
+
valid!(:birth_datetime, DateTime.new(2010, 1, 1, 0, 0, 0))
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
@@ -21,6 +21,7 @@ Gem::Specification.new do |s|
|
|
21
21
|
s.extra_rdoc_files = ["README.md", "CHANGELOG.md", "LICENSE"]
|
22
22
|
|
23
23
|
s.metadata = {
|
24
|
+
"rubygems_mfa_required" => "true",
|
24
25
|
"bug_tracker_uri" => "#{github_url}/issues",
|
25
26
|
"changelog_uri" => "#{github_url}/blob/master/CHANGELOG.md",
|
26
27
|
"source_code_uri" => "#{github_url}",
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: validates_timeliness
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 7.0.0.
|
4
|
+
version: 7.0.0.beta2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Adam Meehan
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2023-03-24 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activemodel
|
@@ -99,6 +99,7 @@ files:
|
|
99
99
|
- spec/validates_timeliness/railtie_spec.rb
|
100
100
|
- spec/validates_timeliness/validator/after_spec.rb
|
101
101
|
- spec/validates_timeliness/validator/before_spec.rb
|
102
|
+
- spec/validates_timeliness/validator/format_spec.rb
|
102
103
|
- spec/validates_timeliness/validator/is_at_spec.rb
|
103
104
|
- spec/validates_timeliness/validator/on_or_after_spec.rb
|
104
105
|
- spec/validates_timeliness/validator/on_or_before_spec.rb
|
@@ -109,6 +110,7 @@ homepage: https://github.com/adzap/validates_timeliness
|
|
109
110
|
licenses:
|
110
111
|
- MIT
|
111
112
|
metadata:
|
113
|
+
rubygems_mfa_required: 'true'
|
112
114
|
bug_tracker_uri: https://github.com/adzap/validates_timeliness/issues
|
113
115
|
changelog_uri: https://github.com/adzap/validates_timeliness/blob/master/CHANGELOG.md
|
114
116
|
source_code_uri: https://github.com/adzap/validates_timeliness
|
@@ -148,6 +150,7 @@ test_files:
|
|
148
150
|
- spec/validates_timeliness/railtie_spec.rb
|
149
151
|
- spec/validates_timeliness/validator/after_spec.rb
|
150
152
|
- spec/validates_timeliness/validator/before_spec.rb
|
153
|
+
- spec/validates_timeliness/validator/format_spec.rb
|
151
154
|
- spec/validates_timeliness/validator/is_at_spec.rb
|
152
155
|
- spec/validates_timeliness/validator/on_or_after_spec.rb
|
153
156
|
- spec/validates_timeliness/validator/on_or_before_spec.rb
|