date_interval 0.0.1 → 0.0.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 813d0b8efd859f424896abe5f481d49f4b8c6f52
4
- data.tar.gz: b7e075815608d25339f44a5e7e85415081cfecee
3
+ metadata.gz: 50fe6f00cdc14871f25bf3ebd95be0fd146b8403
4
+ data.tar.gz: c63993f601f26eeddaf989e72a27cad8f77a5910
5
5
  SHA512:
6
- metadata.gz: 778f7247a5b7a65096b959aa518bad9b176810732dbfaf7e84029b04c57ddff6c416cef3b10bd1618ee70f71790e4eafdee49e464720b14b4e8d9da3ef67da03
7
- data.tar.gz: 5983f419c51d80b2769b3e9369811b2c7e898b49cb1a86a0ba7d4bcb436bdb2a0b803c7a26f8d8f77e203773f7337e09b9d68889f27adc54446189080f767d65
6
+ metadata.gz: f9f1302693ed944c6c3c6d043d56ce6813878e8b1a45309df1dc11b083d7673a824cb7a73aad80b64337d158fff353f3241b8447161cf1eef39bf7c14e054e78
7
+ data.tar.gz: 4a9acc94cd65932d63f8769bc8c838e62b4800f5f1a16c715fd4a80aec9fbeb772d2cb5d34d6149ea4c03ea366a185298651b2d0c9de9c55e4e3d66b76850bc4
data/README.md CHANGED
@@ -1,5 +1,8 @@
1
1
  # DateInterval
2
2
 
