nth_week_with_rules 1.0.0.rc1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +9 -0
- data/Gemfile +4 -0
- data/Guardfile +7 -0
- data/LICENSE.txt +21 -0
- data/README.md +118 -0
- data/Rakefile +10 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/nth_week_with_rules/version.rb +3 -0
- data/lib/nth_week_with_rules.rb +46 -0
- data/nth_week_with_rules.gemspec +28 -0
- metadata +140 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: ef752eb6223510597fe3ce6d57b4928ce8257665
|
4
|
+
data.tar.gz: c32f8805fbaf1b95d6e33ed6d4243d942fc74e4e
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: a0d2ac08026964d83da50625ba728ea50b511f9c9293b3867d9d6ead5f309bf2bdd2cfb7a6515a72b05c14ef104a3b63f70ee02e17df385e87e789dc3703487c
|
7
|
+
data.tar.gz: 4a4284cdf5e37690c91a01d6a2084f38c49ae94e4f92200c5e6a4cb3e1a8e47dcb6e4934b8b97347bfdb2915fed6e56dfb19a7a7500c0538b41ba0ab00905379
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/Guardfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2016 ka
|
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
|
13
|
+
all 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
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,118 @@
|
|
1
|
+
# NthWeekWithRules
|
2
|
+
|
3
|
+
Calculate n-th week of the date with some rules.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'nth_week_with_rules'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install nth_week_with_rules
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
```ruby
|
24
|
+
require 'nth_week_with_rules'
|
25
|
+
|
26
|
+
date = Date.new(2016, 4, 1)
|
27
|
+
|
28
|
+
date.nth_week #=> 1 (means 1st week of 2016-04)
|
29
|
+
|
30
|
+
date.nth_week(month_select: before) #=> 5 (means 5th week of 2016-03)
|
31
|
+
# :before option makes the method to take a before month
|
32
|
+
# if the week is over 2 months.
|
33
|
+
|
34
|
+
date = Date.new(2016, 3, 31)
|
35
|
+
|
36
|
+
date.nth_week(month_select: :after) #=> 1 (means 1st week of 2016-04)
|
37
|
+
# :after option makes the method to take a after month
|
38
|
+
# if the week is over 2 months.
|
39
|
+
|
40
|
+
date = Date.new(2016, 9, 1)
|
41
|
+
|
42
|
+
date.nth_week(month_select: :many) #=> 5 (means 5th week of 2016-08)
|
43
|
+
# :many option makes the method to take a month which has many days.
|
44
|
+
# 2016-08-28, 2016-08-29, 2016-08-30, 2016-08-31: 4 days
|
45
|
+
# 2016-09-01, 2016-09-02, 2016-09-03: 3 days
|
46
|
+
|
47
|
+
date = Date.new(2016, 4, 30)
|
48
|
+
|
49
|
+
date.nth_week(first_wday: 6) #=> 6 (means 6th week of 2016-04)
|
50
|
+
# Do all calculations on the calendar that Saturday is the first wday.
|
51
|
+
# If the first wday of a calendar is Saturday, 2016-04 becomes below:
|
52
|
+
#
|
53
|
+
# Sa Su Mo Tu We Th Fr
|
54
|
+
# 1
|
55
|
+
# 2 3 4 5 6 7 8
|
56
|
+
# 9 10 11 12 13 14 15
|
57
|
+
# 16 17 18 19 20 21 22
|
58
|
+
# 23 24 25 26 27 28 29
|
59
|
+
# 30
|
60
|
+
#
|
61
|
+
# So 2016-04-01 is the 6th week of 2016-04.
|
62
|
+
|
63
|
+
date = Date.new(2016, 4, 1)
|
64
|
+
date.nth_week #=> 1
|
65
|
+
date.nth_week(base: 0) #=> 0
|
66
|
+
# You can get 0 base indices. The default base is 1.
|
67
|
+
```
|
68
|
+
|
69
|
+
Of cource you can use a complex rule.
|
70
|
+
|
71
|
+
```ruby
|
72
|
+
date = Date.new(2016, 4, 1)
|
73
|
+
date.nth_week(first_wday: 3, month_select: :before, base: 0) #=> 5
|
74
|
+
```
|
75
|
+
|
76
|
+
...Is it really correct? Yes!
|
77
|
+
|
78
|
+
```
|
79
|
+
# Start from Wednesday (wday 3)
|
80
|
+
#
|
81
|
+
# March 2016
|
82
|
+
# We Th Fr Sa Su Mo Tu
|
83
|
+
# 1 0th (start from 0th because of base 0 specification)
|
84
|
+
# 2 3 4 5 6 7 8 1st
|
85
|
+
# 9 10 11 12 13 14 15 2nd
|
86
|
+
# 16 17 18 19 20 21 22 3rd
|
87
|
+
# 23 24 25 26 27 28 29 4th
|
88
|
+
# 30 31 5th
|
89
|
+
#
|
90
|
+
# April 2016
|
91
|
+
# We Th Fr Sa Su Mo Tu
|
92
|
+
# 1 2 3 4 5 0th (2016-04-01 is here! And select_month is :before)
|
93
|
+
# 6 7 8 9 10 11 12 1st
|
94
|
+
# 13 14 15 16 17 18 19 ...
|
95
|
+
# 20 21 22 23 24 25 26
|
96
|
+
# 27 28 29 30
|
97
|
+
```
|
98
|
+
|
99
|
+
So, 2016-04-01 is the 5th week of 2016-03.
|
100
|
+
|
101
|
+
## Development
|
102
|
+
|
103
|
+
After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
104
|
+
|
105
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
106
|
+
|
107
|
+
Run `bundle exec guard` for the automatic testing.
|
108
|
+
|
109
|
+
## Contributing
|
110
|
+
|
111
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/kaosf/nth_week_with_rules.
|
112
|
+
|
113
|
+
|
114
|
+
## License
|
115
|
+
|
116
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
117
|
+
|
118
|
+
Copyright (C) 2016 ka
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "nth_week_with_rules"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start
|
data/bin/setup
ADDED
@@ -0,0 +1,46 @@
|
|
1
|
+
require "nth_week_with_rules/version"
|
2
|
+
|
3
|
+
module NthWeekWithRules
|
4
|
+
# @param [Fixnum] first_wday 0, 1, 2, 3, 4, 5 or 6 (default is 0)
|
5
|
+
# @param [Symbol] month_select :normal, :before, :after, :many (default is :normal)
|
6
|
+
# @param [Fixnum] base 0 or 1 (default is 1)
|
7
|
+
# @return [Fixnum] An integer from 1 to 6 (from 0 to 5 if **base** is 0)
|
8
|
+
def nth_week(first_wday: 0, month_select: :normal, base: 1)
|
9
|
+
start_date = first_date_of_week(first_wday: first_wday)
|
10
|
+
end_date = start_date + 7
|
11
|
+
|
12
|
+
week = (start_date.day + 5) / 7
|
13
|
+
case month_select
|
14
|
+
when :normal
|
15
|
+
if start_date.month != end_date.month && self.month == end_date.month
|
16
|
+
week = 0
|
17
|
+
end
|
18
|
+
when :before
|
19
|
+
# Do nothing
|
20
|
+
when :after
|
21
|
+
if start_date.month != end_date.month
|
22
|
+
week = 0
|
23
|
+
end
|
24
|
+
when
|
25
|
+
middle_date = start_date + 3
|
26
|
+
if start_date.month != end_date.month && middle_date.month == end_date.month
|
27
|
+
week = 0
|
28
|
+
end
|
29
|
+
else
|
30
|
+
# Error
|
31
|
+
end
|
32
|
+
base + week
|
33
|
+
end
|
34
|
+
|
35
|
+
# @param [Fixnum] first_wday 0, 1, 2, 3, 4, 5 or 6 (default is 0)
|
36
|
+
# @return [Date] first date of the week of this instance's date
|
37
|
+
def first_date_of_week(first_wday: 0)
|
38
|
+
self - ((self.wday - first_wday) % 7)
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
require "date"
|
43
|
+
|
44
|
+
class Date
|
45
|
+
include NthWeekWithRules
|
46
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'nth_week_with_rules/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "nth_week_with_rules"
|
8
|
+
spec.version = NthWeekWithRules::VERSION
|
9
|
+
spec.authors = ["ka"]
|
10
|
+
spec.email = ["ka.kaosf@gmail.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{Calculate n-th week of the date with some rules}
|
13
|
+
spec.description = %q{Calculate n-th week of the date with some rules}
|
14
|
+
spec.homepage = "https://github.com/kaosf/nth_week_with_rules"
|
15
|
+
spec.license = "MIT"
|
16
|
+
|
17
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
18
|
+
spec.bindir = "exe"
|
19
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
20
|
+
spec.require_paths = ["lib"]
|
21
|
+
|
22
|
+
spec.add_development_dependency "bundler", "~> 1.11"
|
23
|
+
spec.add_development_dependency "rake", "~> 11.1"
|
24
|
+
spec.add_development_dependency "test-unit", "~> 3.1"
|
25
|
+
spec.add_development_dependency "test-unit-notify", "~> 1.0"
|
26
|
+
spec.add_development_dependency "guard", "~> 2.13"
|
27
|
+
spec.add_development_dependency "guard-minitest", "~> 2.4"
|
28
|
+
end
|
metadata
ADDED
@@ -0,0 +1,140 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: nth_week_with_rules
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0.rc1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- ka
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-04-02 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.11'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.11'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '11.1'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '11.1'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: test-unit
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '3.1'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '3.1'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: test-unit-notify
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '1.0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '1.0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: guard
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '2.13'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '2.13'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: guard-minitest
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - "~>"
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '2.4'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - "~>"
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '2.4'
|
97
|
+
description: Calculate n-th week of the date with some rules
|
98
|
+
email:
|
99
|
+
- ka.kaosf@gmail.com
|
100
|
+
executables: []
|
101
|
+
extensions: []
|
102
|
+
extra_rdoc_files: []
|
103
|
+
files:
|
104
|
+
- ".gitignore"
|
105
|
+
- Gemfile
|
106
|
+
- Guardfile
|
107
|
+
- LICENSE.txt
|
108
|
+
- README.md
|
109
|
+
- Rakefile
|
110
|
+
- bin/console
|
111
|
+
- bin/setup
|
112
|
+
- lib/nth_week_with_rules.rb
|
113
|
+
- lib/nth_week_with_rules/version.rb
|
114
|
+
- nth_week_with_rules.gemspec
|
115
|
+
homepage: https://github.com/kaosf/nth_week_with_rules
|
116
|
+
licenses:
|
117
|
+
- MIT
|
118
|
+
metadata: {}
|
119
|
+
post_install_message:
|
120
|
+
rdoc_options: []
|
121
|
+
require_paths:
|
122
|
+
- lib
|
123
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
124
|
+
requirements:
|
125
|
+
- - ">="
|
126
|
+
- !ruby/object:Gem::Version
|
127
|
+
version: '0'
|
128
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
129
|
+
requirements:
|
130
|
+
- - ">"
|
131
|
+
- !ruby/object:Gem::Version
|
132
|
+
version: 1.3.1
|
133
|
+
requirements: []
|
134
|
+
rubyforge_project:
|
135
|
+
rubygems_version: 2.5.1
|
136
|
+
signing_key:
|
137
|
+
specification_version: 4
|
138
|
+
summary: Calculate n-th week of the date with some rules
|
139
|
+
test_files: []
|
140
|
+
has_rdoc:
|