calendar_walker 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 +7 -0
- data/.gitignore +17 -0
- data/.rspec +2 -0
- data/.travis.yml +8 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +33 -0
- data/Rakefile +1 -0
- data/calendar_walker.gemspec +26 -0
- data/lib/calendar_walker.rb +5 -0
- data/lib/calendar_walker/version.rb +3 -0
- data/lib/calendar_walker/walker.rb +45 -0
- data/spec/calendar_walker/walker_spec.rb +58 -0
- metadata +100 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: eaa62fcc7ca6a3e18e16580176a21d20f729d0aa
|
4
|
+
data.tar.gz: 478e148a67b788158eedae8e33c2793c7eacf1c9
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: b163f3da2add1effcdff57cb74e70839af918c17b79a9c3e1c700d250988ba1f96ba19a9f12c626d12b4da0d59525554ca2ca4791d915685dd4f3ca6c9f9bae6
|
7
|
+
data.tar.gz: 2097c85da2a9688dba3fad799d165082905ee09a800945720ae57553f574af75cd6b25f5df861933df563266eed1e707ed94b7576cf97f44eba5283e2179423a
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 Pavel Makarov
|
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,33 @@
|
|
1
|
+
# CalendarWalker
|
2
|
+
|
3
|
+
A simple gem to get calendars in correct order and walk through them as you want.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'calendar_walker'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install calendar_walker
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
CalendarWalker::Walker.new(Date.new(2014, 1, 1)).wday_list # => [0, 1, 2, 3, 4, 5, 6]
|
22
|
+
|
23
|
+
walker = CalendarWalker::Walker.new(Date.new(2014, 5, 5), 6)
|
24
|
+
walker.wday_list # => [6, 0, 1, 2, 3, 4, 5]
|
25
|
+
walker.days_matrix # => [[Date.new(2014, 4, 28), Date.new(2014, 4, 27), ...], ..., [..., Date.new(2014, 6, 1)]]
|
26
|
+
|
27
|
+
## Contributing
|
28
|
+
|
29
|
+
1. Fork it ( http://github.com/violarium/calendar_walker/fork )
|
30
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
31
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
32
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
33
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
@@ -0,0 +1,26 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'calendar_walker/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "calendar_walker"
|
8
|
+
spec.version = CalendarWalker::VERSION
|
9
|
+
spec.authors = ["Pavel Makarov"]
|
10
|
+
spec.email = ["metus.violarium@gmail.com"]
|
11
|
+
spec.summary = %q{Simple gem to walk through calendar months.}
|
12
|
+
spec.description = %q{Gem which allows you to walk through months in calendar, print them in proper order.}
|
13
|
+
spec.homepage = "https://github.com/violarium/calendar_walker"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.required_ruby_version = '>= 1.9.3'
|
17
|
+
|
18
|
+
spec.files = `git ls-files -z`.split("\x0")
|
19
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
20
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
21
|
+
spec.require_paths = ["lib"]
|
22
|
+
|
23
|
+
spec.add_development_dependency "bundler", "~> 1.5"
|
24
|
+
spec.add_development_dependency "rake", "~> 10.3"
|
25
|
+
spec.add_development_dependency "rspec", "~> 2.14"
|
26
|
+
end
|
@@ -0,0 +1,45 @@
|
|
1
|
+
module CalendarWalker
|
2
|
+
# Class to walk through month in calendar
|
3
|
+
class Walker
|
4
|
+
attr_reader :current_date, :first_wday
|
5
|
+
|
6
|
+
# Create a new walker to walk through month in proper order.
|
7
|
+
# Arguments:
|
8
|
+
# current date (date, datetime or time)
|
9
|
+
# number of start week day between 0 and 6. 0 - Sunday, 6 - Monday.
|
10
|
+
# Examples:
|
11
|
+
# CalendarWalker::Walker.new(Date.new(2014, 1, 1))
|
12
|
+
# CalendarWalker::Walker.new(Time.now)
|
13
|
+
def initialize(date, first_wday = 0)
|
14
|
+
raise IncorrectWDay, 'Weekday should have value between 0 and 6' unless 0 <= first_wday and first_wday <= 6
|
15
|
+
|
16
|
+
@current_date = date
|
17
|
+
@first_wday = first_wday
|
18
|
+
end
|
19
|
+
|
20
|
+
# Get array of week days in proper order.
|
21
|
+
def wday_list
|
22
|
+
(@first_wday .. 6).to_a + (0 ... @first_wday).to_a
|
23
|
+
end
|
24
|
+
|
25
|
+
# Get matrix of all days in and near by this month.
|
26
|
+
# Returns an array.
|
27
|
+
# Each element of array represents a row of 7 days in proper order according to start week day.
|
28
|
+
# Each element is a Date object.
|
29
|
+
def days_matrix
|
30
|
+
start_date = Date.new(@current_date.year, @current_date.month)
|
31
|
+
end_date = Date.new(@current_date.year, @current_date.month, -1)
|
32
|
+
until start_date.wday == @first_wday
|
33
|
+
start_date = start_date.prev_day
|
34
|
+
end
|
35
|
+
until end_date.wday == (@first_wday + 6) % 7
|
36
|
+
end_date = end_date.next_day
|
37
|
+
end
|
38
|
+
(start_date .. end_date).to_a.each_slice(7).to_a
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
# Error about incorrect week day.
|
43
|
+
class IncorrectWDay < ::StandardError
|
44
|
+
end
|
45
|
+
end
|
@@ -0,0 +1,58 @@
|
|
1
|
+
require 'rspec'
|
2
|
+
require 'calendar_walker'
|
3
|
+
|
4
|
+
describe CalendarWalker::Walker do
|
5
|
+
let(:current_date) { DateTime.new(2014, 5, 5) }
|
6
|
+
|
7
|
+
describe '#current_date' do
|
8
|
+
it 'should return same current date' do
|
9
|
+
walker = CalendarWalker::Walker.new(current_date)
|
10
|
+
walker.current_date.should eq current_date
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
describe '#first_wday' do
|
15
|
+
it 'should be Sunday by default' do
|
16
|
+
CalendarWalker::Walker.new(current_date).first_wday.should eq 0
|
17
|
+
end
|
18
|
+
it 'should return received wday number' do
|
19
|
+
CalendarWalker::Walker.new(current_date, 4).first_wday.should eq 4
|
20
|
+
end
|
21
|
+
it 'should throw an error when week day is incorrect' do
|
22
|
+
expect do
|
23
|
+
CalendarWalker::Walker.new(current_date, -1)
|
24
|
+
end.to raise_error CalendarWalker::IncorrectWDay
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
describe '#days_matrix' do
|
29
|
+
context 'first wday is Sunday' do
|
30
|
+
let(:walker) { CalendarWalker::Walker.new(current_date) }
|
31
|
+
it 'should print days in proper order' do
|
32
|
+
walker.days_matrix.should eq (Date.new(2014, 4, 27)..Date.new(2014, 5, 31)).to_a.each_slice(7).to_a
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
context 'first day is Monday' do
|
37
|
+
let(:walker) { CalendarWalker::Walker.new(current_date, 1) }
|
38
|
+
it 'should print days in proper order' do
|
39
|
+
walker.days_matrix.should eq (Date.new(2014, 4, 28)..Date.new(2014, 6, 1)).to_a.each_slice(7).to_a
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
|
45
|
+
describe '#wday_list' do
|
46
|
+
context 'first wday is Sunday' do
|
47
|
+
it 'should print week days in proper order' do
|
48
|
+
CalendarWalker::Walker.new(current_date).wday_list.should eq [0, 1, 2, 3, 4, 5, 6]
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
context 'first day is Monday' do
|
53
|
+
it 'should print week days in proper order' do
|
54
|
+
CalendarWalker::Walker.new(current_date, 1).wday_list.should eq [1, 2, 3, 4, 5, 6, 0]
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
metadata
ADDED
@@ -0,0 +1,100 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: calendar_walker
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.2
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Pavel Makarov
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-05-05 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: '10.3'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.3'
|
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'
|
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'
|
55
|
+
description: Gem which allows you to walk through months in calendar, print them in
|
56
|
+
proper order.
|
57
|
+
email:
|
58
|
+
- metus.violarium@gmail.com
|
59
|
+
executables: []
|
60
|
+
extensions: []
|
61
|
+
extra_rdoc_files: []
|
62
|
+
files:
|
63
|
+
- ".gitignore"
|
64
|
+
- ".rspec"
|
65
|
+
- ".travis.yml"
|
66
|
+
- Gemfile
|
67
|
+
- LICENSE.txt
|
68
|
+
- README.md
|
69
|
+
- Rakefile
|
70
|
+
- calendar_walker.gemspec
|
71
|
+
- lib/calendar_walker.rb
|
72
|
+
- lib/calendar_walker/version.rb
|
73
|
+
- lib/calendar_walker/walker.rb
|
74
|
+
- spec/calendar_walker/walker_spec.rb
|
75
|
+
homepage: https://github.com/violarium/calendar_walker
|
76
|
+
licenses:
|
77
|
+
- MIT
|
78
|
+
metadata: {}
|
79
|
+
post_install_message:
|
80
|
+
rdoc_options: []
|
81
|
+
require_paths:
|
82
|
+
- lib
|
83
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
84
|
+
requirements:
|
85
|
+
- - ">="
|
86
|
+
- !ruby/object:Gem::Version
|
87
|
+
version: 1.9.3
|
88
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
89
|
+
requirements:
|
90
|
+
- - ">="
|
91
|
+
- !ruby/object:Gem::Version
|
92
|
+
version: '0'
|
93
|
+
requirements: []
|
94
|
+
rubyforge_project:
|
95
|
+
rubygems_version: 2.2.2
|
96
|
+
signing_key:
|
97
|
+
specification_version: 4
|
98
|
+
summary: Simple gem to walk through calendar months.
|
99
|
+
test_files:
|
100
|
+
- spec/calendar_walker/walker_spec.rb
|