3
+ [![Build Status](https://travis-ci.org/fnando/date_interval.png?branch=master)](https://travis-ci.org/fnando/date_interval)
4
+ [![Code Climate](https://codeclimate.com/github/fnando/date_interval.png)](https://codeclimate.com/github/fnando/date_interval)
5
+
3
6
  Parse date intervals from strings.
4
7
 
5
8
  ## Installation
@@ -21,6 +24,8 @@ Or install it yourself as:
21
24
  You must always define at least one range at the beginning of the expression. The snippet below will return three dates objects.
22
25
 
23
26
  ```ruby
27
+ require "date_interval"
28
+
24
29
  expr = "2014-01-01 - 2014-01-03"
25
30
  dates = DateInterval.parse(expr)
26
31
  ```
@@ -28,6 +33,8 @@ dates = DateInterval.parse(expr)
28
33
  You can provide as many intervals as you want. The following expression returns six date objects.
29
34
 
30
35
  ```ruby
36
+ require "date_interval"
37
+
31
38
  expr = "2014-01-01 - 2014-01-03, 2014-02-01 - 2014-02-03"
32
39
  dates = DateInterval.parse(expr)
33
40
  ```
@@ -38,10 +45,58 @@ You can also define filters. Filters are applied in sequence, from left to right
38
45
  - `[+-]weekends`: filter weekend dates
39
46
  - `[+-]weekdays`: filter weekdays ()
40
47
  - `[+-]sundays`: filter sundays. You can use any weekday name (sundays-saturdays)
48
+ - `[+-]holidays`: filter holidays. You must add the holidays by yourself (see below)
41
49
  - `[+-]yyy-mm-dd`: add/remove the given date.
42
50
 
43
51
  Beware that duplicated dates are removed from the final result. They're also sorted.
44
52
 
53
+ Some expression examples:
54
+
55
+ ```text
56
+ # Don't return weekends
57
+ 2014-01-01 - 2014-01-05, -weekends
58
+
59
+ # Return only weekends
60
+ 2014-01-01 - 2014-01-05, none, +weekends
61
+
62
+ # Don't return weekdays
63
+ 2014-01-01 - 2014-01-05, -weekdays
64
+
65
+ # Return only weekdays
66
+ 2014-01-01 - 2014-01-05, none, +weekdays
67
+
68
+ # Return mondays, wednesdays and fridays
69
+ 2014-01-01 - 2014-01-05, none, +monday, +wednesdays, +fridays
70
+
71
+ # Return the specified range, including one more date
72
+ 2014-01-01 - 2014-01-05, +2014-07-31
73
+
74
+ # Return the specified range, excluding one date
75
+ 2014-01-01 - 2014-01-05, -2014-01-05
76
+
77
+ # Return the specified range, excluding holidays
78
+ 2014-01-01 - 2014-01-05, -holidays
79
+ ```
80
+
81
+ ### Defining holidays
82
+
83
+ To define your holidays you must use the method `DateInterval::Filter::Holiday.add`. It accepts one or more dates.
84
+
85
+ ```ruby
86
+ require "date_interval"
87
+
88
+ DateInterval::Filter::Holidays.add(
89
+ Date.parse("2014-01-01"),
90
+ Date.parse("2014-12-25"),
91
+ )
92
+
93
+ DateInterval.parse("2014-01-01 - 2014-12-31, none, +holidays")
94
+ # => [
95
+ # [0] #<Date: 2014-01-01>,
96
+ # [1] #<Date: 2014-12-25>
97
+ # ]
98
+ ```
99
+
45
100
  ## Contributing
46
101
 
47
102
  1. Fork it ( http://github.com/[my-github-username]/date_interval/fork )
data/lib/date_interval.rb CHANGED
@@ -9,6 +9,7 @@ require "date_interval/filter/date"
9
9
  require "date_interval/filter/none"
10
10
  require "date_interval/filter/weekday"
11
11
  require "date_interval/filter/weekdays"
12
+ require "date_interval/filter/holidays"
12
13
  require "date_interval/filter/weekend"
13
14
  require "date_interval/parser"
14
15
  require "date_interval/transformer"
@@ -3,7 +3,7 @@ module DateInterval
3
3
  attr_reader :date
4
4
 
5
5
  extend Forwardable
6
- def_delegator :date, :wday
6
+ def_delegators :date, :wday, :strftime
7
7
 
8
8
  def initialize(date)
9
9
  @date = date
@@ -0,0 +1,23 @@
1
+ module DateInterval
2
+ module Filter
3
+ class Holidays < Operator
4
+ attr_reader :operator
5
+
6
+ def self.add(*dates)
7
+ dates.map {|date| holidays << date.strftime("%s") }
8
+ end
9
+
10
+ def self.holidays
11
+ @holidays ||= []
12
+ end
13
+
14
+ def initialize(operator)
15
+ @operator = operator
16
+ end
17
+
18
+ def filter(dates)
19
+ dates.select {|date| self.class.holidays.include?(date.strftime("%s")) }
20
+ end
21
+ end
22
+ end
23
+ end
@@ -40,12 +40,18 @@ module DateInterval
40
40
  filter_weekdays.as(:weekdays) |
41
41
  filter_weekend.as(:weekend) |
42
42
  filter_none.as(:none) |
43
- filter_date.as(:date)
43
+ filter_date.as(:date) |
44
+ filter_holidays.as(:holidays)
44
45
  }
45
46
 
46
47
  rule(:s?) { str("s").maybe }
47
48
 
48
- rule(:filter_weekend) {
49
+ rule(:filter_holidays) {
50
+ operator? >>
51
+ str("holiday") >> s?
52
+ }
53
+
54
+ rule(:filter_weekend) {
49
55
  operator? >>
50
56
  str("weekend") >> s?
51
57
  }
@@ -20,6 +20,10 @@ module DateInterval
20
20
  Filter::Weekdays.new(operator.to_s)
21
21
  }
22
22
 
23
+ rule(holidays: {operator: simple(:operator)}) {
24
+ Filter::Holidays.new(operator.to_s)
25
+ }
26
+
23
27
  rule(none: simple(:name)) {
24
28
  Filter::None.new
25
29
  }
@@ -1,3 +1,3 @@
1
1
  module DateInterval
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
@@ -3,7 +3,14 @@ require "spec_helper"
3
3
  describe DateInterval do
4
4
  it "parses the interval" do
5
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
6
+ expected_interval = date_range("2014-01-01", "2014-01-05")
7
+
8
+ expect(interval).to eql(expected_interval)
9
+ end
10
+
11
+ it "parses multiple intervals" do
12
+ interval = DateInterval.parse("2014-01-01 - 2014-01-05, 2014-01-06 - 2014-01-10")
13
+ expected_interval = date_range("2014-01-01", "2014-01-10")
7
14
 
8
15
  expect(interval).to eql(expected_interval)
9
16
  end
@@ -166,4 +173,22 @@ describe DateInterval do
166
173
 
167
174
  expect(interval).to eql(expected_interval)
168
175
  end
176
+
177
+ it "excludes holidays" do
178
+ DateInterval::Filter::Holidays.add Date.parse("2014-01-01")
179
+
180
+ interval = DateInterval.parse("2014-01-01 - 2014-01-02, -holidays")
181
+ expected_interval = [to_date("2014-01-02")]
182
+
183
+ expect(interval).to eql(expected_interval)
184
+ end
185
+
186
+ it "includes holidays" do
187
+ DateInterval::Filter::Holidays.add Date.parse("2014-01-01")
188
+
189
+ interval = DateInterval.parse("2014-01-01 - 2014-01-02, none, +holidays")
190
+ expected_interval = [to_date("2014-01-01")]
191
+
192
+ expect(interval).to eql(expected_interval)
193
+ end
169
194
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: date_interval
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nando Vieira
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-01-30 00:00:00.000000000 Z
11
+ date: 2014-01-31 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: parslet
@@ -99,6 +99,7 @@ files:
99
99
  - lib/date_interval/date.rb
100
100
  - lib/date_interval/filter.rb
101
101
  - lib/date_interval/filter/date.rb
102
+ - lib/date_interval/filter/holidays.rb
102
103
  - lib/date_interval/filter/none.rb
103
104
  - lib/date_interval/filter/operator.rb
104
105
  - lib/date_interval/filter/weekday.rb