periods_validator 0.1.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 +7 -0
- data/.gitignore +50 -0
- data/.travis.yml +17 -0
- data/Appraisals +27 -0
- data/Gemfile +3 -0
- data/Gemfile.lock +63 -0
- data/LICENSE +21 -0
- data/LICENSE.txt +22 -0
- data/README.md +44 -0
- data/Rakefile +11 -0
- data/lib/periods_validator.rb +4 -0
- data/lib/periods_validator/helper_methods.rb +9 -0
- data/lib/periods_validator/periods.rb +66 -0
- data/lib/periods_validator/validator.rb +32 -0
- data/lib/periods_validator/version.rb +3 -0
- data/periods_validator.gemspec +27 -0
- data/spec/periods_validator/periods_spec.rb +191 -0
- data/spec/spec_helper.rb +80 -0
- metadata +133 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: e5db4ccda483c360211d7d5c97c51357321d181953085644a4cf00e0921dd40e
|
|
4
|
+
data.tar.gz: 8c2df14ac4ec17bc996fcc7924500da94af03cb477c33cfeed1f7c119f3eb04a
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: 3581ba63326725f1d1bf482a4382054f6980b2a526483ebd1badb973c1b676862bcb918075ac0c4094c3cb9138f6c09169dbe3d4aee7ce35ff2579c1c5c72ce5
|
|
7
|
+
data.tar.gz: dda20025b5593f59cffca65c0534c9e701ad0a04922608e2c9c9b8cf6a4795e63cd67b6be76dc85459c9802cd715283e0bf43634641956df9c7fe0f5f0fdf68d
|
data/.gitignore
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
*.gem
|
|
2
|
+
*.rbc
|
|
3
|
+
/.config
|
|
4
|
+
/coverage/
|
|
5
|
+
/InstalledFiles
|
|
6
|
+
/pkg/
|
|
7
|
+
/spec/reports/
|
|
8
|
+
/spec/examples.txt
|
|
9
|
+
/test/tmp/
|
|
10
|
+
/test/version_tmp/
|
|
11
|
+
/tmp/
|
|
12
|
+
|
|
13
|
+
# Used by dotenv library to load environment variables.
|
|
14
|
+
# .env
|
|
15
|
+
|
|
16
|
+
## Specific to RubyMotion:
|
|
17
|
+
.dat*
|
|
18
|
+
.repl_history
|
|
19
|
+
build/
|
|
20
|
+
*.bridgesupport
|
|
21
|
+
build-iPhoneOS/
|
|
22
|
+
build-iPhoneSimulator/
|
|
23
|
+
|
|
24
|
+
## Specific to RubyMotion (use of CocoaPods):
|
|
25
|
+
#
|
|
26
|
+
# We recommend against adding the Pods directory to your .gitignore. However
|
|
27
|
+
# you should judge for yourself, the pros and cons are mentioned at:
|
|
28
|
+
# https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control
|
|
29
|
+
#
|
|
30
|
+
# vendor/Pods/
|
|
31
|
+
|
|
32
|
+
## Documentation cache and generated files:
|
|
33
|
+
/.yardoc/
|
|
34
|
+
/_yardoc/
|
|
35
|
+
/doc/
|
|
36
|
+
/rdoc/
|
|
37
|
+
|
|
38
|
+
## Environment normalization:
|
|
39
|
+
/.bundle/
|
|
40
|
+
/vendor/bundle
|
|
41
|
+
/lib/bundler/man/
|
|
42
|
+
|
|
43
|
+
# for a library or gem, you might want to ignore these files since the code is
|
|
44
|
+
# intended to run in multiple environments; otherwise, check them in:
|
|
45
|
+
# Gemfile.lock
|
|
46
|
+
# .ruby-version
|
|
47
|
+
# .ruby-gemset
|
|
48
|
+
|
|
49
|
+
# unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
|
|
50
|
+
.rvmrc
|
data/.travis.yml
ADDED
data/Appraisals
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
appraise 'rails-3.2' do
|
|
2
|
+
gem 'activemodel', '~> 3.2.0'
|
|
3
|
+
end
|
|
4
|
+
|
|
5
|
+
appraise 'rails-4.0' do
|
|
6
|
+
gem 'activemodel', '~> 4.0.0'
|
|
7
|
+
end
|
|
8
|
+
|
|
9
|
+
appraise 'rails-4.1' do
|
|
10
|
+
gem 'activemodel', '~> 4.1.0'
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
appraise 'rails-4.2' do
|
|
14
|
+
gem 'activemodel', '~> 4.2.0'
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
appraise 'rails-5.0' do
|
|
18
|
+
gem 'activemodel', '~> 5.0.0'
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
appraise 'rails-5.1' do
|
|
22
|
+
gem 'activemodel', '~> 5.1.0'
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
appraise 'rails-5.2' do
|
|
26
|
+
gem 'activemodel', '~> 5.2.0'
|
|
27
|
+
end
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
PATH
|
|
2
|
+
remote: .
|
|
3
|
+
specs:
|
|
4
|
+
periods_validator (0.1.0)
|
|
5
|
+
|
|
6
|
+
GEM
|
|
7
|
+
remote: https://rubygems.org/
|
|
8
|
+
specs:
|
|
9
|
+
activesupport (5.2.3)
|
|
10
|
+
concurrent-ruby (~> 1.0, >= 1.0.2)
|
|
11
|
+
i18n (>= 0.7, < 2)
|
|
12
|
+
minitest (~> 5.1)
|
|
13
|
+
tzinfo (~> 1.1)
|
|
14
|
+
appraisal (2.1.0)
|
|
15
|
+
bundler
|
|
16
|
+
rake
|
|
17
|
+
thor (>= 0.14.0)
|
|
18
|
+
byebug (10.0.2)
|
|
19
|
+
coderay (1.1.2)
|
|
20
|
+
concurrent-ruby (1.1.5)
|
|
21
|
+
diff-lcs (1.3)
|
|
22
|
+
i18n (1.6.0)
|
|
23
|
+
concurrent-ruby (~> 1.0)
|
|
24
|
+
method_source (0.9.0)
|
|
25
|
+
minitest (5.11.3)
|
|
26
|
+
pry (0.11.3)
|
|
27
|
+
coderay (~> 1.1.0)
|
|
28
|
+
method_source (~> 0.9.0)
|
|
29
|
+
pry-byebug (3.6.0)
|
|
30
|
+
byebug (~> 10.0)
|
|
31
|
+
pry (~> 0.10)
|
|
32
|
+
rake (12.3.1)
|
|
33
|
+
rspec (3.7.0)
|
|
34
|
+
rspec-core (~> 3.7.0)
|
|
35
|
+
rspec-expectations (~> 3.7.0)
|
|
36
|
+
rspec-mocks (~> 3.7.0)
|
|
37
|
+
rspec-core (3.7.1)
|
|
38
|
+
rspec-support (~> 3.7.0)
|
|
39
|
+
rspec-expectations (3.7.0)
|
|
40
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
|
41
|
+
rspec-support (~> 3.7.0)
|
|
42
|
+
rspec-mocks (3.7.0)
|
|
43
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
|
44
|
+
rspec-support (~> 3.7.0)
|
|
45
|
+
rspec-support (3.7.1)
|
|
46
|
+
thor (0.20.0)
|
|
47
|
+
thread_safe (0.3.6)
|
|
48
|
+
tzinfo (1.2.5)
|
|
49
|
+
thread_safe (~> 0.1)
|
|
50
|
+
|
|
51
|
+
PLATFORMS
|
|
52
|
+
ruby
|
|
53
|
+
|
|
54
|
+
DEPENDENCIES
|
|
55
|
+
activesupport
|
|
56
|
+
appraisal
|
|
57
|
+
bundler (~> 1.5)
|
|
58
|
+
periods_validator!
|
|
59
|
+
pry-byebug
|
|
60
|
+
rspec
|
|
61
|
+
|
|
62
|
+
BUNDLED WITH
|
|
63
|
+
1.17.3
|
data/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2018 pacop
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
data/LICENSE.txt
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
Copyright (c) 2015 Rafael Jurado
|
|
2
|
+
|
|
3
|
+
MIT License
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
|
6
|
+
a copy of this software and associated documentation files (the
|
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
|
11
|
+
the following conditions:
|
|
12
|
+
|
|
13
|
+
The above copyright notice and this permission notice shall be
|
|
14
|
+
included in all copies or substantial portions of the Software.
|
|
15
|
+
|
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
# periods_validator
|
|
2
|
+
[](https://travis-ci.org/jponferrada26/periods_validator)
|
|
3
|
+
#### Gem used to validate periods between a range of dates.
|
|
4
|
+
|
|
5
|
+
The periods allowed are the following:
|
|
6
|
+
- monthly
|
|
7
|
+
- quarterly
|
|
8
|
+
- semesterly
|
|
9
|
+
- yearly
|
|
10
|
+
|
|
11
|
+
These periods start from the first day of the month corresponding to the first date, until the last day of the month which corresponds to the last date.
|
|
12
|
+
|
|
13
|
+
For now, the models in which this validation is used must have the fields:
|
|
14
|
+
- start_range
|
|
15
|
+
- end_range
|
|
16
|
+
|
|
17
|
+
## Installation
|
|
18
|
+
Add this line to your application's Gemfile:
|
|
19
|
+
```ruby
|
|
20
|
+
gem 'periods_validator'
|
|
21
|
+
```
|
|
22
|
+
## Examples
|
|
23
|
+
```ruby
|
|
24
|
+
class Model < ApplicationRecord
|
|
25
|
+
validates :start_range, presence: true, periods: %i[monthly yearly]
|
|
26
|
+
validates :end_range, presence: true
|
|
27
|
+
end
|
|
28
|
+
```
|
|
29
|
+
## Errors
|
|
30
|
+
The validator can return different errors depending on the data entered or the periods indicated by the developer.
|
|
31
|
+
|
|
32
|
+
There are several:
|
|
33
|
+
- invalid_define_period: It occurs when the developer has not indicated a period allowed by the validator. For example:
|
|
34
|
+
```ruby
|
|
35
|
+
validates :start_range, periods: %i[ other_period ]
|
|
36
|
+
# ---- or ----
|
|
37
|
+
validates :start_range, periods: %i[]
|
|
38
|
+
```
|
|
39
|
+
- invalid_period: It occurs when the date of start_range or end_range are not filled or when the range formed by both dates does not match any required period. For example:
|
|
40
|
+
```ruby
|
|
41
|
+
validates :start_range, periods: %i[ monthly ]
|
|
42
|
+
# start_range -> 01/01/2015
|
|
43
|
+
# end_range -> 01/12/2015 / or nil
|
|
44
|
+
```
|
data/Rakefile
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
module PeriodsValidator
|
|
2
|
+
class Periods
|
|
3
|
+
attr_reader :error
|
|
4
|
+
|
|
5
|
+
PERIODS = %i[monthly quarterly semesterly yearly].freeze
|
|
6
|
+
MONTHS_QUARTERLY = [3, 6, 9, 12].freeze
|
|
7
|
+
MONTHS_SEMESTERLY = [6, 12].freeze
|
|
8
|
+
YEARLY = 11
|
|
9
|
+
QUARTERLY = 2
|
|
10
|
+
SEMESTERLY = 5
|
|
11
|
+
|
|
12
|
+
def initialize(args={})
|
|
13
|
+
@start_range = args.fetch(:start_range)
|
|
14
|
+
@end_range = args.fetch(:end_range)
|
|
15
|
+
@periods = args.fetch(:periods, [])
|
|
16
|
+
@error = nil
|
|
17
|
+
@months = 0
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
def valid?
|
|
21
|
+
if @periods.empty? || !(@periods - PERIODS).empty?
|
|
22
|
+
@error = :invalid_define_period
|
|
23
|
+
return false
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
if @start_range.nil? || @end_range.nil?
|
|
27
|
+
@error = :invalid_period
|
|
28
|
+
return false
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
@months = (@end_range.year * 12 + @end_range.month) - (@start_range.year * 12 +
|
|
32
|
+
@start_range.month)
|
|
33
|
+
|
|
34
|
+
@periods.each do |period|
|
|
35
|
+
return true if send("period_#{period}")
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
@error = :invalid_period
|
|
39
|
+
false
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
alias validate valid?
|
|
43
|
+
|
|
44
|
+
protected
|
|
45
|
+
|
|
46
|
+
def limits_date
|
|
47
|
+
@start_range == @start_range.beginning_of_month && @end_range == @end_range.at_end_of_month
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
def period_monthly
|
|
51
|
+
(@end_range - @start_range).to_i == @start_range.at_end_of_month.day - 1
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
def period_quarterly
|
|
55
|
+
limits_date && @months == QUARTERLY && MONTHS_QUARTERLY.include?(@end_range.month)
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
def period_semesterly
|
|
59
|
+
limits_date && @months == SEMESTERLY && MONTHS_SEMESTERLY.include?(@end_range.month)
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
def period_yearly
|
|
63
|
+
limits_date && @months == YEARLY && @start_range.month == 1 && @end_range.month == 12
|
|
64
|
+
end
|
|
65
|
+
end
|
|
66
|
+
end
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
require 'active_model/validator'
|
|
2
|
+
|
|
3
|
+
module PeriodsValidator
|
|
4
|
+
class Validator < ActiveModel::EachValidator
|
|
5
|
+
def initialize(options)
|
|
6
|
+
@end_range = options.fetch(:end_range, nil)
|
|
7
|
+
@end_range_attribute = options.fetch(:end_range_attribute, :end_range)
|
|
8
|
+
@periods = options.fetch(:in, [])
|
|
9
|
+
|
|
10
|
+
super
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
def validate_each(record, _attribute, value)
|
|
14
|
+
end_range = @end_range || record.send(@end_range_attribute)
|
|
15
|
+
options = {
|
|
16
|
+
start_range: value,
|
|
17
|
+
end_range: end_range,
|
|
18
|
+
periods: @periods
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
validator = PeriodsValidator::Periods.new(options)
|
|
22
|
+
|
|
23
|
+
return if validator.valid?
|
|
24
|
+
|
|
25
|
+
record.errors.add(:base, validator.error)
|
|
26
|
+
end
|
|
27
|
+
end
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
unless defined? ActiveModel::Validations::PeriodsValidator
|
|
31
|
+
ActiveModel::Validations::PeriodsValidator = PeriodsValidator::Validator
|
|
32
|
+
end
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
# coding: utf-8
|
|
2
|
+
|
|
3
|
+
lib = File.expand_path('lib', __dir__)
|
|
4
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
|
5
|
+
require 'periods_validator/version'
|
|
6
|
+
|
|
7
|
+
Gem::Specification.new do |spec|
|
|
8
|
+
spec.name = 'periods_validator'
|
|
9
|
+
spec.version = PeriodsValidator::VERSION
|
|
10
|
+
spec.authors = ['Javier Ponferrada López']
|
|
11
|
+
spec.email = ['jponferrada26@gmail.com']
|
|
12
|
+
spec.summary = 'Validates periods between two date ranges'
|
|
13
|
+
spec.description = 'Validates periods between two date ranges'
|
|
14
|
+
spec.homepage = ''
|
|
15
|
+
spec.license = 'MIT'
|
|
16
|
+
|
|
17
|
+
spec.files = `git ls-files -z`.split("\x0")
|
|
18
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
|
19
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
|
20
|
+
spec.require_paths = ['lib']
|
|
21
|
+
|
|
22
|
+
spec.add_development_dependency 'activesupport'
|
|
23
|
+
spec.add_development_dependency 'appraisal'
|
|
24
|
+
spec.add_development_dependency 'bundler', '~> 1.5'
|
|
25
|
+
spec.add_development_dependency 'pry-byebug'
|
|
26
|
+
spec.add_development_dependency 'rspec'
|
|
27
|
+
end
|
|
@@ -0,0 +1,191 @@
|
|
|
1
|
+
require 'spec_helper'
|
|
2
|
+
|
|
3
|
+
RSpec.describe PeriodsValidator::Periods do
|
|
4
|
+
let(:options) do
|
|
5
|
+
{
|
|
6
|
+
monthly: {
|
|
7
|
+
start_range: Date.new(2015, 1, 1), end_range: Date.new(2015, 1, 31), periods: %i[monthly]
|
|
8
|
+
},
|
|
9
|
+
quarterly: {
|
|
10
|
+
start_range: Date.new(2015, 4, 1), end_range: Date.new(2015, 6, 30), periods: %i[quarterly]
|
|
11
|
+
},
|
|
12
|
+
semesterly: {
|
|
13
|
+
start_range: Date.new(2015, 1, 1), end_range: Date.new(2015, 6, 30), periods: %i[semesterly]
|
|
14
|
+
},
|
|
15
|
+
yearly: {
|
|
16
|
+
start_range: Date.new(2015, 1, 1), end_range: Date.new(2015, 12, 31), periods: %i[yearly]
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
context 'when exists periods' do
|
|
22
|
+
describe 'One Period' do
|
|
23
|
+
%i[monthly quarterly semesterly yearly].each do |period|
|
|
24
|
+
context "when period is #{period}" do
|
|
25
|
+
context 'when range is valid' do
|
|
26
|
+
before do
|
|
27
|
+
@validator = PeriodsValidator::Periods.new(options[period])
|
|
28
|
+
@validator.valid?
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
it 'hides errors' do
|
|
32
|
+
expect(@validator.error).to be_nil
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
context "when range isn\'t #{period}" do
|
|
37
|
+
let(:option_invalid) do
|
|
38
|
+
option = options[period]
|
|
39
|
+
option[:end_range] = option[:end_range].next_month
|
|
40
|
+
option
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
before do
|
|
44
|
+
@validator = PeriodsValidator::Periods.new(option_invalid)
|
|
45
|
+
@validator.valid?
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
it 'shows errors' do
|
|
49
|
+
expect(@validator.error).to eq(:invalid_period)
|
|
50
|
+
end
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
context 'when end_range isn\'t end day of month' do
|
|
54
|
+
let(:option_invalid) do
|
|
55
|
+
option = options[period]
|
|
56
|
+
option[:end_range] = option[:end_range].beginning_of_month
|
|
57
|
+
option
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
before do
|
|
61
|
+
@validator = PeriodsValidator::Periods.new(option_invalid)
|
|
62
|
+
@validator.valid?
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
it 'shows errors' do
|
|
66
|
+
expect(@validator.error).to eq(:invalid_period)
|
|
67
|
+
end
|
|
68
|
+
end
|
|
69
|
+
end
|
|
70
|
+
end
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
describe 'Cases complex' do
|
|
74
|
+
context 'when is yearly and quarterly' do
|
|
75
|
+
before do
|
|
76
|
+
@validator = PeriodsValidator::Periods.new(start_range: Date.new(2019, 1, 1),
|
|
77
|
+
end_range: Date.new(2020, 3, 31),
|
|
78
|
+
periods: %i[quarterly yearly])
|
|
79
|
+
@validator.valid?
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
it 'shows errors' do
|
|
83
|
+
expect(@validator.error).to eq(:invalid_period)
|
|
84
|
+
end
|
|
85
|
+
end
|
|
86
|
+
|
|
87
|
+
context 'when yearly isn\'t period real' do
|
|
88
|
+
before do
|
|
89
|
+
@validator = PeriodsValidator::Periods.new(start_range: Date.new(2015, 2, 1),
|
|
90
|
+
end_range: Date.new(2016, 1, 31),
|
|
91
|
+
periods: %i[yearly])
|
|
92
|
+
@validator.valid?
|
|
93
|
+
end
|
|
94
|
+
|
|
95
|
+
it 'shows errors' do
|
|
96
|
+
expect(@validator.error).to eq(:invalid_period)
|
|
97
|
+
end
|
|
98
|
+
end
|
|
99
|
+
|
|
100
|
+
context 'when quarterly isn\'t period real' do
|
|
101
|
+
before do
|
|
102
|
+
@validator = PeriodsValidator::Periods.new(start_range: Date.new(2015, 2, 1),
|
|
103
|
+
end_range: Date.new(2015, 4, 30),
|
|
104
|
+
periods: %i[yearly])
|
|
105
|
+
@validator.valid?
|
|
106
|
+
end
|
|
107
|
+
|
|
108
|
+
it 'shows errors' do
|
|
109
|
+
expect(@validator.error).to eq(:invalid_period)
|
|
110
|
+
end
|
|
111
|
+
end
|
|
112
|
+
|
|
113
|
+
context 'when semesterly isn\'t period real' do
|
|
114
|
+
before do
|
|
115
|
+
@validator = PeriodsValidator::Periods.new(start_range: Date.new(2015, 2, 1),
|
|
116
|
+
end_range: Date.new(2015, 7, 31),
|
|
117
|
+
periods: %i[yearly])
|
|
118
|
+
@validator.valid?
|
|
119
|
+
end
|
|
120
|
+
|
|
121
|
+
it 'shows errors' do
|
|
122
|
+
expect(@validator.error).to eq(:invalid_period)
|
|
123
|
+
end
|
|
124
|
+
end
|
|
125
|
+
end
|
|
126
|
+
|
|
127
|
+
describe 'More Periods' do
|
|
128
|
+
context 'when range is valid' do
|
|
129
|
+
let(:option) do
|
|
130
|
+
{start_range: Date.new(2015, 1, 1), end_range: Date.new(2015, 1, 31),
|
|
131
|
+
periods: %i[quarterly monthly]}
|
|
132
|
+
end
|
|
133
|
+
|
|
134
|
+
before do
|
|
135
|
+
@validator = PeriodsValidator::Periods.new(option)
|
|
136
|
+
@validator.valid?
|
|
137
|
+
end
|
|
138
|
+
|
|
139
|
+
it 'hides errors' do
|
|
140
|
+
expect(@validator.error).to be_nil
|
|
141
|
+
end
|
|
142
|
+
end
|
|
143
|
+
|
|
144
|
+
context 'when range is invalid' do
|
|
145
|
+
let(:option_invalid) do
|
|
146
|
+
{start_range: Date.new(2015, 2, 3), end_range: Date.new(2015, 2, 1),
|
|
147
|
+
periods: %i[quarterly monthly]}
|
|
148
|
+
end
|
|
149
|
+
|
|
150
|
+
before do
|
|
151
|
+
@validator = PeriodsValidator::Periods.new(option_invalid)
|
|
152
|
+
@validator.valid?
|
|
153
|
+
end
|
|
154
|
+
|
|
155
|
+
it 'shows errors' do
|
|
156
|
+
expect(@validator.error).to eq(:invalid_period)
|
|
157
|
+
end
|
|
158
|
+
end
|
|
159
|
+
end
|
|
160
|
+
end
|
|
161
|
+
|
|
162
|
+
context 'when period defined in periods is invalid' do
|
|
163
|
+
context 'when periods not exists' do
|
|
164
|
+
before do
|
|
165
|
+
option_invalid = {start_range: Date.new(2015, 1, 1), end_range: Date.new(2015, 1, 31),
|
|
166
|
+
periods: %i[invalid]}
|
|
167
|
+
|
|
168
|
+
@validator = PeriodsValidator::Periods.new(option_invalid)
|
|
169
|
+
@validator.valid?
|
|
170
|
+
end
|
|
171
|
+
|
|
172
|
+
it 'shows errors' do
|
|
173
|
+
expect(@validator.error).to eq(:invalid_define_period)
|
|
174
|
+
end
|
|
175
|
+
end
|
|
176
|
+
|
|
177
|
+
context 'when periods is empty' do
|
|
178
|
+
before do
|
|
179
|
+
option_invalid = {start_range: Date.new(2015, 1, 1), end_range: Date.new(2015, 1, 31),
|
|
180
|
+
periods: %i[]}
|
|
181
|
+
|
|
182
|
+
@validator = PeriodsValidator::Periods.new(option_invalid)
|
|
183
|
+
@validator.valid?
|
|
184
|
+
end
|
|
185
|
+
|
|
186
|
+
it 'shows errors' do
|
|
187
|
+
expect(@validator.error).to eq(:invalid_define_period)
|
|
188
|
+
end
|
|
189
|
+
end
|
|
190
|
+
end
|
|
191
|
+
end
|
data/spec/spec_helper.rb
ADDED
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
ENV['RAILS_ENV'] ||= 'test'
|
|
2
|
+
|
|
3
|
+
require 'bundler/setup'
|
|
4
|
+
|
|
5
|
+
require 'periods_validator/periods'
|
|
6
|
+
|
|
7
|
+
require 'active_support'
|
|
8
|
+
require 'active_support/core_ext'
|
|
9
|
+
|
|
10
|
+
RSpec.configure do |config|
|
|
11
|
+
# rspec-expectations config goes here. You can use an alternate
|
|
12
|
+
# assertion/expectation library such as wrong or the stdlib/minitest
|
|
13
|
+
# assertions if you prefer.
|
|
14
|
+
config.expect_with :rspec do |expectations|
|
|
15
|
+
# This option will default to `true` in RSpec 4. It makes the `description`
|
|
16
|
+
# and `failure_message` of custom matchers include text for helper methods
|
|
17
|
+
# defined using `chain`, e.g.:
|
|
18
|
+
# be_bigger_than(2).and_smaller_than(4).description
|
|
19
|
+
# # => "be bigger than 2 and smaller than 4"
|
|
20
|
+
# ...rather than:
|
|
21
|
+
# # => "be bigger than 2"
|
|
22
|
+
expectations.include_chain_clauses_in_custom_matcher_descriptions = true
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
# rspec-mocks config goes here. You can use an alternate test double
|
|
26
|
+
# library (such as bogus or mocha) by changing the `mock_with` option here.
|
|
27
|
+
config.mock_with :rspec do |mocks|
|
|
28
|
+
# Prevents you from mocking or stubbing a method that does not exist on
|
|
29
|
+
# a real object. This is generally recommended, and will default to
|
|
30
|
+
# `true` in RSpec 4.
|
|
31
|
+
mocks.verify_partial_doubles = true
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
# This option will default to `:apply_to_host_groups` in RSpec 4 (and will
|
|
35
|
+
# have no way to turn it off -- the option exists only for backwards
|
|
36
|
+
# compatibility in RSpec 3). It causes shared context metadata to be
|
|
37
|
+
# inherited by the metadata hash of host groups and examples, rather than
|
|
38
|
+
# triggering implicit auto-inclusion in groups with matching metadata.
|
|
39
|
+
config.shared_context_metadata_behavior = :apply_to_host_groups
|
|
40
|
+
|
|
41
|
+
# This allows you to limit a spec run to individual examples or groups
|
|
42
|
+
# you care about by tagging them with `:focus` metadata. When nothing
|
|
43
|
+
# is tagged with `:focus`, all examples get run. RSpec also provides
|
|
44
|
+
# aliases for `it`, `describe`, and `context` that include `:focus`
|
|
45
|
+
# metadata: `fit`, `fdescribe` and `fcontext`, respectively.
|
|
46
|
+
config.filter_run_when_matching :focus
|
|
47
|
+
# Allows RSpec to persist some state between runs in order to support
|
|
48
|
+
# the `--only-failures` and `--next-failure` CLI options. We recommend
|
|
49
|
+
# you configure your source control system to ignore this file.
|
|
50
|
+
# config.example_status_persistence_file_path = 'spec/examples.txt'
|
|
51
|
+
# Limits the available syntax to the non-monkey patched syntax that is
|
|
52
|
+
# recommended. For more details, see:
|
|
53
|
+
# - http://rspec.info/blog/2012/06/rspecs-new-expectation-syntax/
|
|
54
|
+
# - http://www.teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/
|
|
55
|
+
# - http://rspec.info/blog/2014/05/notable-changes-in-rspec-3/#zero-monkey-patching-mode
|
|
56
|
+
config.disable_monkey_patching!
|
|
57
|
+
# Many RSpec users commonly either run the entire suite or an individual
|
|
58
|
+
# file, and it's useful to allow more verbose output when running an
|
|
59
|
+
# individual spec file.
|
|
60
|
+
if config.files_to_run.one?
|
|
61
|
+
# Use the documentation formatter for detailed output,
|
|
62
|
+
# unless a formatter has already been configured
|
|
63
|
+
# (e.g. via a command-line flag).
|
|
64
|
+
config.default_formatter = 'doc'
|
|
65
|
+
end
|
|
66
|
+
# Print the 10 slowest examples and example groups at the
|
|
67
|
+
# end of the spec run, to help surface which specs are running
|
|
68
|
+
# particularly slow.
|
|
69
|
+
config.profile_examples = 10
|
|
70
|
+
# Run specs in random order to surface order dependencies. If you find an
|
|
71
|
+
# order dependency and want to debug it, you can fix the order by providing
|
|
72
|
+
# the seed, which is printed after each run.
|
|
73
|
+
# --seed 1234
|
|
74
|
+
config.order = :random
|
|
75
|
+
# Seed global randomization in this process using the `--seed` CLI option.
|
|
76
|
+
# Setting this allows you to use `--seed` to deterministically reproduce
|
|
77
|
+
# test failures related to randomization by passing the same `--seed` value
|
|
78
|
+
# as the one that triggered the failure.
|
|
79
|
+
Kernel.srand config.seed
|
|
80
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: periods_validator
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.1.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Javier Ponferrada López
|
|
8
|
+
autorequire:
|
|
9
|
+
bindir: bin
|
|
10
|
+
cert_chain: []
|
|
11
|
+
date: 2019-07-02 00:00:00.000000000 Z
|
|
12
|
+
dependencies:
|
|
13
|
+
- !ruby/object:Gem::Dependency
|
|
14
|
+
name: activesupport
|
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
|
16
|
+
requirements:
|
|
17
|
+
- - ">="
|
|
18
|
+
- !ruby/object:Gem::Version
|
|
19
|
+
version: '0'
|
|
20
|
+
type: :development
|
|
21
|
+
prerelease: false
|
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
23
|
+
requirements:
|
|
24
|
+
- - ">="
|
|
25
|
+
- !ruby/object:Gem::Version
|
|
26
|
+
version: '0'
|
|
27
|
+
- !ruby/object:Gem::Dependency
|
|
28
|
+
name: appraisal
|
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
|
30
|
+
requirements:
|
|
31
|
+
- - ">="
|
|
32
|
+
- !ruby/object:Gem::Version
|
|
33
|
+
version: '0'
|
|
34
|
+
type: :development
|
|
35
|
+
prerelease: false
|
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
37
|
+
requirements:
|
|
38
|
+
- - ">="
|
|
39
|
+
- !ruby/object:Gem::Version
|
|
40
|
+
version: '0'
|
|
41
|
+
- !ruby/object:Gem::Dependency
|
|
42
|
+
name: bundler
|
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
|
44
|
+
requirements:
|
|
45
|
+
- - "~>"
|
|
46
|
+
- !ruby/object:Gem::Version
|
|
47
|
+
version: '1.5'
|
|
48
|
+
type: :development
|
|
49
|
+
prerelease: false
|
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
51
|
+
requirements:
|
|
52
|
+
- - "~>"
|
|
53
|
+
- !ruby/object:Gem::Version
|
|
54
|
+
version: '1.5'
|
|
55
|
+
- !ruby/object:Gem::Dependency
|
|
56
|
+
name: pry-byebug
|
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
|
58
|
+
requirements:
|
|
59
|
+
- - ">="
|
|
60
|
+
- !ruby/object:Gem::Version
|
|
61
|
+
version: '0'
|
|
62
|
+
type: :development
|
|
63
|
+
prerelease: false
|
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
65
|
+
requirements:
|
|
66
|
+
- - ">="
|
|
67
|
+
- !ruby/object:Gem::Version
|
|
68
|
+
version: '0'
|
|
69
|
+
- !ruby/object:Gem::Dependency
|
|
70
|
+
name: rspec
|
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
|
72
|
+
requirements:
|
|
73
|
+
- - ">="
|
|
74
|
+
- !ruby/object:Gem::Version
|
|
75
|
+
version: '0'
|
|
76
|
+
type: :development
|
|
77
|
+
prerelease: false
|
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
79
|
+
requirements:
|
|
80
|
+
- - ">="
|
|
81
|
+
- !ruby/object:Gem::Version
|
|
82
|
+
version: '0'
|
|
83
|
+
description: Validates periods between two date ranges
|
|
84
|
+
email:
|
|
85
|
+
- jponferrada26@gmail.com
|
|
86
|
+
executables: []
|
|
87
|
+
extensions: []
|
|
88
|
+
extra_rdoc_files: []
|
|
89
|
+
files:
|
|
90
|
+
- ".gitignore"
|
|
91
|
+
- ".travis.yml"
|
|
92
|
+
- Appraisals
|
|
93
|
+
- Gemfile
|
|
94
|
+
- Gemfile.lock
|
|
95
|
+
- LICENSE
|
|
96
|
+
- LICENSE.txt
|
|
97
|
+
- README.md
|
|
98
|
+
- Rakefile
|
|
99
|
+
- lib/periods_validator.rb
|
|
100
|
+
- lib/periods_validator/helper_methods.rb
|
|
101
|
+
- lib/periods_validator/periods.rb
|
|
102
|
+
- lib/periods_validator/validator.rb
|
|
103
|
+
- lib/periods_validator/version.rb
|
|
104
|
+
- periods_validator.gemspec
|
|
105
|
+
- spec/periods_validator/periods_spec.rb
|
|
106
|
+
- spec/spec_helper.rb
|
|
107
|
+
homepage: ''
|
|
108
|
+
licenses:
|
|
109
|
+
- MIT
|
|
110
|
+
metadata: {}
|
|
111
|
+
post_install_message:
|
|
112
|
+
rdoc_options: []
|
|
113
|
+
require_paths:
|
|
114
|
+
- lib
|
|
115
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
116
|
+
requirements:
|
|
117
|
+
- - ">="
|
|
118
|
+
- !ruby/object:Gem::Version
|
|
119
|
+
version: '0'
|
|
120
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
121
|
+
requirements:
|
|
122
|
+
- - ">="
|
|
123
|
+
- !ruby/object:Gem::Version
|
|
124
|
+
version: '0'
|
|
125
|
+
requirements: []
|
|
126
|
+
rubyforge_project:
|
|
127
|
+
rubygems_version: 2.7.6
|
|
128
|
+
signing_key:
|
|
129
|
+
specification_version: 4
|
|
130
|
+
summary: Validates periods between two date ranges
|
|
131
|
+
test_files:
|
|
132
|
+
- spec/periods_validator/periods_spec.rb
|
|
133
|
+
- spec/spec_helper.rb
|