date_iterator 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +1 -0
- data/Gemfile +2 -0
- data/Gemfile.lock +17 -0
- data/LICENSE +9 -0
- data/README.md +32 -0
- data/Rakefile +9 -0
- data/date_iterator.gemspec +21 -0
- data/lib/date_iterator.rb +53 -0
- data/lib/date_iterator/version.rb +3 -0
- data/test/date_iterator_test.rb +100 -0
- metadata +82 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 56ce56dcfc36825be40c067bbf96a19bf71f0172
|
4
|
+
data.tar.gz: a2505232b1fd0a984237c9e11bcede08395babfa
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: e969f0e08cdc3f5fef7ebd595cd7005f37a78acd91ebe9ccc177166d888b6a3e041dddbc9e46925568569824b0f6701ee0ecf4c88f45f7f1e848441c63b372fc
|
7
|
+
data.tar.gz: eea00652d8057f833ed166b2c12c0e728f9766e61e45b38eb8edf1740c7b807e4e6d6169651cda9c503eda9a5e542b1a21b3c12d55e36639118b2007b13c1427
|
data/.gitignore
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
.DS_Store
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,9 @@
|
|
1
|
+
(The MIT License)
|
2
|
+
|
3
|
+
Copyright © 2011 Jonathan Otto
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the ‘Software’), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
6
|
+
|
7
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
8
|
+
|
9
|
+
THE SOFTWARE IS PROVIDED ‘AS IS’, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTW
|
data/README.md
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
# DateIterator
|
2
|
+
---
|
3
|
+
|
4
|
+
**Iterate by day, week, or month - inclusive**
|
5
|
+
|
6
|
+
* Tested on CRuby: 2.1, 2.2 (but should work at least from 1.8.7)
|
7
|
+
* **adds 3 instance methods** to Date:
|
8
|
+
* `each_day_until`
|
9
|
+
* `each_week_until`
|
10
|
+
* `each_month_until`
|
11
|
+
* If no block is given, returns an enumerator.
|
12
|
+
|
13
|
+
## Usage
|
14
|
+
```ruby
|
15
|
+
>> Date.parse("2010-01-01").each_day_until("2010-01-05") { |d| puts d }
|
16
|
+
2010-01-01
|
17
|
+
2010-01-02
|
18
|
+
2010-01-03
|
19
|
+
2010-01-04
|
20
|
+
2010-01-05
|
21
|
+
=> nil
|
22
|
+
|
23
|
+
>> Date.parse("2010-01-01").each_week_until("2010-01-05") { |d| puts d }
|
24
|
+
2009-12-28
|
25
|
+
2010-01-04
|
26
|
+
=> nil
|
27
|
+
|
28
|
+
>> Date.parse("2010-01-01").each_month_until("2010-02-05") { |d| puts d }
|
29
|
+
2010-01-01
|
30
|
+
2010-02-01
|
31
|
+
=> nil
|
32
|
+
```
|
data/Rakefile
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'date_iterator/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = 'date_iterator'
|
8
|
+
s.version = DateIterator::VERSION
|
9
|
+
s.authors = ['Jonathan Otto']
|
10
|
+
s.email = ['jonathan.otto@gmail.com']
|
11
|
+
s.homepage = 'https://github.com/jotto/date_iterator'
|
12
|
+
s.summary = 'iterate by day, week, or month on the date class'
|
13
|
+
s.description = 'iterate by day, week, or month on the date class'
|
14
|
+
|
15
|
+
s.files = `git ls-files`.split($/)
|
16
|
+
s.test_files = s.files.grep(%r{^(test)/})
|
17
|
+
s.require_paths = ['lib']
|
18
|
+
|
19
|
+
s.add_development_dependency 'bundler', '~> 1.5'
|
20
|
+
s.add_development_dependency 'rake'
|
21
|
+
end
|
@@ -0,0 +1,53 @@
|
|
1
|
+
require 'date'
|
2
|
+
|
3
|
+
module DateIterator
|
4
|
+
|
5
|
+
# yields inclusive date object of each day
|
6
|
+
def each_day_until(_until=Date.today, &block)
|
7
|
+
return self.to_enum(:each_day_until, _until) unless block_given?
|
8
|
+
self.step(_until.is_a?(Date) ? _until : Date.parse(_until.to_s), +1, &block)
|
9
|
+
nil
|
10
|
+
end
|
11
|
+
|
12
|
+
# yields inclusive date object of first day of week, starting with mondays
|
13
|
+
def each_week_until(_until=Date.today, use_sunday_as_beg_of_week = false, &block)
|
14
|
+
return self.to_enum(:each_week_until, _until, use_sunday_as_beg_of_week) unless block_given?
|
15
|
+
_days_to_monday = self.wday == 0 ? 6 : self.wday - 1
|
16
|
+
_monday = self - _days_to_monday
|
17
|
+
_monday.step(_until.is_a?(Date) ? _until : Date.parse(_until.to_s), +7, &block)
|
18
|
+
nil
|
19
|
+
end
|
20
|
+
|
21
|
+
# yields inclusive date object of first day of month
|
22
|
+
def each_month_until(_until=Date.today, &block)
|
23
|
+
return self.to_enum(:each_month_until, _until) unless block_given?
|
24
|
+
|
25
|
+
_date = Date.new(self.year,self.month,1)
|
26
|
+
_until = _until.is_a?(Date) ? _until : Date.parse(_until.to_s)
|
27
|
+
_until = Date.new(_until.year,_until.month,1)
|
28
|
+
|
29
|
+
while _date <= _until
|
30
|
+
yield _date
|
31
|
+
_date = _date >> 1
|
32
|
+
_date = Date.new(_date.year,_date.month,1)
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
# yields inclusive date object of first day of the year
|
37
|
+
def each_year_until(_until=Date.today, &block)
|
38
|
+
return self.to_enum(:each_year_until, _until) unless block_given?
|
39
|
+
|
40
|
+
_date = Date.new(self.year,1,1)
|
41
|
+
_until = _until.is_a?(Date) ? _until : Date.parse(_until.to_s)
|
42
|
+
_until = Date.new(_until.year,1,1)
|
43
|
+
|
44
|
+
while _date <= _until
|
45
|
+
yield _date
|
46
|
+
_date = _date >> 12
|
47
|
+
_date = Date.new(_date.year,_date.month,1)
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
end
|
52
|
+
|
53
|
+
Date.send(:include, DateIterator)
|
@@ -0,0 +1,100 @@
|
|
1
|
+
require_relative '../lib/date_iterator'
|
2
|
+
require 'minitest/autorun'
|
3
|
+
|
4
|
+
class DateIteratorTest < Minitest::Test
|
5
|
+
|
6
|
+
def test_day_iterator
|
7
|
+
counter = 0
|
8
|
+
Date.parse("2011-01-25").each_day_until("2011-02-02") do |date|
|
9
|
+
assert date.is_a?(Date)
|
10
|
+
counter += 1
|
11
|
+
end
|
12
|
+
assert_equal 9, counter
|
13
|
+
end
|
14
|
+
|
15
|
+
def test_day_iterator_with_iterator
|
16
|
+
counter = 0
|
17
|
+
Date.parse("2011-01-25").each_day_until("2011-02-02").each do |date|
|
18
|
+
assert date.is_a?(Date)
|
19
|
+
counter += 1
|
20
|
+
end
|
21
|
+
assert_equal 9, counter
|
22
|
+
end
|
23
|
+
|
24
|
+
def test_week_iterator
|
25
|
+
counter = 0
|
26
|
+
Date.parse("2010-12-05").each_week_until("2011-02-02") do |date|
|
27
|
+
assert date.is_a?(Date)
|
28
|
+
assert_equal 1, date.wday
|
29
|
+
counter += 1
|
30
|
+
end
|
31
|
+
assert_equal 10, counter
|
32
|
+
end
|
33
|
+
|
34
|
+
def test_week_iterator_with_iterator
|
35
|
+
counter = 0
|
36
|
+
Date.parse("2010-12-05").each_week_until("2011-02-02").each do |date|
|
37
|
+
assert date.is_a?(Date)
|
38
|
+
assert_equal 1, date.wday
|
39
|
+
counter += 1
|
40
|
+
end
|
41
|
+
assert_equal 10, counter
|
42
|
+
end
|
43
|
+
|
44
|
+
def test_month_iterator
|
45
|
+
counter = 0
|
46
|
+
Date.parse("2010-12-25").each_month_until("2011-05-02") do |date|
|
47
|
+
assert date.is_a?(Date)
|
48
|
+
assert_equal 1, date.day
|
49
|
+
counter += 1
|
50
|
+
end
|
51
|
+
assert_equal 6, counter
|
52
|
+
end
|
53
|
+
|
54
|
+
def test_month_iterator_with_iterator
|
55
|
+
counter = 0
|
56
|
+
Date.parse("2010-12-25").each_month_until("2011-05-02").each do |date|
|
57
|
+
assert date.is_a?(Date)
|
58
|
+
assert_equal 1, date.day
|
59
|
+
counter += 1
|
60
|
+
end
|
61
|
+
assert_equal 6, counter
|
62
|
+
end
|
63
|
+
|
64
|
+
def test_original_value_is_preserved_in_day_iterator_and_block_returns_nil
|
65
|
+
_date = Date.parse("2010-12-25")
|
66
|
+
assert_nil _date.each_day_until("2011-02-01") {}
|
67
|
+
assert_equal Date.parse("2010-12-25"), _date
|
68
|
+
end
|
69
|
+
|
70
|
+
def test_original_value_is_preserved_in_month_iterator_and_block_returns_nil
|
71
|
+
_date = Date.parse("2010-12-25")
|
72
|
+
assert_nil _date.each_week_until("2011-02-01") {}
|
73
|
+
assert_equal Date.parse("2010-12-25"), _date
|
74
|
+
end
|
75
|
+
|
76
|
+
def test_original_value_is_preserved_in_month_iterator_and_block_returns_nil
|
77
|
+
_date = Date.parse("2010-12-25")
|
78
|
+
assert_nil _date.each_month_until("2011-02-01") {}
|
79
|
+
assert_equal Date.parse("2010-12-25"), _date
|
80
|
+
end
|
81
|
+
|
82
|
+
def test_year_iterator
|
83
|
+
counter = 0
|
84
|
+
Date.parse("2009-01-01").each_year_until("2012-02-02") do |date|
|
85
|
+
assert date.is_a?(Date)
|
86
|
+
counter += 1
|
87
|
+
end
|
88
|
+
assert_equal 4, counter
|
89
|
+
end
|
90
|
+
|
91
|
+
def test_year_iterator_with_iterator
|
92
|
+
counter = 0
|
93
|
+
Date.parse("2009-01-01").each_year_until("2012-02-02").each do |date|
|
94
|
+
assert date.is_a?(Date)
|
95
|
+
counter += 1
|
96
|
+
end
|
97
|
+
assert_equal 4, counter
|
98
|
+
end
|
99
|
+
|
100
|
+
end
|
metadata
ADDED
@@ -0,0 +1,82 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: date_iterator
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Jonathan Otto
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-02-28 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.5'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.5'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
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
|
+
description: iterate by day, week, or month on the date class
|
42
|
+
email:
|
43
|
+
- jonathan.otto@gmail.com
|
44
|
+
executables: []
|
45
|
+
extensions: []
|
46
|
+
extra_rdoc_files: []
|
47
|
+
files:
|
48
|
+
- ".gitignore"
|
49
|
+
- Gemfile
|
50
|
+
- Gemfile.lock
|
51
|
+
- LICENSE
|
52
|
+
- README.md
|
53
|
+
- Rakefile
|
54
|
+
- date_iterator.gemspec
|
55
|
+
- lib/date_iterator.rb
|
56
|
+
- lib/date_iterator/version.rb
|
57
|
+
- test/date_iterator_test.rb
|
58
|
+
homepage: https://github.com/jotto/date_iterator
|
59
|
+
licenses: []
|
60
|
+
metadata: {}
|
61
|
+
post_install_message:
|
62
|
+
rdoc_options: []
|
63
|
+
require_paths:
|
64
|
+
- lib
|
65
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
66
|
+
requirements:
|
67
|
+
- - ">="
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '0'
|
70
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
71
|
+
requirements:
|
72
|
+
- - ">="
|
73
|
+
- !ruby/object:Gem::Version
|
74
|
+
version: '0'
|
75
|
+
requirements: []
|
76
|
+
rubyforge_project:
|
77
|
+
rubygems_version: 2.4.6
|
78
|
+
signing_key:
|
79
|
+
specification_version: 4
|
80
|
+
summary: iterate by day, week, or month on the date class
|
81
|
+
test_files:
|
82
|
+
- test/date_iterator_test.rb
|