date_interval 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +17 -0
- data/.rspec +2 -0
- data/.travis.yml +5 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +51 -0
- data/Rakefile +6 -0
- data/date_interval.gemspec +22 -0
- data/lib/date_interval.rb +32 -0
- data/lib/date_interval/date.rb +37 -0
- data/lib/date_interval/filter.rb +15 -0
- data/lib/date_interval/filter/date.rb +22 -0
- data/lib/date_interval/filter/none.rb +9 -0
- data/lib/date_interval/filter/operator.rb +17 -0
- data/lib/date_interval/filter/weekday.rb +19 -0
- data/lib/date_interval/filter/weekdays.rb +15 -0
- data/lib/date_interval/filter/weekend.rb +15 -0
- data/lib/date_interval/parser.rb +90 -0
- data/lib/date_interval/transformer.rb +27 -0
- data/lib/date_interval/version.rb +3 -0
- data/spec/date_interval_spec.rb +169 -0
- data/spec/spec_helper.rb +16 -0
- metadata +138 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 813d0b8efd859f424896abe5f481d49f4b8c6f52
|
4
|
+
data.tar.gz: b7e075815608d25339f44a5e7e85415081cfecee
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 778f7247a5b7a65096b959aa518bad9b176810732dbfaf7e84029b04c57ddff6c416cef3b10bd1618ee70f71790e4eafdee49e464720b14b4e8d9da3ef67da03
|
7
|
+
data.tar.gz: 5983f419c51d80b2769b3e9369811b2c7e898b49cb1a86a0ba7d4bcb436bdb2a0b803c7a26f8d8f77e203773f7337e09b9d68889f27adc54446189080f767d65
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 Nando Vieira
|
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,51 @@
|
|
1
|
+
# DateInterval
|
2
|
+
|
3
|
+
Parse date intervals from strings.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'date_interval'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install date_interval
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
You must always define at least one range at the beginning of the expression. The snippet below will return three dates objects.
|
22
|
+
|
23
|
+
```ruby
|
24
|
+
expr = "2014-01-01 - 2014-01-03"
|
25
|
+
dates = DateInterval.parse(expr)
|
26
|
+
```
|
27
|
+
|
28
|
+
You can provide as many intervals as you want. The following expression returns six date objects.
|
29
|
+
|
30
|
+
```ruby
|
31
|
+
expr = "2014-01-01 - 2014-01-03, 2014-02-01 - 2014-02-03"
|
32
|
+
dates = DateInterval.parse(expr)
|
33
|
+
```
|
34
|
+
|
35
|
+
You can also define filters. Filters are applied in sequence, from left to right. The following filters are available:
|
36
|
+
|
37
|
+
- `none`: return no dates. Useful for applying specific filters afterwards.
|
38
|
+
- `[+-]weekends`: filter weekend dates
|
39
|
+
- `[+-]weekdays`: filter weekdays ()
|
40
|
+
- `[+-]sundays`: filter sundays. You can use any weekday name (sundays-saturdays)
|
41
|
+
- `[+-]yyy-mm-dd`: add/remove the given date.
|
42
|
+
|
43
|
+
Beware that duplicated dates are removed from the final result. They're also sorted.
|
44
|
+
|
45
|
+
## Contributing
|
46
|
+
|
47
|
+
1. Fork it ( http://github.com/[my-github-username]/date_interval/fork )
|
48
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
49
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
50
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
51
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
require "./lib/date_interval/version"
|
2
|
+
|
3
|
+
Gem::Specification.new do |spec|
|
4
|
+
spec.name = "date_interval"
|
5
|
+
spec.version = DateInterval::VERSION
|
6
|
+
spec.authors = ["Nando Vieira"]
|
7
|
+
spec.email = ["fnando.vieira@gmail.com"]
|
8
|
+
spec.summary = "Parse date intervals from strings"
|
9
|
+
spec.homepage = "http://github.com/fnando/date_interval"
|
10
|
+
spec.license = "MIT"
|
11
|
+
|
12
|
+
spec.files = `git ls-files -z`.split("\x0")
|
13
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
14
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
15
|
+
spec.require_paths = ["lib"]
|
16
|
+
|
17
|
+
spec.add_dependency "parslet"
|
18
|
+
spec.add_development_dependency "bundler", ">= 0"
|
19
|
+
spec.add_development_dependency "rake"
|
20
|
+
spec.add_development_dependency "rspec"
|
21
|
+
spec.add_development_dependency "pry-meta"
|
22
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
require "date"
|
2
|
+
require "parslet"
|
3
|
+
require "forwardable"
|
4
|
+
|
5
|
+
require "date_interval/date"
|
6
|
+
require "date_interval/filter"
|
7
|
+
require "date_interval/filter/operator"
|
8
|
+
require "date_interval/filter/date"
|
9
|
+
require "date_interval/filter/none"
|
10
|
+
require "date_interval/filter/weekday"
|
11
|
+
require "date_interval/filter/weekdays"
|
12
|
+
require "date_interval/filter/weekend"
|
13
|
+
require "date_interval/parser"
|
14
|
+
require "date_interval/transformer"
|
15
|
+
require "date_interval/version"
|
16
|
+
|
17
|
+
module DateInterval
|
18
|
+
InvalidRuleError = Class.new(StandardError)
|
19
|
+
|
20
|
+
def self.parse(expression)
|
21
|
+
parser = Parser.new
|
22
|
+
transformer = Transformer.new
|
23
|
+
|
24
|
+
tree = parser.parse(expression)
|
25
|
+
ast = transformer.apply(tree)
|
26
|
+
|
27
|
+
Filter.filter(
|
28
|
+
ast[:intervals].flatten.uniq,
|
29
|
+
ast[:filters].kind_of?(Array) ? ast[:filters] : []
|
30
|
+
)
|
31
|
+
end
|
32
|
+
end
|
@@ -0,0 +1,37 @@
|
|
1
|
+
module DateInterval
|
2
|
+
class Date
|
3
|
+
attr_reader :date
|
4
|
+
|
5
|
+
extend Forwardable
|
6
|
+
def_delegator :date, :wday
|
7
|
+
|
8
|
+
def initialize(date)
|
9
|
+
@date = date
|
10
|
+
@add = true
|
11
|
+
end
|
12
|
+
|
13
|
+
def add?
|
14
|
+
@add
|
15
|
+
end
|
16
|
+
|
17
|
+
def add!
|
18
|
+
@add = true
|
19
|
+
end
|
20
|
+
|
21
|
+
def remove!
|
22
|
+
@add = false
|
23
|
+
end
|
24
|
+
|
25
|
+
def weekend?
|
26
|
+
[0, 6].include?(date.wday)
|
27
|
+
end
|
28
|
+
|
29
|
+
def weekday?
|
30
|
+
!weekend?
|
31
|
+
end
|
32
|
+
|
33
|
+
def to_date
|
34
|
+
date
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
module DateInterval
|
2
|
+
module Filter
|
3
|
+
class Date < Operator
|
4
|
+
attr_reader :date, :operator
|
5
|
+
|
6
|
+
def initialize(date, operator)
|
7
|
+
@date = date
|
8
|
+
@operator = operator
|
9
|
+
end
|
10
|
+
|
11
|
+
def positive(dates)
|
12
|
+
dates << DateInterval::Date.new(date)
|
13
|
+
end
|
14
|
+
|
15
|
+
def negative(dates)
|
16
|
+
dates
|
17
|
+
.select {|d| d.to_date == date }
|
18
|
+
.each(&:remove!)
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
module DateInterval
|
2
|
+
module Filter
|
3
|
+
class Operator
|
4
|
+
def apply(dates)
|
5
|
+
operator == "-" ? negative(dates) : positive(dates)
|
6
|
+
end
|
7
|
+
|
8
|
+
def negative(dates)
|
9
|
+
filter(dates).each(&:remove!)
|
10
|
+
end
|
11
|
+
|
12
|
+
def positive(dates)
|
13
|
+
filter(dates).each(&:add!)
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
module DateInterval
|
2
|
+
module Filter
|
3
|
+
class Weekday < Operator
|
4
|
+
attr_reader :day, :operator, :wday
|
5
|
+
|
6
|
+
WEEKDAYS = %w[sunday monday tuesday wednesday thursday friday saturday]
|
7
|
+
|
8
|
+
def initialize(day, operator)
|
9
|
+
@day = day
|
10
|
+
@wday = WEEKDAYS.index(day)
|
11
|
+
@operator = operator
|
12
|
+
end
|
13
|
+
|
14
|
+
def filter(dates)
|
15
|
+
dates.select {|date| date.wday == wday }
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,90 @@
|
|
1
|
+
module DateInterval
|
2
|
+
class Parser < Parslet::Parser
|
3
|
+
rule(:date) { year >> hyphen >> month >> hyphen >> day }
|
4
|
+
|
5
|
+
rule(:year) {
|
6
|
+
match("[12]") >> match("[0-9]").repeat(3)
|
7
|
+
}
|
8
|
+
|
9
|
+
rule(:month) {
|
10
|
+
str("0") >> match("[1-9]") |
|
11
|
+
str("1") >> match("[0-2]")
|
12
|
+
}
|
13
|
+
|
14
|
+
rule(:day) {
|
15
|
+
str("0") >> match("[1-9]") |
|
16
|
+
match("[12]") >> match("[0-9]") |
|
17
|
+
str("3") >> match("[01]")
|
18
|
+
}
|
19
|
+
|
20
|
+
rule(:comma) { str(",") >> space? }
|
21
|
+
rule(:hyphen) { str("-") }
|
22
|
+
rule(:hyphen_with_space) { space? >> hyphen >> space? }
|
23
|
+
|
24
|
+
rule(:space) { match(" ").repeat(1) }
|
25
|
+
rule(:space?) { space.maybe }
|
26
|
+
|
27
|
+
rule(:interval) {
|
28
|
+
(date.as(:starting) >> hyphen_with_space >> date.as(:ending)).as(:interval)
|
29
|
+
}
|
30
|
+
|
31
|
+
rule(:intervals) {
|
32
|
+
interval >>
|
33
|
+
((comma >> interval).repeat).maybe
|
34
|
+
}
|
35
|
+
|
36
|
+
rule(:filters) { (comma >> filter).repeat }
|
37
|
+
rule(:filters?) { filters.maybe }
|
38
|
+
rule(:filter) {
|
39
|
+
filter_weekday.as(:weekday) |
|
40
|
+
filter_weekdays.as(:weekdays) |
|
41
|
+
filter_weekend.as(:weekend) |
|
42
|
+
filter_none.as(:none) |
|
43
|
+
filter_date.as(:date)
|
44
|
+
}
|
45
|
+
|
46
|
+
rule(:s?) { str("s").maybe }
|
47
|
+
|
48
|
+
rule(:filter_weekend) {
|
49
|
+
operator? >>
|
50
|
+
str("weekend") >> s?
|
51
|
+
}
|
52
|
+
|
53
|
+
rule(:filter_weekdays) {
|
54
|
+
operator? >>
|
55
|
+
str("weekday") >> s?
|
56
|
+
}
|
57
|
+
|
58
|
+
rule(:filter_weekday) {
|
59
|
+
operator? >>
|
60
|
+
(
|
61
|
+
str("sunday") |
|
62
|
+
str("monday") |
|
63
|
+
str("tuesday") |
|
64
|
+
str("wednesday") |
|
65
|
+
str("thursday") |
|
66
|
+
str("friday") |
|
67
|
+
str("saturday")
|
68
|
+
).as(:day) >> s?
|
69
|
+
}
|
70
|
+
|
71
|
+
rule(:filter_none) {
|
72
|
+
str("none")
|
73
|
+
}
|
74
|
+
|
75
|
+
rule(:filter_date) {
|
76
|
+
operator? >>
|
77
|
+
date.as(:date)
|
78
|
+
}
|
79
|
+
|
80
|
+
rule(:operator) { match("[+-]").as(:operator) }
|
81
|
+
rule(:operator?) { operator.maybe }
|
82
|
+
|
83
|
+
rule(:expression) {
|
84
|
+
intervals.as(:intervals) >>
|
85
|
+
filters?.as(:filters)
|
86
|
+
}
|
87
|
+
|
88
|
+
root :expression
|
89
|
+
end
|
90
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
module DateInterval
|
2
|
+
class Transformer < Parslet::Transform
|
3
|
+
rule(interval: {starting: simple(:starting), ending: simple(:ending)}) {
|
4
|
+
(::Date.parse(starting)..::Date.parse(ending)).map {|date| Date.new(date) }
|
5
|
+
}
|
6
|
+
|
7
|
+
rule(weekday: {operator: simple(:operator), day: simple(:day)}) {
|
8
|
+
Filter::Weekday.new(day.to_s, operator.to_s)
|
9
|
+
}
|
10
|
+
|
11
|
+
rule(date: {operator: simple(:operator), date: simple(:date)}) {
|
12
|
+
Filter::Date.new(::Date.parse(date), operator.to_s)
|
13
|
+
}
|
14
|
+
|
15
|
+
rule(weekend: {operator: simple(:operator)}) {
|
16
|
+
Filter::Weekend.new(operator.to_s)
|
17
|
+
}
|
18
|
+
|
19
|
+
rule(weekdays: {operator: simple(:operator)}) {
|
20
|
+
Filter::Weekdays.new(operator.to_s)
|
21
|
+
}
|
22
|
+
|
23
|
+
rule(none: simple(:name)) {
|
24
|
+
Filter::None.new
|
25
|
+
}
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1,169 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
describe DateInterval do
|
4
|
+
it "parses the interval" do
|
5
|
+
interval = DateInterval.parse("2014-01-01 - 2014-01-05")
|
6
|
+
expected_interval = (Date.parse("2014-01-01")..Date.parse("2014-01-05")).to_a
|
7
|
+
|
8
|
+
expect(interval).to eql(expected_interval)
|
9
|
+
end
|
10
|
+
|
11
|
+
it "filters all out" do
|
12
|
+
interval = DateInterval.parse("2014-01-01 - 2014-01-05, none")
|
13
|
+
expect(interval).to eql([])
|
14
|
+
end
|
15
|
+
|
16
|
+
it "includes weekends" do
|
17
|
+
interval = DateInterval.parse("2014-01-01 - 2014-01-05, none, +weekends")
|
18
|
+
expected_interval = date_range("2014-01-04", "2014-01-05")
|
19
|
+
|
20
|
+
expect(interval).to eql(expected_interval)
|
21
|
+
end
|
22
|
+
|
23
|
+
it "excludes weekends" do
|
24
|
+
interval = DateInterval.parse("2014-01-01 - 2014-01-05, -weekends")
|
25
|
+
expected_interval = date_range("2014-01-01", "2014-01-03")
|
26
|
+
|
27
|
+
expect(interval).to eql(expected_interval)
|
28
|
+
end
|
29
|
+
|
30
|
+
it "includes weekdays" do
|
31
|
+
interval = DateInterval.parse("2014-01-01 - 2014-01-05, none, +weekdays")
|
32
|
+
expected_interval = date_range("2014-01-01", "2014-01-03")
|
33
|
+
|
34
|
+
expect(interval).to eql(expected_interval)
|
35
|
+
end
|
36
|
+
|
37
|
+
it "excludes weekdays" do
|
38
|
+
interval = DateInterval.parse("2014-01-01 - 2014-01-05, -weekdays")
|
39
|
+
expected_interval = date_range("2014-01-04", "2014-01-05")
|
40
|
+
|
41
|
+
expect(interval).to eql(expected_interval)
|
42
|
+
end
|
43
|
+
|
44
|
+
it "includes date" do
|
45
|
+
interval = DateInterval.parse("2014-01-01 - 2014-01-05, +2014-01-06")
|
46
|
+
expected_interval = date_range("2014-01-01", "2014-01-06")
|
47
|
+
|
48
|
+
expect(interval).to eql(expected_interval)
|
49
|
+
end
|
50
|
+
|
51
|
+
it "excludes date" do
|
52
|
+
interval = DateInterval.parse("2014-01-01 - 2014-01-05, -2014-01-05")
|
53
|
+
expected_interval = date_range("2014-01-01", "2014-01-04")
|
54
|
+
|
55
|
+
expect(interval).to eql(expected_interval)
|
56
|
+
end
|
57
|
+
|
58
|
+
it "includes sunday" do
|
59
|
+
interval = DateInterval.parse("2014-01-01 - 2014-01-05, none, +sunday")
|
60
|
+
expected_interval = [to_date("2014-01-05")]
|
61
|
+
|
62
|
+
expect(interval).to eql(expected_interval)
|
63
|
+
end
|
64
|
+
|
65
|
+
it "excludes sunday" do
|
66
|
+
interval = DateInterval.parse("2014-01-01 - 2014-01-05, -sunday")
|
67
|
+
expected_interval = date_range("2014-01-01", "2014-01-04")
|
68
|
+
|
69
|
+
expect(interval).to eql(expected_interval)
|
70
|
+
end
|
71
|
+
|
72
|
+
it "includes monday" do
|
73
|
+
interval = DateInterval.parse("2014-01-01 - 2014-01-06, none, +monday")
|
74
|
+
expected_interval = [to_date("2014-01-06")]
|
75
|
+
|
76
|
+
expect(interval).to eql(expected_interval)
|
77
|
+
end
|
78
|
+
|
79
|
+
it "excludes monday" do
|
80
|
+
interval = DateInterval.parse("2014-01-01 - 2014-01-06, -monday")
|
81
|
+
expected_interval = date_range("2014-01-01", "2014-01-05")
|
82
|
+
|
83
|
+
expect(interval).to eql(expected_interval)
|
84
|
+
end
|
85
|
+
|
86
|
+
it "includes tuesday" do
|
87
|
+
interval = DateInterval.parse("2014-01-01 - 2014-01-07, none, +tuesday")
|
88
|
+
expected_interval = [to_date("2014-01-07")]
|
89
|
+
|
90
|
+
expect(interval).to eql(expected_interval)
|
91
|
+
end
|
92
|
+
|
93
|
+
it "excludes tuesday" do
|
94
|
+
interval = DateInterval.parse("2014-01-01 - 2014-01-07, -tuesday")
|
95
|
+
expected_interval = date_range("2014-01-01", "2014-01-06")
|
96
|
+
|
97
|
+
expect(interval).to eql(expected_interval)
|
98
|
+
end
|
99
|
+
|
100
|
+
it "includes wednesday" do
|
101
|
+
interval = DateInterval.parse("2014-01-01 - 2014-01-05, none, +wednesday")
|
102
|
+
expected_interval = [to_date("2014-01-01")]
|
103
|
+
|
104
|
+
expect(interval).to eql(expected_interval)
|
105
|
+
end
|
106
|
+
|
107
|
+
it "excludes wednesday" do
|
108
|
+
interval = DateInterval.parse("2014-01-01 - 2014-01-05, -wednesday")
|
109
|
+
expected_interval = date_range("2014-01-02", "2014-01-05")
|
110
|
+
|
111
|
+
expect(interval).to eql(expected_interval)
|
112
|
+
end
|
113
|
+
|
114
|
+
it "includes thursday" do
|
115
|
+
interval = DateInterval.parse("2014-01-01 - 2014-01-05, none, +thursday")
|
116
|
+
expected_interval = [to_date("2014-01-02")]
|
117
|
+
|
118
|
+
expect(interval).to eql(expected_interval)
|
119
|
+
end
|
120
|
+
|
121
|
+
it "excludes thursday" do
|
122
|
+
interval = DateInterval.parse("2014-01-02 - 2014-01-05, -thursday")
|
123
|
+
expected_interval = date_range("2014-01-03", "2014-01-05")
|
124
|
+
|
125
|
+
expect(interval).to eql(expected_interval)
|
126
|
+
end
|
127
|
+
|
128
|
+
it "includes friday" do
|
129
|
+
interval = DateInterval.parse("2014-01-01 - 2014-01-05, none, +friday")
|
130
|
+
expected_interval = [to_date("2014-01-03")]
|
131
|
+
|
132
|
+
expect(interval).to eql(expected_interval)
|
133
|
+
end
|
134
|
+
|
135
|
+
it "excludes friday" do
|
136
|
+
interval = DateInterval.parse("2014-01-03 - 2014-01-05, -friday")
|
137
|
+
expected_interval = date_range("2014-01-04", "2014-01-05")
|
138
|
+
|
139
|
+
expect(interval).to eql(expected_interval)
|
140
|
+
end
|
141
|
+
|
142
|
+
it "includes saturday" do
|
143
|
+
interval = DateInterval.parse("2014-01-01 - 2014-01-04, none, +saturday")
|
144
|
+
expected_interval = [to_date("2014-01-04")]
|
145
|
+
|
146
|
+
expect(interval).to eql(expected_interval)
|
147
|
+
end
|
148
|
+
|
149
|
+
it "excludes saturday" do
|
150
|
+
interval = DateInterval.parse("2014-01-01 - 2014-01-04, -saturday")
|
151
|
+
expected_interval = date_range("2014-01-01", "2014-01-03")
|
152
|
+
|
153
|
+
expect(interval).to eql(expected_interval)
|
154
|
+
end
|
155
|
+
|
156
|
+
it "returns unique dates" do
|
157
|
+
interval = DateInterval.parse("2014-01-01 - 2014-01-02, +2014-01-02")
|
158
|
+
expected_interval = date_range("2014-01-01", "2014-01-02")
|
159
|
+
|
160
|
+
expect(interval).to eql(expected_interval)
|
161
|
+
end
|
162
|
+
|
163
|
+
it "returns sorted dates" do
|
164
|
+
interval = DateInterval.parse("2014-01-01 - 2014-01-02, +2013-12-31")
|
165
|
+
expected_interval = date_range("2013-12-31", "2014-01-02")
|
166
|
+
|
167
|
+
expect(interval).to eql(expected_interval)
|
168
|
+
end
|
169
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
require "bundler/setup"
|
2
|
+
require "date_interval"
|
3
|
+
|
4
|
+
module SpecHelper
|
5
|
+
def date_range(starting, ending)
|
6
|
+
(to_date(starting)..to_date(ending)).to_a
|
7
|
+
end
|
8
|
+
|
9
|
+
def to_date(literal)
|
10
|
+
Date.parse(literal)
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
RSpec.configure do |config|
|
15
|
+
config.include SpecHelper
|
16
|
+
end
|
metadata
ADDED
@@ -0,0 +1,138 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: date_interval
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Nando Vieira
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-01-30 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: parslet
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
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: bundler
|
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: rake
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rspec
|
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: pry-meta
|
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:
|
84
|
+
email:
|
85
|
+
- fnando.vieira@gmail.com
|
86
|
+
executables: []
|
87
|
+
extensions: []
|
88
|
+
extra_rdoc_files: []
|
89
|
+
files:
|
90
|
+
- ".gitignore"
|
91
|
+
- ".rspec"
|
92
|
+
- ".travis.yml"
|
93
|
+
- Gemfile
|
94
|
+
- LICENSE.txt
|
95
|
+
- README.md
|
96
|
+
- Rakefile
|
97
|
+
- date_interval.gemspec
|
98
|
+
- lib/date_interval.rb
|
99
|
+
- lib/date_interval/date.rb
|
100
|
+
- lib/date_interval/filter.rb
|
101
|
+
- lib/date_interval/filter/date.rb
|
102
|
+
- lib/date_interval/filter/none.rb
|
103
|
+
- lib/date_interval/filter/operator.rb
|
104
|
+
- lib/date_interval/filter/weekday.rb
|
105
|
+
- lib/date_interval/filter/weekdays.rb
|
106
|
+
- lib/date_interval/filter/weekend.rb
|
107
|
+
- lib/date_interval/parser.rb
|
108
|
+
- lib/date_interval/transformer.rb
|
109
|
+
- lib/date_interval/version.rb
|
110
|
+
- spec/date_interval_spec.rb
|
111
|
+
- spec/spec_helper.rb
|
112
|
+
homepage: http://github.com/fnando/date_interval
|
113
|
+
licenses:
|
114
|
+
- MIT
|
115
|
+
metadata: {}
|
116
|
+
post_install_message:
|
117
|
+
rdoc_options: []
|
118
|
+
require_paths:
|
119
|
+
- lib
|
120
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
121
|
+
requirements:
|
122
|
+
- - ">="
|
123
|
+
- !ruby/object:Gem::Version
|
124
|
+
version: '0'
|
125
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
126
|
+
requirements:
|
127
|
+
- - ">="
|
128
|
+
- !ruby/object:Gem::Version
|
129
|
+
version: '0'
|
130
|
+
requirements: []
|
131
|
+
rubyforge_project:
|
132
|
+
rubygems_version: 2.2.0
|
133
|
+
signing_key:
|
134
|
+
specification_version: 4
|
135
|
+
summary: Parse date intervals from strings
|
136
|
+
test_files:
|
137
|
+
- spec/date_interval_spec.rb
|
138
|
+
- spec/spec_helper.rb
|