nth_day 0.0.1

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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 748e855a150ef266a429bd80cea3eace6af0203e
4
+ data.tar.gz: ae1851606c0cb588a8f43f34cc6d6342c25d5ecc
5
+ SHA512:
6
+ metadata.gz: 4f9d76329278cee54ab4e0cf65cf55d55c2e9220041edf8b955bddf95599f0ec97ffcb14c0aecc63ed0f47ce090e47b06cf9ebd76d3c632f5ed405bf9addf522
7
+ data.tar.gz: d02404b9b4110d1353231c545720388b2e979f5b323a473c7fb4f2e3a0cb5b14f5ce98bf6976d1322da7d081e406362e187304a3f856918a7407c64670aada0b
@@ -0,0 +1,22 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ *.bundle
19
+ *.so
20
+ *.o
21
+ *.a
22
+ mkmf.log
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --require spec_helper
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in nth_day.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 Dolan Murphy
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.
@@ -0,0 +1,47 @@
1
+ # NthDay
2
+
3
+ A bite-size gem for finding the next occurrence of the *n*th *week*day of the month. For example, you can use it to find the next "2nd Monday" that will occur.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'nth_day'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install nth_day
18
+
19
+ ## Usage
20
+
21
+ Find the next occurrence of the nth day specified:
22
+
23
+ NthDay.next_occurrence("1st Tuesday")
24
+
25
+ You can also pass in a custom value for the date to start from if you don't want to find the next date from today:
26
+
27
+ NthDay.next_occurrence("1st Tuesday", Date.today - 1)
28
+
29
+ NthDay can handle abbreviated weekday names, and is case-insensitive. As long as your name has the first two characters of the name, NthDay will recognize it:
30
+
31
+ NthDay.next_occurrence("1st Tu")
32
+
33
+ ## Testing
34
+
35
+ NthDay uses [RSpec](http://rspec.info) for testing. To run the tests:
36
+
37
+ $ bundle exec rspec
38
+
39
+ ## Contributing
40
+
41
+ 1. Fork it ( https://github.com/dmur/nth-day/fork )
42
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
43
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
44
+ 4. Push to the branch (`git push origin my-new-feature`)
45
+ 5. Create a new Pull Request
46
+
47
+
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
@@ -0,0 +1,24 @@
1
+ require "nth_day/version"
2
+ require 'date'
3
+
4
+ module NthDay
5
+ def self.next_occurrence(day, now=Date.today)
6
+ n = day.split.first[/\d/].to_i
7
+ w = self.wday_named(day.split.last)
8
+ now.step(now + 37).select{|d| d.nth_wday?(n, w) }.first
9
+ end
10
+
11
+ def self.wday_named(name)
12
+ ["su", "mo", "tu", "we", "th", "fr", "sa"].index(name[0..1].downcase)
13
+ end
14
+ end
15
+
16
+ class Date
17
+ def nth_wday?(n, w)
18
+ (self.wday == w) && (self.nth_of_wday == n)
19
+ end
20
+
21
+ def nth_of_wday
22
+ (self.mday.to_f / 7).ceil
23
+ end
24
+ end
@@ -0,0 +1,3 @@
1
+ module NthDay
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'nth_day/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "nth_day"
8
+ spec.version = NthDay::VERSION
9
+ spec.authors = ["Dolan Murphy"]
10
+ spec.email = ["admin@dmur.me"]
11
+ spec.summary = %q{Query for the next nth weekday of the month.}
12
+ spec.description = %q{Nth day allows you to specify dates in the format "1st Tuesday", and have returned a date for the next occurrence of that day.}
13
+ spec.homepage = "http://github.com/dmur/nth-day"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.6"
22
+ spec.add_development_dependency "rake"
23
+ spec.add_development_dependency "rspec"
24
+ end
@@ -0,0 +1,131 @@
1
+ require 'nth_day'
2
+
3
+ RSpec.describe Date do
4
+ describe "#next_nth_day" do
5
+ context "when 2nd Wednesday passed" do
6
+ let(:next_date) { NthDay.next_occurrence("2nd Wednesday") }
7
+
8
+ it "returns a date after today" do
9
+ (next_date > Date.today).should be(true)
10
+ end
11
+ it "returns a wednesday" do
12
+ (next_date.wednesday?).should be(true)
13
+ end
14
+ it "returns the 2nd wednesday of the month" do
15
+ (next_date.nth_of_wday).should be(2)
16
+ end
17
+ end
18
+
19
+ context "when 4th Thu passed" do
20
+ let(:next_date) { NthDay.next_occurrence("4th Thu") }
21
+
22
+ it "returns a date after today" do
23
+ (next_date > Date.today).should be(true)
24
+ end
25
+ it "returns a thursday" do
26
+ (next_date.thursday?).should be(true)
27
+ end
28
+ it "returns the 4th thursday of the month" do
29
+ (next_date.nth_of_wday).should be(4)
30
+ end
31
+ end
32
+ end
33
+
34
+ describe "#nth_wday?" do
35
+ context "when date is Thursday, Jan 1st 2015" do
36
+ let(:date) { Date.parse('2015-01-01') }
37
+
38
+ it "is 1st thursday" do
39
+ (date.nth_wday?(1, 4)).should be(true)
40
+ end
41
+
42
+ it "is not 1st wednesday" do
43
+ (date.nth_wday?(1, 3)).should be(false)
44
+ end
45
+
46
+ it "is not 2nd thursday" do
47
+ (date.nth_wday?(2, 4)).should be(false)
48
+ end
49
+ end
50
+
51
+ context "when date is Friday, Feb 20th 2015" do
52
+ let(:date) { Date.parse('2015-02-20') }
53
+
54
+ it "is 3rd friday" do
55
+ (date.nth_wday?(3, 5)).should be(true)
56
+ end
57
+
58
+ it "is not 3rd thursday" do
59
+ (date.nth_wday?(3, 4)).should be(false)
60
+ end
61
+
62
+ it "is not 4th friday" do
63
+ (date.nth_wday?(4, 5)).should be(false)
64
+ end
65
+ end
66
+ end
67
+
68
+ describe "#nth_of_wday" do
69
+ context "when Jan 1st passed" do
70
+ let(:nth_of_wday) { Date.parse('2015-01-01').nth_of_wday }
71
+
72
+ it "returns a number" do
73
+ expect(nth_of_wday).to be_a(Fixnum)
74
+ end
75
+
76
+ it "returns 1" do
77
+ expect(nth_of_wday).to be(1)
78
+ end
79
+ end
80
+
81
+ context "when Feb 24th passed" do
82
+ let(:nth_of_wday) { Date.parse('2015-02-24').nth_of_wday }
83
+
84
+ it "returns a number" do
85
+ expect(nth_of_wday).to be_a(Fixnum)
86
+ end
87
+
88
+ it "returns 4" do
89
+ expect(nth_of_wday).to be(4)
90
+ end
91
+ end
92
+
93
+ context "when March 31st passed" do
94
+ let(:nth_of_wday) { Date.parse('2015-03-31').nth_of_wday }
95
+
96
+ it "returns a number" do
97
+ expect(nth_of_wday).to be_a(Fixnum)
98
+ end
99
+
100
+ it "returns 5" do
101
+ expect(nth_of_wday).to be(5)
102
+ end
103
+ end
104
+ end
105
+
106
+ describe "#wday_named" do
107
+ it "parses full weekday names" do
108
+ expect(NthDay.wday_named("Monday")).to be(1)
109
+ expect(NthDay.wday_named("Tuesday")).to be(2)
110
+ expect(NthDay.wday_named("Wednesday")).to be(3)
111
+ end
112
+
113
+ it "parses shortened names" do
114
+ expect(NthDay.wday_named("Thurs")).to be(4)
115
+ expect(NthDay.wday_named("Fri")).to be(5)
116
+ end
117
+
118
+ it "doesn't care about case" do
119
+ expect(NthDay.wday_named("MON")).to be(1)
120
+ end
121
+
122
+ it "even works with 2 character names" do
123
+ expect(NthDay.wday_named("Su")).to be(0)
124
+ expect(NthDay.wday_named("Sa")).to be(6)
125
+ end
126
+
127
+ it "does require at least 2 chars" do
128
+ expect(NthDay.wday_named("S")).to be_nil
129
+ end
130
+ end
131
+ end
@@ -0,0 +1,11 @@
1
+ require 'bundler/setup'
2
+ Bundler.setup
3
+
4
+ require 'nth_day'
5
+
6
+ RSpec.configure do |config|
7
+ config.expect_with :rspec do |expectations|
8
+ # Enable both expect() and .should syntax
9
+ expectations.syntax = [:should, :expect]
10
+ end
11
+ end
metadata ADDED
@@ -0,0 +1,100 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: nth_day
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Dolan Murphy
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-06-03 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.6'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.6'
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
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
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
+ description: Nth day allows you to specify dates in the format "1st Tuesday", and
56
+ have returned a date for the next occurrence of that day.
57
+ email:
58
+ - admin@dmur.me
59
+ executables: []
60
+ extensions: []
61
+ extra_rdoc_files: []
62
+ files:
63
+ - ".gitignore"
64
+ - ".rspec"
65
+ - Gemfile
66
+ - LICENSE.txt
67
+ - README.md
68
+ - Rakefile
69
+ - lib/nth_day.rb
70
+ - lib/nth_day/version.rb
71
+ - nth_day.gemspec
72
+ - spec/nth_day_spec.rb
73
+ - spec/spec_helper.rb
74
+ homepage: http://github.com/dmur/nth-day
75
+ licenses:
76
+ - MIT
77
+ metadata: {}
78
+ post_install_message:
79
+ rdoc_options: []
80
+ require_paths:
81
+ - lib
82
+ required_ruby_version: !ruby/object:Gem::Requirement
83
+ requirements:
84
+ - - ">="
85
+ - !ruby/object:Gem::Version
86
+ version: '0'
87
+ required_rubygems_version: !ruby/object:Gem::Requirement
88
+ requirements:
89
+ - - ">="
90
+ - !ruby/object:Gem::Version
91
+ version: '0'
92
+ requirements: []
93
+ rubyforge_project:
94
+ rubygems_version: 2.2.2
95
+ signing_key:
96
+ specification_version: 4
97
+ summary: Query for the next nth weekday of the month.
98
+ test_files:
99
+ - spec/nth_day_spec.rb
100
+ - spec/spec_helper.rb