koyomi 0.0.2.2 → 0.0.3
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.
- data/README.md +15 -2
- data/Rakefile +16 -4
- data/koyomi.gemspec +1 -0
- data/lib/koyomi.rb +1 -0
- data/lib/koyomi/calendar.rb +4 -63
- data/lib/koyomi/helper.rb +1 -0
- data/lib/koyomi/helper/date.rb +18 -15
- data/lib/koyomi/helper/week.rb +44 -0
- data/lib/koyomi/version.rb +1 -1
- data/lib/koyomi/week.rb +139 -0
- data/test/{init.rb → test_helper.rb} +2 -3
- data/test/units/test_calendar.rb +2 -0
- data/test/units/test_date.rb +3 -1
- data/test/units/test_month.rb +2 -0
- data/test/units/test_week.rb +46 -0
- metadata +10 -7
- data/test/units.rb +0 -5
data/README.md
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
# Koyomi
|
2
2
|
|
3
|
-
|
3
|
+
Extends Date class to handling with calendar.
|
4
4
|
|
5
5
|
## Installation
|
6
6
|
|
@@ -18,7 +18,20 @@ Or install it yourself as:
|
|
18
18
|
|
19
19
|
## Usage
|
20
20
|
|
21
|
-
|
21
|
+
Handling calendar by this gem.
|
22
|
+
|
23
|
+
require "koyomi"
|
24
|
+
cal = Koyomi::Calendar.new(2012, 12, :mon)
|
25
|
+
cal.first.to_s # => "2012-11-26"
|
26
|
+
|
27
|
+
month = cal.the_month
|
28
|
+
month.first.to_s # => "2012-12-01"
|
29
|
+
|
30
|
+
week = Koyomi::Week.new(month.first, :tue)
|
31
|
+
week.first.to_s # => "2012-11-27"
|
32
|
+
|
33
|
+
cal.first.week_end? # => false
|
34
|
+
cal.first.week_end?(:tue) # => true
|
22
35
|
|
23
36
|
## Contributing
|
24
37
|
|
data/Rakefile
CHANGED
@@ -1,9 +1,21 @@
|
|
1
|
+
# encoding: utf-8
|
1
2
|
require "bundler/gem_tasks"
|
3
|
+
require "rake/testtask"
|
2
4
|
|
3
5
|
$: << File.expand_path("../lib", __FILE__)
|
4
6
|
|
5
|
-
|
6
|
-
|
7
|
-
require File.expand_path("../test/init", __FILE__)
|
8
|
-
require File.expand_path("../test/units", __FILE__)
|
7
|
+
task :default do
|
8
|
+
sh "rake -T"
|
9
9
|
end
|
10
|
+
|
11
|
+
Rake::TestTask.new do |t|
|
12
|
+
t.libs << "test"
|
13
|
+
t.test_files = FileList["test/**/test_*.rb"]
|
14
|
+
end
|
15
|
+
|
16
|
+
namespace :test do
|
17
|
+
Rake::TestTask.new(:units) do |t|
|
18
|
+
t.libs << "test"
|
19
|
+
t.test_files = FileList["test/units/**/test_*.rb"]
|
20
|
+
end
|
21
|
+
end
|
data/koyomi.gemspec
CHANGED
@@ -11,6 +11,7 @@ Gem::Specification.new do |gem|
|
|
11
11
|
gem.description = %q{Extends Date class to handling with calendar.}
|
12
12
|
gem.summary = %q{Add some classes to handle year, month, period.}
|
13
13
|
gem.homepage = ""
|
14
|
+
gem.license = 'MIT'
|
14
15
|
|
15
16
|
gem.files = `git ls-files`.split($/)
|
16
17
|
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
data/lib/koyomi.rb
CHANGED
data/lib/koyomi/calendar.rb
CHANGED
@@ -2,17 +2,10 @@
|
|
2
2
|
|
3
3
|
require "koyomi/period"
|
4
4
|
require "koyomi/month"
|
5
|
+
require "koyomi/helper/week"
|
5
6
|
|
6
7
|
class Koyomi::Calendar < Koyomi::Period
|
7
|
-
|
8
|
-
# constant
|
9
|
-
DEFAULT_WEEK_START = :mon
|
10
|
-
WEEK_DAYS = 7
|
11
|
-
WEEK_START_RANGE = (0..6)
|
12
|
-
WEEK_START_STRING = [:sun, :mon, :tue, :wed, :thu, :fri, :sat]
|
13
|
-
|
14
|
-
#--------------------#
|
15
|
-
# class methods
|
8
|
+
include Koyomi::Helper::Week
|
16
9
|
|
17
10
|
# create Koyomi::Calendar instance from date.
|
18
11
|
#
|
@@ -23,35 +16,6 @@ class Koyomi::Calendar < Koyomi::Period
|
|
23
16
|
self.new(date.year, date.month, week_start)
|
24
17
|
end
|
25
18
|
|
26
|
-
# week index
|
27
|
-
#
|
28
|
-
# @param [Object] value
|
29
|
-
# @return [Integer]
|
30
|
-
def self.windex(value)
|
31
|
-
case value
|
32
|
-
when Numeric
|
33
|
-
index = value
|
34
|
-
when Date
|
35
|
-
index = value.wday
|
36
|
-
when String, Symbol
|
37
|
-
value = value.to_s.downcase[0, 3].to_sym
|
38
|
-
raise "Range invalid, required #{WEEK_START_STRING}." unless WEEK_START_STRING.include?(value)
|
39
|
-
index = WEEK_START_STRING.index(value)
|
40
|
-
else
|
41
|
-
index = value.to_s.to_i
|
42
|
-
end
|
43
|
-
raise "Range overflow, required (#{WEEK_START_RANGE})." unless WEEK_START_RANGE.cover?(index)
|
44
|
-
index
|
45
|
-
end
|
46
|
-
|
47
|
-
# week label
|
48
|
-
#
|
49
|
-
# @param [Object] value
|
50
|
-
# @return [Symbol]
|
51
|
-
def self.wlabel(value)
|
52
|
-
WEEK_START_STRING.at(self.windex(value))
|
53
|
-
end
|
54
|
-
|
55
19
|
#--------------------#
|
56
20
|
# instance methods
|
57
21
|
attr_reader :year, :month, :koyomi_month
|
@@ -82,14 +46,14 @@ class Koyomi::Calendar < Koyomi::Period
|
|
82
46
|
#
|
83
47
|
# @return [Date]
|
84
48
|
def first
|
85
|
-
|
49
|
+
Koyomi::Week.new(self.koyomi_month.first, self.week_start).first
|
86
50
|
end
|
87
51
|
|
88
52
|
# last date of the calendar (NOT last date of the MONTH)
|
89
53
|
#
|
90
54
|
# @return [Date]
|
91
55
|
def last
|
92
|
-
|
56
|
+
Koyomi::Week.new(self.koyomi_month.last, self.week_start).last
|
93
57
|
end
|
94
58
|
|
95
59
|
# range of the calendar.
|
@@ -110,27 +74,4 @@ class Koyomi::Calendar < Koyomi::Period
|
|
110
74
|
protected
|
111
75
|
|
112
76
|
attr_writer :year, :month, :koyomi_month
|
113
|
-
|
114
|
-
#--------------------#
|
115
|
-
private
|
116
|
-
|
117
|
-
# week start date
|
118
|
-
#
|
119
|
-
# @param [Date] date
|
120
|
-
# @param [Object] week_start
|
121
|
-
# @return [Date]
|
122
|
-
def week_starts(date, week_start = nil)
|
123
|
-
week_start ||= self.week_start
|
124
|
-
diff = date.wday - self.class.windex(week_start)
|
125
|
-
date - diff - (diff < 0 ? WEEK_DAYS : 0)
|
126
|
-
end
|
127
|
-
|
128
|
-
# week end date
|
129
|
-
#
|
130
|
-
# @param [Date] date
|
131
|
-
# @param [Object] week_start
|
132
|
-
# @return [Date]
|
133
|
-
def week_ends(date, week_start = nil)
|
134
|
-
week_starts(date, week_start) + WEEK_DAYS - 1
|
135
|
-
end
|
136
77
|
end
|
data/lib/koyomi/helper.rb
CHANGED
data/lib/koyomi/helper/date.rb
CHANGED
@@ -1,28 +1,31 @@
|
|
1
1
|
# encoding: utf-8
|
2
2
|
|
3
3
|
require "date"
|
4
|
-
require "koyomi/
|
5
|
-
|
6
|
-
module Koyomi::Helper::Date
|
7
|
-
# check week end?
|
8
|
-
#
|
9
|
-
# @param [Date] date
|
10
|
-
# @param [Object] week_start
|
11
|
-
# @return [Boolean]
|
12
|
-
def week_ends?(date, week_start = nil)
|
13
|
-
week_start ||= Koyomi::Calendar::DEFAULT_WEEK_START
|
14
|
-
(date + 1).wday == Koyomi::Calendar.windex(week_start)
|
15
|
-
end
|
16
|
-
end
|
4
|
+
require "koyomi/helper/init"
|
5
|
+
require "koyomi/helper/week"
|
17
6
|
|
18
7
|
class Date
|
19
|
-
include Koyomi::Helper::
|
8
|
+
include Koyomi::Helper::Week
|
20
9
|
|
21
10
|
# check week end?
|
22
11
|
#
|
23
12
|
# @param [Object] week_start
|
24
13
|
# @return [Boolean]
|
25
14
|
def week_end?(week_start = nil)
|
26
|
-
week_ends?(self, week_start)
|
15
|
+
self.class.week_ends?(self, week_start)
|
16
|
+
end
|
17
|
+
|
18
|
+
# week day index
|
19
|
+
#
|
20
|
+
# @return [Integer]
|
21
|
+
def windex
|
22
|
+
self.class.windex(self)
|
23
|
+
end
|
24
|
+
|
25
|
+
# week day name
|
26
|
+
#
|
27
|
+
# @return [Symbol]
|
28
|
+
def wday_name
|
29
|
+
self.class.wday_name(self)
|
27
30
|
end
|
28
31
|
end
|
@@ -0,0 +1,44 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require "koyomi/helper/init"
|
4
|
+
require "koyomi/week"
|
5
|
+
|
6
|
+
module Koyomi::Helper::Week
|
7
|
+
DEFAULT_WEEK_START = Koyomi::Week::DEFAULT_START
|
8
|
+
WEEK_WDAYS = Koyomi::Week::WDAYS
|
9
|
+
WEEK_DAYS = Koyomi::Week::DAYS
|
10
|
+
WEEK_START_RANGE = Koyomi::Week::START_RANGE
|
11
|
+
|
12
|
+
def self.included(mod)
|
13
|
+
mod.class_eval do
|
14
|
+
extend Koyomi::Helper::Week::ClassMethod
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
module ClassMethod
|
19
|
+
# week index
|
20
|
+
#
|
21
|
+
# @param [Object] value
|
22
|
+
# @return [Integer]
|
23
|
+
def windex(value)
|
24
|
+
Koyomi::Week.windex(value)
|
25
|
+
end
|
26
|
+
|
27
|
+
# week day name
|
28
|
+
#
|
29
|
+
# @param [Object] value
|
30
|
+
# @return [Symbol]
|
31
|
+
def wday_name(value)
|
32
|
+
Koyomi::Week.wday_name(value)
|
33
|
+
end
|
34
|
+
|
35
|
+
# week end?
|
36
|
+
#
|
37
|
+
# @param [Date] date
|
38
|
+
# @param [Object] week_start week start
|
39
|
+
# @return [Boolean]
|
40
|
+
def week_ends?(date, week_start = nil)
|
41
|
+
Koyomi::Week.ends?(date, week_start)
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
data/lib/koyomi/version.rb
CHANGED
data/lib/koyomi/week.rb
ADDED
@@ -0,0 +1,139 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require "koyomi/period"
|
4
|
+
|
5
|
+
class Koyomi::Week < Koyomi::Period
|
6
|
+
#--------------------#
|
7
|
+
# constants
|
8
|
+
|
9
|
+
DEFAULT_START = :mon
|
10
|
+
WDAYS = [:sun, :mon, :tue, :wed, :thu, :fri, :sat]
|
11
|
+
DAYS = WDAYS.size
|
12
|
+
START_RANGE = (0..(DAYS - 1))
|
13
|
+
|
14
|
+
#--------------------#
|
15
|
+
# attributes
|
16
|
+
|
17
|
+
#--------------------#
|
18
|
+
# class methods
|
19
|
+
|
20
|
+
# week index
|
21
|
+
#
|
22
|
+
# @param [Object] value
|
23
|
+
# @return [Integer]
|
24
|
+
def self.windex(value)
|
25
|
+
case value
|
26
|
+
when Numeric
|
27
|
+
index = value
|
28
|
+
when Date
|
29
|
+
index = value.wday
|
30
|
+
when String, Symbol
|
31
|
+
value = value.to_s.downcase[0, 3].to_sym
|
32
|
+
raise "Range invalid, required #{WDAYS}." unless WDAYS.include?(value)
|
33
|
+
index = WDAYS.index(value)
|
34
|
+
else
|
35
|
+
index = value.to_s.to_i
|
36
|
+
end
|
37
|
+
raise "Range overflow, required (#{START_RANGE})." unless START_RANGE.cover?(index)
|
38
|
+
index
|
39
|
+
end
|
40
|
+
|
41
|
+
# week day name
|
42
|
+
#
|
43
|
+
# @param [Object] value
|
44
|
+
# @return [Symbol]
|
45
|
+
def self.wday_name(value)
|
46
|
+
WDAYS.at(self.windex(value))
|
47
|
+
end
|
48
|
+
|
49
|
+
# week starts?
|
50
|
+
#
|
51
|
+
# @param [Date] date
|
52
|
+
# @param [Object] start_wday week start
|
53
|
+
# @return [Boolean]
|
54
|
+
def self.starts?(date, start_wday = nil)
|
55
|
+
start_wday ||= DEFAULT_START
|
56
|
+
(date).wday == self.windex(start_wday)
|
57
|
+
end
|
58
|
+
|
59
|
+
# week end?
|
60
|
+
#
|
61
|
+
# @param [Date] date
|
62
|
+
# @param [Object] start_wday week start
|
63
|
+
# @return [Boolean]
|
64
|
+
def self.ends?(date, start_wday = nil)
|
65
|
+
start_wday ||= DEFAULT_START
|
66
|
+
(date + 1).wday == self.windex(start_wday)
|
67
|
+
end
|
68
|
+
|
69
|
+
#--------------------#
|
70
|
+
# instance methods
|
71
|
+
|
72
|
+
# initialize method
|
73
|
+
#
|
74
|
+
# @param [Date] date optional, default use Date.today.
|
75
|
+
# @param [Object] start_wday optionail, default use Koyomi::Week::DEFAULT_START
|
76
|
+
def initialize(date = nil, start_wday = nil)
|
77
|
+
super()
|
78
|
+
self.date = date||self.created_at
|
79
|
+
self.start_wday = start_wday||DEFAULT_START
|
80
|
+
end
|
81
|
+
|
82
|
+
# sepified week day
|
83
|
+
#
|
84
|
+
# @param [Object] wday_name
|
85
|
+
# @return [Date]
|
86
|
+
def wday(wday_name)
|
87
|
+
diff = self.class.windex(wday_name) - self.class.windex(self.start_wday)
|
88
|
+
factor = diff + ((diff < 0) ? DAYS : 0)
|
89
|
+
self.range.first + factor
|
90
|
+
end
|
91
|
+
|
92
|
+
# start date
|
93
|
+
#
|
94
|
+
# @return [Date] date
|
95
|
+
def starts
|
96
|
+
self.range.first
|
97
|
+
end
|
98
|
+
|
99
|
+
# end date
|
100
|
+
#
|
101
|
+
# @return [Date] date
|
102
|
+
def ends
|
103
|
+
self.range.last
|
104
|
+
end
|
105
|
+
|
106
|
+
#--------------------#
|
107
|
+
protected
|
108
|
+
|
109
|
+
attr_accessor :date
|
110
|
+
attr_reader :start_wday
|
111
|
+
|
112
|
+
# set week starts
|
113
|
+
#
|
114
|
+
# @param [Object] value
|
115
|
+
def start_wday=(value)
|
116
|
+
@start_wday = value
|
117
|
+
self.setup_range
|
118
|
+
@start_wday
|
119
|
+
end
|
120
|
+
|
121
|
+
# setup week range with given week start
|
122
|
+
def setup_range
|
123
|
+
diff = self.date.wday - self.class.windex(self.start_wday)
|
124
|
+
starts = self.date - (diff + ((diff < 0) ? DAYS : 0))
|
125
|
+
@range = Range.new(starts, starts + DAYS - 1)
|
126
|
+
end
|
127
|
+
|
128
|
+
#--------------------#
|
129
|
+
private
|
130
|
+
|
131
|
+
def method_missing(name, *args, &block)
|
132
|
+
case
|
133
|
+
when WDAYS.include?(name.to_s.to_sym)
|
134
|
+
self.wday(name,*args, &block)
|
135
|
+
else
|
136
|
+
super
|
137
|
+
end
|
138
|
+
end
|
139
|
+
end
|
data/test/units/test_calendar.rb
CHANGED
data/test/units/test_date.rb
CHANGED
@@ -1,10 +1,12 @@
|
|
1
1
|
# encoding: utf-8
|
2
2
|
|
3
|
+
require "test_helper"
|
4
|
+
|
3
5
|
class TestKoyomiDate < Test::Unit::TestCase
|
4
6
|
context "Koyomi::Date" do
|
5
7
|
setup do
|
6
8
|
@date = Date.new(2012, 12, 16)
|
7
|
-
@wdays =
|
9
|
+
@wdays = Date::WEEK_WDAYS
|
8
10
|
end # setup
|
9
11
|
|
10
12
|
should "correct week end" do
|
data/test/units/test_month.rb
CHANGED
@@ -0,0 +1,46 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require "test_helper"
|
4
|
+
|
5
|
+
class TestKoyomiWeek < Test::Unit::TestCase
|
6
|
+
context "Koyomi::Week" do
|
7
|
+
|
8
|
+
setup do
|
9
|
+
@today = Date.today
|
10
|
+
@year = @today.year
|
11
|
+
@month = @today.month
|
12
|
+
end # setup
|
13
|
+
|
14
|
+
should "generate week instance" do
|
15
|
+
assert_nothing_raised(Exception) do
|
16
|
+
@week = Koyomi::Week.new(@today, Koyomi::Week::DEFAULT_START)
|
17
|
+
end
|
18
|
+
end # should "generate week instance"
|
19
|
+
|
20
|
+
should "calcurate windex" do
|
21
|
+
date = Date.new(2012, 12, 23)
|
22
|
+
assert(Koyomi::Week.windex(:sun) == (date).wday)
|
23
|
+
assert(Koyomi::Week.windex(:mon) == (date + 1).wday)
|
24
|
+
assert(Koyomi::Week.windex(:tue) == (date + 2).wday)
|
25
|
+
assert(Koyomi::Week.windex(:wed) == (date + 3).wday)
|
26
|
+
assert(Koyomi::Week.windex(:thu) == (date + 4).wday)
|
27
|
+
assert(Koyomi::Week.windex(:fri) == (date + 5).wday)
|
28
|
+
assert(Koyomi::Week.windex(:sat) == (date + 6).wday)
|
29
|
+
end
|
30
|
+
|
31
|
+
should "start specified week day" do
|
32
|
+
date = Date.new(2012, 12, 23)
|
33
|
+
Koyomi::Week::WDAYS.each do |wday|
|
34
|
+
assert_equal(Koyomi::Week.windex(wday), Koyomi::Week.new(date, wday).range.first.wday)
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
should "respond date to request of week day" do
|
39
|
+
date = Date.new(2012, 12, 23)
|
40
|
+
Koyomi::Week::WDAYS.each do |wday|
|
41
|
+
assert_equal(Koyomi::Week.windex(wday), Koyomi::Week.new(date, wday).__send__(wday).wday)
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
end # context "Koyomi::Week"
|
46
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: koyomi
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-12-
|
12
|
+
date: 2012-12-23 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: shoulda
|
@@ -45,17 +45,20 @@ files:
|
|
45
45
|
- lib/koyomi/helper.rb
|
46
46
|
- lib/koyomi/helper/date.rb
|
47
47
|
- lib/koyomi/helper/init.rb
|
48
|
+
- lib/koyomi/helper/week.rb
|
48
49
|
- lib/koyomi/init.rb
|
49
50
|
- lib/koyomi/month.rb
|
50
51
|
- lib/koyomi/period.rb
|
51
52
|
- lib/koyomi/version.rb
|
52
|
-
-
|
53
|
-
- test/
|
53
|
+
- lib/koyomi/week.rb
|
54
|
+
- test/test_helper.rb
|
54
55
|
- test/units/test_calendar.rb
|
55
56
|
- test/units/test_date.rb
|
56
57
|
- test/units/test_month.rb
|
58
|
+
- test/units/test_week.rb
|
57
59
|
homepage: ''
|
58
|
-
licenses:
|
60
|
+
licenses:
|
61
|
+
- MIT
|
59
62
|
post_install_message:
|
60
63
|
rdoc_options: []
|
61
64
|
require_paths:
|
@@ -79,9 +82,9 @@ signing_key:
|
|
79
82
|
specification_version: 3
|
80
83
|
summary: Add some classes to handle year, month, period.
|
81
84
|
test_files:
|
82
|
-
- test/
|
83
|
-
- test/units.rb
|
85
|
+
- test/test_helper.rb
|
84
86
|
- test/units/test_calendar.rb
|
85
87
|
- test/units/test_date.rb
|
86
88
|
- test/units/test_month.rb
|
89
|
+
- test/units/test_week.rb
|
87
90
|
has_rdoc:
|