daily_affirmation 0.6.0 → 0.7.0
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.
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ad1c5a4b4e1e7249d7c2d5dbf1fa15f12359e42c
|
4
|
+
data.tar.gz: f78b555770fa41e2d8fa5067386fc222f4cedcd1
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 021ecc75cfd176b1c0dcef4c20483390eae03c1cd6fbc70ed1ecb59a017668c26407ef50d953b82d95e2f69a073c8bd68cae5755d0ff848d495abbbe8d92e268
|
7
|
+
data.tar.gz: 59f2f6394cf6d359e39b8a7c441b2ae9882ce7e6bd63f993a4690418dd114c06d418b29810a5339f427e164ca99bb2153f8cc3cb0a1aab6b67c2eb0c4c5e1785
|
@@ -0,0 +1,43 @@
|
|
1
|
+
require_relative "../validator"
|
2
|
+
|
3
|
+
module DailyAffirmation
|
4
|
+
module Validators
|
5
|
+
class DateValidator < Validator
|
6
|
+
def valid?
|
7
|
+
@valid ||= parseable?
|
8
|
+
end
|
9
|
+
|
10
|
+
def error_message
|
11
|
+
@error_message ||= i18n_error_message(
|
12
|
+
:format, :default => "#{attribute} is not a valid #{as}"
|
13
|
+
)
|
14
|
+
end
|
15
|
+
|
16
|
+
private
|
17
|
+
|
18
|
+
def parseable?
|
19
|
+
value.is_a?(klass) || !!klass.parse(value)
|
20
|
+
rescue ArgumentError
|
21
|
+
false
|
22
|
+
end
|
23
|
+
|
24
|
+
def as
|
25
|
+
opts.fetch(:as, :date)
|
26
|
+
end
|
27
|
+
|
28
|
+
def klass
|
29
|
+
case as
|
30
|
+
when :date
|
31
|
+
Date
|
32
|
+
when :datetime
|
33
|
+
DateTime
|
34
|
+
when :time
|
35
|
+
Time
|
36
|
+
else
|
37
|
+
raise OptionError,
|
38
|
+
"Invalid value for `:as`: #{opts[:as]}. Valid options are :date, :datetime and :time."
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
data/lib/daily_affirmation.rb
CHANGED
@@ -4,6 +4,7 @@ require_relative "daily_affirmation/validators/absence_validator"
|
|
4
4
|
require_relative "daily_affirmation/validators/acceptance_validator"
|
5
5
|
require_relative "daily_affirmation/validators/confirmation_validator"
|
6
6
|
require_relative "daily_affirmation/validators/custom_validator"
|
7
|
+
require_relative "daily_affirmation/validators/date_validator"
|
7
8
|
require_relative "daily_affirmation/validators/equality_validator"
|
8
9
|
require_relative "daily_affirmation/validators/exclusion_validator"
|
9
10
|
require_relative "daily_affirmation/validators/format_validator"
|
@@ -14,6 +15,8 @@ require_relative "daily_affirmation/validators/presence_validator"
|
|
14
15
|
require_relative "daily_affirmation/version"
|
15
16
|
|
16
17
|
module DailyAffirmation
|
18
|
+
OptionError = Class.new(StandardError)
|
19
|
+
|
17
20
|
def self.affirmations
|
18
21
|
Module.new do
|
19
22
|
def self.included(descendant)
|
data/spec/validator_spec.rb
CHANGED
@@ -21,7 +21,11 @@ describe "Validator" do
|
|
21
21
|
end
|
22
22
|
|
23
23
|
context "i18n required" do
|
24
|
-
before(:all)
|
24
|
+
before(:all) do
|
25
|
+
require "i18n"
|
26
|
+
I18n.enforce_available_locales = false
|
27
|
+
end
|
28
|
+
|
25
29
|
after(:all) { Object.send(:remove_const, :I18n) if defined?(I18n) }
|
26
30
|
|
27
31
|
it "uses specific translation if available" do
|
@@ -0,0 +1,135 @@
|
|
1
|
+
require_relative "../spec_helper"
|
2
|
+
require_relative "../../lib/daily_affirmation/validators/date_validator"
|
3
|
+
|
4
|
+
require "date"
|
5
|
+
|
6
|
+
unless defined?(DailyAffirmation::OptionError)
|
7
|
+
DailyAffirmation::OptionError = Class.new(StandardError)
|
8
|
+
end
|
9
|
+
|
10
|
+
describe "DateValidator" do
|
11
|
+
subject { DailyAffirmation::Validators::DateValidator }
|
12
|
+
|
13
|
+
context ":as is unset" do
|
14
|
+
it "passes validation if the attribute is a valid date" do
|
15
|
+
obj1 = double(:created_at => Date.today)
|
16
|
+
obj2 = double(:created_at => Date.today.to_s)
|
17
|
+
|
18
|
+
validator1 = subject.new(obj1, :created_at)
|
19
|
+
validator2 = subject.new(obj2, :created_at)
|
20
|
+
expect(validator1).to be_valid
|
21
|
+
expect(validator2).to be_valid
|
22
|
+
end
|
23
|
+
|
24
|
+
it "fails validation if the attribute is an invalid date" do
|
25
|
+
obj = double(:created_at => "Invalid Date")
|
26
|
+
|
27
|
+
validator = subject.new(obj, :created_at)
|
28
|
+
expect(validator).to_not be_valid
|
29
|
+
end
|
30
|
+
|
31
|
+
it "has the correct error message" do
|
32
|
+
obj = double(:created_at => "Invalid Date")
|
33
|
+
|
34
|
+
validator = subject.new(obj, :created_at)
|
35
|
+
expect(validator.error_message).to eq(
|
36
|
+
"created_at is not a valid date"
|
37
|
+
)
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
context ":as is :date" do
|
42
|
+
it "passes validation if the attribute is a valid date" do
|
43
|
+
obj1 = double(:created_at => Date.today)
|
44
|
+
obj2 = double(:created_at => Date.today.to_s)
|
45
|
+
|
46
|
+
validator1 = subject.new(obj1, :created_at, :as => :date)
|
47
|
+
validator2 = subject.new(obj2, :created_at, :as => :date)
|
48
|
+
expect(validator1).to be_valid
|
49
|
+
expect(validator2).to be_valid
|
50
|
+
end
|
51
|
+
|
52
|
+
it "fails validation if the attribute is an invalid date" do
|
53
|
+
obj = double(:created_at => "Invalid Date")
|
54
|
+
|
55
|
+
validator = subject.new(obj, :created_at, :as => :date)
|
56
|
+
expect(validator).to_not be_valid
|
57
|
+
end
|
58
|
+
|
59
|
+
it "has the correct error message" do
|
60
|
+
obj = double(:created_at => "Invalid Date")
|
61
|
+
|
62
|
+
validator = subject.new(obj, :created_at, :as => :date)
|
63
|
+
expect(validator.error_message).to eq(
|
64
|
+
"created_at is not a valid date"
|
65
|
+
)
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
context ":as is :datetime" do
|
70
|
+
it "passes validation if the attribute is a valid datetime" do
|
71
|
+
obj1 = double(:created_at => DateTime.now)
|
72
|
+
obj2 = double(:created_at => DateTime.now.to_s)
|
73
|
+
|
74
|
+
validator1 = subject.new(obj1, :created_at, :as => :datetime)
|
75
|
+
validator2 = subject.new(obj2, :created_at, :as => :datetime)
|
76
|
+
expect(validator1).to be_valid
|
77
|
+
expect(validator2).to be_valid
|
78
|
+
end
|
79
|
+
|
80
|
+
it "fails validation if the attribute is an invalid datetime" do
|
81
|
+
obj = double(:created_at => "Invalid DateTime")
|
82
|
+
|
83
|
+
validator = subject.new(obj, :created_at, :as => :datetime)
|
84
|
+
expect(validator).to_not be_valid
|
85
|
+
end
|
86
|
+
|
87
|
+
it "has the correct error message" do
|
88
|
+
obj = double(:created_at => "Invalid DateTime")
|
89
|
+
|
90
|
+
validator = subject.new(obj, :created_at, :as => :datetime)
|
91
|
+
expect(validator.error_message).to eq(
|
92
|
+
"created_at is not a valid datetime"
|
93
|
+
)
|
94
|
+
end
|
95
|
+
end
|
96
|
+
|
97
|
+
context ":as is :time" do
|
98
|
+
it "passes validation if the attribute is a valid time" do
|
99
|
+
obj1 = double(:created_at => Time.now)
|
100
|
+
obj2 = double(:created_at => Time.now.to_s)
|
101
|
+
|
102
|
+
validator1 = subject.new(obj1, :created_at, :as => :time)
|
103
|
+
validator2 = subject.new(obj2, :created_at, :as => :time)
|
104
|
+
expect(validator1).to be_valid
|
105
|
+
expect(validator2).to be_valid
|
106
|
+
end
|
107
|
+
|
108
|
+
it "fails validation if the attribute is an invalid time" do
|
109
|
+
obj = double(:created_at => "Invalid Time")
|
110
|
+
|
111
|
+
validator = subject.new(obj, :created_at, :as => :time)
|
112
|
+
expect(validator).to_not be_valid
|
113
|
+
end
|
114
|
+
|
115
|
+
it "has the correct error message" do
|
116
|
+
obj = double(:created_at => "Invalid Time")
|
117
|
+
|
118
|
+
validator = subject.new(obj, :created_at, :as => :time)
|
119
|
+
expect(validator.error_message).to eq(
|
120
|
+
"created_at is not a valid time"
|
121
|
+
)
|
122
|
+
end
|
123
|
+
end
|
124
|
+
|
125
|
+
context ":as is invalid" do
|
126
|
+
it "raises an ArgumentError" do
|
127
|
+
obj = double(:created_at => "Invalid Time")
|
128
|
+
|
129
|
+
validator = subject.new(obj, :created_at, :as => :foo)
|
130
|
+
expect {
|
131
|
+
validator.valid?
|
132
|
+
}.to raise_error(DailyAffirmation::OptionError)
|
133
|
+
end
|
134
|
+
end
|
135
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: daily_affirmation
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.7.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Shane Emmons
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-08-
|
11
|
+
date: 2014-08-12 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -87,6 +87,7 @@ files:
|
|
87
87
|
- lib/daily_affirmation/validators/acceptance_validator.rb
|
88
88
|
- lib/daily_affirmation/validators/confirmation_validator.rb
|
89
89
|
- lib/daily_affirmation/validators/custom_validator.rb
|
90
|
+
- lib/daily_affirmation/validators/date_validator.rb
|
90
91
|
- lib/daily_affirmation/validators/equality_validator.rb
|
91
92
|
- lib/daily_affirmation/validators/exclusion_validator.rb
|
92
93
|
- lib/daily_affirmation/validators/format_validator.rb
|
@@ -105,6 +106,7 @@ files:
|
|
105
106
|
- spec/validators/acceptance_validator_spec.rb
|
106
107
|
- spec/validators/confirmation_validator_spec.rb
|
107
108
|
- spec/validators/custom_validator_spec.rb
|
109
|
+
- spec/validators/date_validator_spec.rb
|
108
110
|
- spec/validators/equality_validator_spec.rb
|
109
111
|
- spec/validators/exclusion_validator_spec.rb
|
110
112
|
- spec/validators/format_validator_spec.rb
|
@@ -147,6 +149,7 @@ test_files:
|
|
147
149
|
- spec/validators/acceptance_validator_spec.rb
|
148
150
|
- spec/validators/confirmation_validator_spec.rb
|
149
151
|
- spec/validators/custom_validator_spec.rb
|
152
|
+
- spec/validators/date_validator_spec.rb
|
150
153
|
- spec/validators/equality_validator_spec.rb
|
151
154
|
- spec/validators/exclusion_validator_spec.rb
|
152
155
|
- spec/validators/format_validator_spec.rb
|