ordered_week 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/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +78 -0
- data/Rakefile +7 -0
- data/lib/ordered_week.rb +67 -0
- data/ordered_week.gemspec +25 -0
- data/spec/ordered_week_spec.rb +208 -0
- data/spec/spec_helper.rb +8 -0
- metadata +99 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 0517bac6ebb69bc05da834db9ee73f2b1bd1beea
|
4
|
+
data.tar.gz: d1948d0bb756d9ef883890178695e3aadb4f186c
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 8c1a790f59d4a0ea9324419bbe9a2adf39ac40efca131d69520dbac5e21165ec796f6a3d90c77ea1bdb6c5e03865b9e0df84d3dd0eff665e0380b5670840660e
|
7
|
+
data.tar.gz: 5496b24554658c13a59992cdc85e57dd562aa163a0811011b0dd66c6724c359a2107ef388c3cc582e914e11dac19d54ef7943f090515f17265bd0a2c459e8122
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 Brad Rice
|
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,78 @@
|
|
1
|
+
# OrderedWeek
|
2
|
+
|
3
|
+
A class useful for when it makes sense to think of a week, conceptually, as a single object.
|
4
|
+
An example, at least my use case, would be an app tracking pay periods. It makes sense to explicitly label a beginning marker.
|
5
|
+
|
6
|
+
## Installation
|
7
|
+
|
8
|
+
Add this line to your application's Gemfile:
|
9
|
+
|
10
|
+
gem 'ordered_week'
|
11
|
+
|
12
|
+
And then execute:
|
13
|
+
|
14
|
+
$ bundle
|
15
|
+
|
16
|
+
Or install it yourself as:
|
17
|
+
|
18
|
+
$ gem install ordered_week
|
19
|
+
|
20
|
+
## Usage
|
21
|
+
|
22
|
+
You now have access to the OrderedWeek class, which returns an array of Dates, which includes an optionally given Date (defaults to including current day).
|
23
|
+
Note: The following examples assume the current date to be Sunday, 2014-02-16.
|
24
|
+
|
25
|
+
OrderedWeek.new
|
26
|
+
#=> [2014-02-10, 2014-02-11, ... ]
|
27
|
+
|
28
|
+
These collections have a concept of where they start and end.
|
29
|
+
|
30
|
+
OrderedWeek.new.start_date
|
31
|
+
#=> #<Date: 2014-02-10 ((2456699j,0s,0n),+0s,2299161j)>
|
32
|
+
OrderedWeek.new.end_date
|
33
|
+
#=> #<Date: 2014-02-16 ((2456705j,0s,0n),+0s,2299161j)>
|
34
|
+
|
35
|
+
By default, the "week" starts on Monday. You can change this by setting the class option.
|
36
|
+
Note: The following example shows that by resetting the start date for the week, a completely different week can be returned.
|
37
|
+
|
38
|
+
OrderedWeek.start_day
|
39
|
+
#=> :monday
|
40
|
+
OrderedWeek.start_day = :sunday
|
41
|
+
#=> :sunday
|
42
|
+
OrderedWeek.new.start_date
|
43
|
+
#=> #<Date: 2014-02-16 ((2456705j,0s,0n),+0s,2299161j)>
|
44
|
+
|
45
|
+
Don't worry though! If you accidentally pass an argument that isn't the symbol of a valid day of the week, nothing changes
|
46
|
+
|
47
|
+
OrderedWeek.start_day
|
48
|
+
#=> :monday
|
49
|
+
OrderedWeek.start_day = :bad
|
50
|
+
#=> :bad
|
51
|
+
OrderedWeek.start_day
|
52
|
+
#=> :monday
|
53
|
+
|
54
|
+
These objects respond to the following methods
|
55
|
+
|
56
|
+
OrderedWeek.new.monday
|
57
|
+
#=> #<Date: 2014-02-10 ((2456699j,0s,0n),+0s,2299161j)>
|
58
|
+
|
59
|
+
...
|
60
|
+
|
61
|
+
OrderedWeek.new.sunday
|
62
|
+
#=> #<Date: 2014-02-16 ((2456705j,0s,0n),+0s,2299161j)>
|
63
|
+
|
64
|
+
OrderedWeek.new.to_a
|
65
|
+
#=> [#<Date: 2014-02-10 ((2456699j,0s,0n),+0s,2299161j)>, #<Date: 2014-02-11 ((2456700j,0s,0n),+0s,2299161j)>, ... ]
|
66
|
+
|
67
|
+
The objects are also enumerable, so feel free to modify your Dates however you need
|
68
|
+
|
69
|
+
OrderedWeek.new.map {|day| day.strftime("%F")}
|
70
|
+
#=> ["2014-02-10", "2014-02-11", ... ]
|
71
|
+
|
72
|
+
## Contributing
|
73
|
+
|
74
|
+
1. Fork it ( http://github.com/<my-github-username>/ordered_week/fork )
|
75
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
76
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
77
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
78
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
data/lib/ordered_week.rb
ADDED
@@ -0,0 +1,67 @@
|
|
1
|
+
class OrderedWeek
|
2
|
+
include Enumerable
|
3
|
+
|
4
|
+
VERSION = '0.0.1'
|
5
|
+
WEEK_DAYS = [:sunday, :monday, :tuesday, :wednesday, :thursday, :friday, :saturday]
|
6
|
+
|
7
|
+
@@start_day ||= :monday
|
8
|
+
|
9
|
+
private_constant :WEEK_DAYS
|
10
|
+
|
11
|
+
def self.start_day
|
12
|
+
@@start_day
|
13
|
+
end
|
14
|
+
|
15
|
+
def self.start_day= day
|
16
|
+
return false unless WEEK_DAYS.include?(day)
|
17
|
+
@@start_day = day
|
18
|
+
end
|
19
|
+
|
20
|
+
def initialize includes_date=nil
|
21
|
+
includes_date = includes_date.is_a?(Date) ? includes_date : Date.today
|
22
|
+
@days = build_days(includes_date)
|
23
|
+
end
|
24
|
+
|
25
|
+
def inspect
|
26
|
+
@days.map {|d| d.strftime("%F")}.inspect.gsub('"','')
|
27
|
+
end
|
28
|
+
|
29
|
+
def to_a
|
30
|
+
@days
|
31
|
+
end
|
32
|
+
|
33
|
+
def each &block
|
34
|
+
@days.each {|d| yield d}
|
35
|
+
end
|
36
|
+
|
37
|
+
def start_date
|
38
|
+
@days.first
|
39
|
+
end
|
40
|
+
|
41
|
+
def end_date
|
42
|
+
@days.last
|
43
|
+
end
|
44
|
+
|
45
|
+
WEEK_DAYS.each_with_index do |day, day_index|
|
46
|
+
define_method day do
|
47
|
+
@days[ day_index - start_day_index ]
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
private
|
52
|
+
|
53
|
+
def build_days(date)
|
54
|
+
while not date_is_start_of_week(date)
|
55
|
+
date -= 1
|
56
|
+
end
|
57
|
+
(date..date+6).to_a
|
58
|
+
end
|
59
|
+
|
60
|
+
def date_is_start_of_week date
|
61
|
+
date.send( (@@start_day.to_s + ??).to_sym )
|
62
|
+
end
|
63
|
+
|
64
|
+
def start_day_index
|
65
|
+
WEEK_DAYS.index(@@start_day)
|
66
|
+
end
|
67
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
#require 'ordered_week/version'
|
5
|
+
require 'ordered_week'
|
6
|
+
|
7
|
+
Gem::Specification.new do |spec|
|
8
|
+
spec.name = "ordered_week"
|
9
|
+
spec.version = OrderedWeek::VERSION
|
10
|
+
spec.authors = ["Brad Rice"]
|
11
|
+
spec.email = ["bradley_rice@mac.com"]
|
12
|
+
spec.summary = %q{A customizable ordered collection of Dates}
|
13
|
+
spec.description = %q{Useful for situations in which the concept of a week requires a specific start/stop marker}
|
14
|
+
spec.homepage = ""
|
15
|
+
spec.license = "MIT"
|
16
|
+
|
17
|
+
spec.files = `git ls-files -z`.split("\x0")
|
18
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
19
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
20
|
+
spec.require_paths = ["lib"]
|
21
|
+
|
22
|
+
spec.add_development_dependency "bundler", "~> 1.5"
|
23
|
+
spec.add_development_dependency "rake"
|
24
|
+
spec.add_development_dependency "rspec", "~> 2.14.1"
|
25
|
+
end
|
@@ -0,0 +1,208 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe OrderedWeek do
|
4
|
+
describe "::start_day" do
|
5
|
+
subject { OrderedWeek.start_day }
|
6
|
+
|
7
|
+
it "should respond" do
|
8
|
+
expect{ subject }.to_not raise_error
|
9
|
+
end
|
10
|
+
|
11
|
+
it "should return a Symbol" do
|
12
|
+
subject.should be_a Symbol
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
describe "::start_day=" do
|
17
|
+
subject { OrderedWeek.start_day = :sunday }
|
18
|
+
|
19
|
+
it "should respond" do
|
20
|
+
expect{ subject }.to_not raise_error
|
21
|
+
end
|
22
|
+
|
23
|
+
context "given a valid day of week" do
|
24
|
+
let(:day) { :wednesday }
|
25
|
+
|
26
|
+
subject { OrderedWeek.start_day = day }
|
27
|
+
|
28
|
+
it "should update the established start of week" do
|
29
|
+
OrderedWeek.start_day.should_not eq day
|
30
|
+
subject
|
31
|
+
OrderedWeek.start_day.should eq day
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
context "given an invalid day of week" do
|
36
|
+
let(:day) { :bad }
|
37
|
+
|
38
|
+
subject { OrderedWeek.start_day = day }
|
39
|
+
|
40
|
+
it "should not update the established start of week" do
|
41
|
+
OrderedWeek.start_day.should_not eq day
|
42
|
+
subject
|
43
|
+
OrderedWeek.start_day.should_not eq day
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
describe "::new" do
|
49
|
+
subject { OrderedWeek.new }
|
50
|
+
|
51
|
+
it "should respond" do
|
52
|
+
expect{ subject }.to_not raise_error
|
53
|
+
end
|
54
|
+
|
55
|
+
it "should accept a Date" do
|
56
|
+
expect{ OrderedWeek.new(Date.today) }.to_not raise_error
|
57
|
+
end
|
58
|
+
|
59
|
+
it "should default to the current week, if not given an arg" do
|
60
|
+
with_arg = OrderedWeek.new(Date.today)
|
61
|
+
without_arg = OrderedWeek.new
|
62
|
+
with_arg.start_date.should eq without_arg.start_date
|
63
|
+
with_arg.end_date.should eq without_arg.end_date
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
describe "An instance of", OrderedWeek do
|
68
|
+
subject { OrderedWeek.new }
|
69
|
+
|
70
|
+
describe "#start_date" do
|
71
|
+
subject { OrderedWeek.new.start_date }
|
72
|
+
|
73
|
+
it "should respond" do
|
74
|
+
expect{ subject }.to_not raise_error
|
75
|
+
end
|
76
|
+
|
77
|
+
it "should return a Date" do
|
78
|
+
subject.should be_a Date
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
82
|
+
describe "#end_date" do
|
83
|
+
subject { OrderedWeek.new.end_date }
|
84
|
+
|
85
|
+
it "should respond" do
|
86
|
+
expect{ subject }.to_not raise_error
|
87
|
+
end
|
88
|
+
|
89
|
+
it "should return a Date" do
|
90
|
+
subject.should be_a Date
|
91
|
+
end
|
92
|
+
end
|
93
|
+
|
94
|
+
describe "#sunday" do
|
95
|
+
subject { OrderedWeek.new.sunday }
|
96
|
+
|
97
|
+
it "should respond" do
|
98
|
+
expect{ subject }.to_not raise_error
|
99
|
+
end
|
100
|
+
|
101
|
+
it "should return a Date" do
|
102
|
+
subject.should be_a Date
|
103
|
+
end
|
104
|
+
end
|
105
|
+
|
106
|
+
describe "#monday" do
|
107
|
+
subject { OrderedWeek.new.monday }
|
108
|
+
|
109
|
+
it "should respond" do
|
110
|
+
expect{ subject }.to_not raise_error
|
111
|
+
end
|
112
|
+
|
113
|
+
it "should return a Date" do
|
114
|
+
subject.should be_a Date
|
115
|
+
end
|
116
|
+
end
|
117
|
+
|
118
|
+
describe "#tuesday" do
|
119
|
+
subject { OrderedWeek.new.tuesday }
|
120
|
+
|
121
|
+
it "should respond" do
|
122
|
+
expect{ subject }.to_not raise_error
|
123
|
+
end
|
124
|
+
|
125
|
+
it "should return a Date" do
|
126
|
+
subject.should be_a Date
|
127
|
+
end
|
128
|
+
end
|
129
|
+
|
130
|
+
describe "#wednesday" do
|
131
|
+
subject { OrderedWeek.new.wednesday }
|
132
|
+
|
133
|
+
it "should respond" do
|
134
|
+
expect{ subject }.to_not raise_error
|
135
|
+
end
|
136
|
+
|
137
|
+
it "should return a Date" do
|
138
|
+
subject.should be_a Date
|
139
|
+
end
|
140
|
+
end
|
141
|
+
|
142
|
+
describe "#thursday" do
|
143
|
+
subject { OrderedWeek.new.thursday }
|
144
|
+
|
145
|
+
it "should respond" do
|
146
|
+
expect{ subject }.to_not raise_error
|
147
|
+
end
|
148
|
+
|
149
|
+
it "should return a Date" do
|
150
|
+
subject.should be_a Date
|
151
|
+
end
|
152
|
+
end
|
153
|
+
|
154
|
+
describe "#friday" do
|
155
|
+
subject { OrderedWeek.new.friday }
|
156
|
+
|
157
|
+
it "should respond" do
|
158
|
+
expect{ subject }.to_not raise_error
|
159
|
+
end
|
160
|
+
|
161
|
+
it "should return a Date" do
|
162
|
+
subject.should be_a Date
|
163
|
+
end
|
164
|
+
end
|
165
|
+
|
166
|
+
describe "#saturday" do
|
167
|
+
subject { OrderedWeek.new.saturday }
|
168
|
+
|
169
|
+
it "should respond" do
|
170
|
+
expect{ subject }.to_not raise_error
|
171
|
+
end
|
172
|
+
|
173
|
+
it "should return a Date" do
|
174
|
+
subject.should be_a Date
|
175
|
+
end
|
176
|
+
end
|
177
|
+
|
178
|
+
describe "#inspect" do
|
179
|
+
subject { OrderedWeek.new.inspect }
|
180
|
+
|
181
|
+
it "should respond" do
|
182
|
+
expect{ subject }.to_not raise_error
|
183
|
+
end
|
184
|
+
end
|
185
|
+
|
186
|
+
describe "#to_a" do
|
187
|
+
subject { OrderedWeek.new.to_a }
|
188
|
+
|
189
|
+
it "should respond" do
|
190
|
+
expect{ subject }.to_not raise_error
|
191
|
+
end
|
192
|
+
end
|
193
|
+
|
194
|
+
describe "#each" do
|
195
|
+
subject { OrderedWeek.new.each {|obj| obj} }
|
196
|
+
|
197
|
+
it "should respond" do
|
198
|
+
expect{ subject }.to_not raise_error
|
199
|
+
end
|
200
|
+
|
201
|
+
it "should make for enumerable operations" do
|
202
|
+
enum_return = OrderedWeek.new.map {|obj| obj.strftime("%F")}
|
203
|
+
enum_return.each {|e_obj| e_obj.should be_a String}
|
204
|
+
end
|
205
|
+
end
|
206
|
+
|
207
|
+
end
|
208
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,99 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: ordered_week
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Brad Rice
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-02-17 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
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ~>
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: 2.14.1
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ~>
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: 2.14.1
|
55
|
+
description: Useful for situations in which the concept of a week requires a specific
|
56
|
+
start/stop marker
|
57
|
+
email:
|
58
|
+
- bradley_rice@mac.com
|
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/ordered_week.rb
|
70
|
+
- ordered_week.gemspec
|
71
|
+
- spec/ordered_week_spec.rb
|
72
|
+
- spec/spec_helper.rb
|
73
|
+
homepage: ''
|
74
|
+
licenses:
|
75
|
+
- MIT
|
76
|
+
metadata: {}
|
77
|
+
post_install_message:
|
78
|
+
rdoc_options: []
|
79
|
+
require_paths:
|
80
|
+
- lib
|
81
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
82
|
+
requirements:
|
83
|
+
- - '>='
|
84
|
+
- !ruby/object:Gem::Version
|
85
|
+
version: '0'
|
86
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
87
|
+
requirements:
|
88
|
+
- - '>='
|
89
|
+
- !ruby/object:Gem::Version
|
90
|
+
version: '0'
|
91
|
+
requirements: []
|
92
|
+
rubyforge_project:
|
93
|
+
rubygems_version: 2.0.3
|
94
|
+
signing_key:
|
95
|
+
specification_version: 4
|
96
|
+
summary: A customizable ordered collection of Dates
|
97
|
+
test_files:
|
98
|
+
- spec/ordered_week_spec.rb
|
99
|
+
- spec/spec_helper.rb
|