koyomi 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +20 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +29 -0
- data/Rakefile +9 -0
- data/koyomi.gemspec +24 -0
- data/lib/koyomi.rb +11 -0
- data/lib/koyomi/calendar.rb +126 -0
- data/lib/koyomi/init.rb +8 -0
- data/lib/koyomi/month.rb +85 -0
- data/lib/koyomi/period.rb +33 -0
- data/lib/koyomi/version.rb +5 -0
- data/test/init.rb +7 -0
- data/test/units.rb +4 -0
- data/test/units/test_calendar.rb +78 -0
- data/test/units/test_month.rb +23 -0
- metadata +82 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 sekizo
|
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,29 @@
|
|
1
|
+
# Koyomi
|
2
|
+
|
3
|
+
TODO: Write a gem description
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'koyomi'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install koyomi
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
TODO: Write usage instructions here
|
22
|
+
|
23
|
+
## Contributing
|
24
|
+
|
25
|
+
1. Fork it
|
26
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
27
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
28
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
29
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
data/koyomi.gemspec
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'koyomi/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |gem|
|
7
|
+
gem.name = "koyomi"
|
8
|
+
gem.version = Koyomi::VERSION
|
9
|
+
gem.authors = ["sekizo"]
|
10
|
+
gem.email = ["sekizoworld@mac.com"]
|
11
|
+
gem.description = %q{Extends Date class to handling with calendar.}
|
12
|
+
gem.summary = %q{Add some classes to handle year, month, period.}
|
13
|
+
gem.homepage = ""
|
14
|
+
|
15
|
+
gem.files = `git ls-files`.split($/)
|
16
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
17
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
18
|
+
gem.require_paths = ["lib"]
|
19
|
+
|
20
|
+
#--------------------#
|
21
|
+
# additionals
|
22
|
+
gem.add_development_dependency "shoulda"
|
23
|
+
|
24
|
+
end
|
data/lib/koyomi.rb
ADDED
@@ -0,0 +1,126 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require "koyomi/period"
|
4
|
+
require "koyomi/month"
|
5
|
+
|
6
|
+
class Koyomi::Calendar < Koyomi::Period
|
7
|
+
#--------------------#
|
8
|
+
# constant
|
9
|
+
DEFAULT_WEEK_START = :mon
|
10
|
+
WEEK_START_RANGE = (0..6)
|
11
|
+
WEEK_START_STRING = [:sun, :mon, :tue, :wed, :thu, :fri, :sat]
|
12
|
+
|
13
|
+
#--------------------#
|
14
|
+
# class methods
|
15
|
+
|
16
|
+
# week index
|
17
|
+
#
|
18
|
+
# @param [Object] value
|
19
|
+
# @return [Integer]
|
20
|
+
def self.windex(value)
|
21
|
+
case value
|
22
|
+
when Numeric
|
23
|
+
index = value
|
24
|
+
when Date
|
25
|
+
index = value.wday
|
26
|
+
when String, Symbol
|
27
|
+
value = value.to_s.downcase[0, 3].to_sym
|
28
|
+
raise "Range invalid, required #{WEEK_START_STRING}." unless WEEK_START_STRING.include?(value)
|
29
|
+
index = WEEK_START_STRING.index(value)
|
30
|
+
else
|
31
|
+
index = value.to_s.to_i
|
32
|
+
end
|
33
|
+
raise "Range overflow, required (#{WEEK_START_RANGE})." unless WEEK_START_RANGE.cover?(index)
|
34
|
+
index
|
35
|
+
end
|
36
|
+
|
37
|
+
# week label
|
38
|
+
#
|
39
|
+
# @param [Object] value
|
40
|
+
# @return [Symbol]
|
41
|
+
def self.wlabel(value)
|
42
|
+
WEEK_START_STRING.at(self.windex(value))
|
43
|
+
end
|
44
|
+
|
45
|
+
#--------------------#
|
46
|
+
# instance methods
|
47
|
+
attr_reader :year, :month, :koyomi_month
|
48
|
+
attr_accessor :week_start
|
49
|
+
|
50
|
+
# initialize instance
|
51
|
+
#
|
52
|
+
# @param [Integer] year optional, use instance create date.
|
53
|
+
# @param [Integer] month optional, use instance create date.
|
54
|
+
# @param [Object] week_start weekday which week starts with. optional, use DEFAULT_WEEK_START.
|
55
|
+
def initialize(year = nil, month = nil, week_start = nil)
|
56
|
+
super()
|
57
|
+
self.year = year||self.created_at.year
|
58
|
+
self.month = month||self.created_at.month
|
59
|
+
self.week_start = week_start||DEFAULT_WEEK_START
|
60
|
+
|
61
|
+
self.koyomi_month = Koyomi::Month.new(self.month, self.year)
|
62
|
+
end
|
63
|
+
|
64
|
+
# set week_start
|
65
|
+
#
|
66
|
+
# @param [Object] value
|
67
|
+
def week_start=(value)
|
68
|
+
@week_start = self.class.windex(value)
|
69
|
+
end
|
70
|
+
|
71
|
+
# first date of the calendar (NOT first date of the MONTH)
|
72
|
+
#
|
73
|
+
# @return [Date]
|
74
|
+
def first
|
75
|
+
week_starts(self.koyomi_month.first, self.week_start)
|
76
|
+
end
|
77
|
+
|
78
|
+
# last date of the calendar (NOT last date of the MONTH)
|
79
|
+
#
|
80
|
+
# @return [Date]
|
81
|
+
def last
|
82
|
+
week_ends(self.koyomi_month.last, self.week_start)
|
83
|
+
end
|
84
|
+
|
85
|
+
# range of the calendar.
|
86
|
+
#
|
87
|
+
# @return [Range]
|
88
|
+
def range
|
89
|
+
Range.new(self.first, self.last)
|
90
|
+
end
|
91
|
+
|
92
|
+
# Koyomi::Month of the calendar's month.
|
93
|
+
#
|
94
|
+
# @return [Koyomi::Month]
|
95
|
+
def the_month
|
96
|
+
self.koyomi_month
|
97
|
+
end
|
98
|
+
|
99
|
+
#--------------------#
|
100
|
+
protected
|
101
|
+
|
102
|
+
attr_writer :year, :month, :koyomi_month
|
103
|
+
|
104
|
+
#--------------------#
|
105
|
+
private
|
106
|
+
|
107
|
+
# week start date
|
108
|
+
#
|
109
|
+
# @param [Date] date
|
110
|
+
# @param [Object] week_start
|
111
|
+
# @return [Date]
|
112
|
+
def week_starts(date, week_start = nil)
|
113
|
+
week_start ||= self.week_start
|
114
|
+
diff = date.wday - self.class.windex(week_start)
|
115
|
+
date - diff - (diff < 0 ? 7 : 0)
|
116
|
+
end
|
117
|
+
|
118
|
+
# week end date
|
119
|
+
#
|
120
|
+
# @param [Date] date
|
121
|
+
# @param [Object] week_start
|
122
|
+
# @return [Date]
|
123
|
+
def week_ends(date, week_start = nil)
|
124
|
+
week_starts(date, week_start) + 6
|
125
|
+
end
|
126
|
+
end
|
data/lib/koyomi/init.rb
ADDED
data/lib/koyomi/month.rb
ADDED
@@ -0,0 +1,85 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require "koyomi/period"
|
4
|
+
|
5
|
+
class Koyomi::Month < Koyomi::Period
|
6
|
+
|
7
|
+
#--------------------#
|
8
|
+
# class methods
|
9
|
+
|
10
|
+
# create instance from date
|
11
|
+
#
|
12
|
+
# @param [Date] date
|
13
|
+
# @return [Koyomi::Month]
|
14
|
+
def self.of(date)
|
15
|
+
self.new(date.month, date.year)
|
16
|
+
end
|
17
|
+
|
18
|
+
#--------------------#
|
19
|
+
# instance methods
|
20
|
+
|
21
|
+
attr_reader :year, :month
|
22
|
+
attr_reader :first, :last
|
23
|
+
|
24
|
+
# initialize instance.
|
25
|
+
#
|
26
|
+
# @param [Integer] month optional, default use the month of instance created.
|
27
|
+
# @param [Integer] year optional, default use the year of instance created.
|
28
|
+
def initialize(month = nil, year = nil)
|
29
|
+
super()
|
30
|
+
self.range = self.initialize_range(month, year)
|
31
|
+
end
|
32
|
+
|
33
|
+
# next month
|
34
|
+
#
|
35
|
+
# @return [Koyomi::Month]
|
36
|
+
def next
|
37
|
+
self.class.of(self.last + 1)
|
38
|
+
end
|
39
|
+
|
40
|
+
# previous month
|
41
|
+
#
|
42
|
+
# @return [Koyomi::Month]
|
43
|
+
def prev
|
44
|
+
self.class.of(self.first - 1)
|
45
|
+
end
|
46
|
+
|
47
|
+
#--------------------#
|
48
|
+
protected
|
49
|
+
|
50
|
+
attr_writer :year, :month
|
51
|
+
attr_writer :first, :last
|
52
|
+
|
53
|
+
# initialize range
|
54
|
+
#
|
55
|
+
# @param [Integer] month optional, default use the month of instance created.
|
56
|
+
# @param [Integer] year optional, default use the year of instance created.
|
57
|
+
# @return [Range]
|
58
|
+
def initialize_range(month = nil, year = nil)
|
59
|
+
self.year = year||self.created_at.year
|
60
|
+
self.month = month||self.created_at.month
|
61
|
+
self.first = Date.new(self.year, self.month, 1)
|
62
|
+
self.last = (first_date_next_month(self.first) - 1)
|
63
|
+
Range.new(self.first, self.last)
|
64
|
+
end
|
65
|
+
|
66
|
+
#--------------------#
|
67
|
+
private
|
68
|
+
|
69
|
+
# a date next month of the date
|
70
|
+
#
|
71
|
+
# @param [Date] date
|
72
|
+
# @return [Date]
|
73
|
+
def a_date_next_month(date)
|
74
|
+
Date.new(date.year, date.month, 1) + 32
|
75
|
+
end
|
76
|
+
|
77
|
+
# first date of next month
|
78
|
+
#
|
79
|
+
# @param [Date] date
|
80
|
+
# @return [Date]
|
81
|
+
def first_date_next_month(date)
|
82
|
+
a_date = a_date_next_month(date)
|
83
|
+
Date.new(a_date.year, a_date.month, 1)
|
84
|
+
end
|
85
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require "date"
|
4
|
+
|
5
|
+
class Koyomi::Period
|
6
|
+
attr_reader :range
|
7
|
+
|
8
|
+
#--------------------#
|
9
|
+
# instance methods
|
10
|
+
|
11
|
+
def initialize
|
12
|
+
super()
|
13
|
+
self.created_at = Date.today
|
14
|
+
end
|
15
|
+
|
16
|
+
#--------------------#
|
17
|
+
protected
|
18
|
+
|
19
|
+
attr_accessor :created_at
|
20
|
+
attr_writer :range
|
21
|
+
|
22
|
+
#--------------------#
|
23
|
+
private
|
24
|
+
|
25
|
+
# throw uniplemeted method to self range.
|
26
|
+
def method_missing(name, *args, &block)
|
27
|
+
begin
|
28
|
+
self.range.__send__(name, *args, &block)
|
29
|
+
rescue => e
|
30
|
+
super
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
data/test/init.rb
ADDED
data/test/units.rb
ADDED
@@ -0,0 +1,78 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
class TestKoyomiCalendar < Test::Unit::TestCase
|
4
|
+
context "Koyomi::Calendar" do
|
5
|
+
setup do
|
6
|
+
@calendar = Koyomi::Calendar.new()
|
7
|
+
end # setup
|
8
|
+
|
9
|
+
should "have koyomi month" do
|
10
|
+
assert(@calendar.koyomi_month.is_a?(Koyomi::Month))
|
11
|
+
assert(@calendar.the_month.is_a?(Koyomi::Month))
|
12
|
+
end
|
13
|
+
|
14
|
+
should "have week start" do
|
15
|
+
assert(@calendar.week_start.is_a?(Numeric))
|
16
|
+
|
17
|
+
assert_nothing_raised(Exception) do
|
18
|
+
(0..6).each do |wd|
|
19
|
+
@calendar.week_start = wd
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
assert_raise(RuntimeError) do
|
24
|
+
@calendar.week_start = 7
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
context "handle correct week" do
|
29
|
+
setup do
|
30
|
+
@cal = Koyomi::Calendar.new(2012, 12)
|
31
|
+
end # setup
|
32
|
+
|
33
|
+
should "starts sun." do
|
34
|
+
@cal.week_start = :sun
|
35
|
+
assert_equal(Date.new(2012, 11, 25), @cal.first, "wrong first.")
|
36
|
+
assert_equal(Date.new(2013, 1, 5), @cal.last, "wrong last.")
|
37
|
+
end
|
38
|
+
|
39
|
+
should "starts mon." do
|
40
|
+
@cal.week_start = :mon
|
41
|
+
assert_equal(Date.new(2012, 11, 26), @cal.first, "wrong first.")
|
42
|
+
assert_equal(Date.new(2013, 1, 6), @cal.last, "wrong last.")
|
43
|
+
end
|
44
|
+
|
45
|
+
should "starts tue." do
|
46
|
+
@cal.week_start = :tue
|
47
|
+
assert_equal(Date.new(2012, 11, 27), @cal.first, "wrong first.")
|
48
|
+
assert_equal(Date.new(2012, 12, 31), @cal.last, "wrong last.")
|
49
|
+
end
|
50
|
+
|
51
|
+
should "starts wed." do
|
52
|
+
@cal.week_start = :wed
|
53
|
+
assert_equal(Date.new(2012, 11, 28), @cal.first, "wrong first.")
|
54
|
+
assert_equal(Date.new(2013, 1, 1), @cal.last, "wrong last.")
|
55
|
+
end
|
56
|
+
|
57
|
+
should "starts thu." do
|
58
|
+
@cal.week_start = :thu
|
59
|
+
assert_equal(Date.new(2012, 11, 29), @cal.first, "wrong first.")
|
60
|
+
assert_equal(Date.new(2013, 1, 2), @cal.last, "wrong last.")
|
61
|
+
end
|
62
|
+
|
63
|
+
should "starts fri." do
|
64
|
+
@cal.week_start = :fri
|
65
|
+
assert_equal(Date.new(2012, 11, 30), @cal.first, "wrong first.")
|
66
|
+
assert_equal(Date.new(2013, 1, 3), @cal.last, "wrong last.")
|
67
|
+
end
|
68
|
+
|
69
|
+
should "starts sat." do
|
70
|
+
@cal.week_start = :sat
|
71
|
+
assert_equal(Date.new(2012, 12, 1), @cal.first, "wrong first.")
|
72
|
+
assert_equal(Date.new(2013, 1, 4), @cal.last, "wrong last.")
|
73
|
+
end
|
74
|
+
|
75
|
+
end # context "handle correct week"
|
76
|
+
|
77
|
+
end # context "Koyomi::Calendar"
|
78
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
class TestKoyomiMonth < Test::Unit::TestCase
|
4
|
+
context "Koyomi::Month" do
|
5
|
+
|
6
|
+
setup do
|
7
|
+
@month = Koyomi::Month.new()
|
8
|
+
end
|
9
|
+
|
10
|
+
should "have range" do
|
11
|
+
assert(@month.range.is_a?(Range))
|
12
|
+
end
|
13
|
+
|
14
|
+
should "respond to next" do
|
15
|
+
assert(@month.next.is_a?(Koyomi::Month))
|
16
|
+
end
|
17
|
+
|
18
|
+
should "respond to prev" do
|
19
|
+
assert(@month.prev.is_a?(Koyomi::Month))
|
20
|
+
end
|
21
|
+
|
22
|
+
end # context "Koyomi::Month"
|
23
|
+
end
|
metadata
ADDED
@@ -0,0 +1,82 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: koyomi
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- sekizo
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-12-16 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: shoulda
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '0'
|
30
|
+
description: Extends Date class to handling with calendar.
|
31
|
+
email:
|
32
|
+
- sekizoworld@mac.com
|
33
|
+
executables: []
|
34
|
+
extensions: []
|
35
|
+
extra_rdoc_files: []
|
36
|
+
files:
|
37
|
+
- .gitignore
|
38
|
+
- Gemfile
|
39
|
+
- LICENSE.txt
|
40
|
+
- README.md
|
41
|
+
- Rakefile
|
42
|
+
- koyomi.gemspec
|
43
|
+
- lib/koyomi.rb
|
44
|
+
- lib/koyomi/calendar.rb
|
45
|
+
- lib/koyomi/init.rb
|
46
|
+
- lib/koyomi/month.rb
|
47
|
+
- lib/koyomi/period.rb
|
48
|
+
- lib/koyomi/version.rb
|
49
|
+
- test/init.rb
|
50
|
+
- test/units.rb
|
51
|
+
- test/units/test_calendar.rb
|
52
|
+
- test/units/test_month.rb
|
53
|
+
homepage: ''
|
54
|
+
licenses: []
|
55
|
+
post_install_message:
|
56
|
+
rdoc_options: []
|
57
|
+
require_paths:
|
58
|
+
- lib
|
59
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
60
|
+
none: false
|
61
|
+
requirements:
|
62
|
+
- - ! '>='
|
63
|
+
- !ruby/object:Gem::Version
|
64
|
+
version: '0'
|
65
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
66
|
+
none: false
|
67
|
+
requirements:
|
68
|
+
- - ! '>='
|
69
|
+
- !ruby/object:Gem::Version
|
70
|
+
version: '0'
|
71
|
+
requirements: []
|
72
|
+
rubyforge_project:
|
73
|
+
rubygems_version: 1.8.24
|
74
|
+
signing_key:
|
75
|
+
specification_version: 3
|
76
|
+
summary: Add some classes to handle year, month, period.
|
77
|
+
test_files:
|
78
|
+
- test/init.rb
|
79
|
+
- test/units.rb
|
80
|
+
- test/units/test_calendar.rb
|
81
|
+
- test/units/test_month.rb
|
82
|
+
has_rdoc:
|