koyomi 0.0.1 → 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.
data/lib/koyomi.rb CHANGED
@@ -6,6 +6,7 @@ require "koyomi/version"
6
6
  require "koyomi/period"
7
7
  require "koyomi/month"
8
8
  require "koyomi/calendar"
9
+ require "koyomi/helper"
9
10
 
10
11
  module Koyomi
11
12
  end
@@ -7,12 +7,22 @@ class Koyomi::Calendar < Koyomi::Period
7
7
  #--------------------#
8
8
  # constant
9
9
  DEFAULT_WEEK_START = :mon
10
+ WEEK_DAYS = 7
10
11
  WEEK_START_RANGE = (0..6)
11
12
  WEEK_START_STRING = [:sun, :mon, :tue, :wed, :thu, :fri, :sat]
12
13
 
13
14
  #--------------------#
14
15
  # class methods
15
16
 
17
+ # create Koyomi::Calendar instance from date.
18
+ #
19
+ # @param [Date] date
20
+ # @param [Object] week_start
21
+ # @return [Koyomi::Calendar]
22
+ def self.of(date, week_start = nil)
23
+ self.new(date.year, date.month, week_start)
24
+ end
25
+
16
26
  # week index
17
27
  #
18
28
  # @param [Object] value
@@ -112,7 +122,7 @@ class Koyomi::Calendar < Koyomi::Period
112
122
  def week_starts(date, week_start = nil)
113
123
  week_start ||= self.week_start
114
124
  diff = date.wday - self.class.windex(week_start)
115
- date - diff - (diff < 0 ? 7 : 0)
125
+ date - diff - (diff < 0 ? WEEK_DAYS : 0)
116
126
  end
117
127
 
118
128
  # week end date
@@ -121,6 +131,6 @@ class Koyomi::Calendar < Koyomi::Period
121
131
  # @param [Object] week_start
122
132
  # @return [Date]
123
133
  def week_ends(date, week_start = nil)
124
- week_starts(date, week_start) + 6
134
+ week_starts(date, week_start) + WEEK_DAYS - 1
125
135
  end
126
136
  end
@@ -0,0 +1,8 @@
1
+ # encoding: utf-8
2
+
3
+ require "koyomi/helper/init"
4
+ require "koyomi/helper/date"
5
+
6
+ module Koyomi::Helper
7
+ end
8
+
@@ -0,0 +1,30 @@
1
+ # encoding: utf-8
2
+
3
+ require "date"
4
+ require "koyomi/calendar"
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
+ wdays = Koyomi::Calendar::WEEK_DAYS
14
+ week_start ||= Koyomi::Calendar::DEFAULT_WEEK_START
15
+ week_ends = (Koyomi::Calendar.windex(week_start) + wdays - 1) % wdays
16
+ date.wday == week_ends
17
+ end
18
+ end
19
+
20
+ class Date
21
+ include Koyomi::Helper::Date
22
+
23
+ # check week end?
24
+ #
25
+ # @param [Object] week_start
26
+ # @return [Boolean]
27
+ def week_end?(week_start = nil)
28
+ week_ends?(self, week_start)
29
+ end
30
+ end
@@ -0,0 +1,5 @@
1
+ # encoding: utf-8
2
+
3
+ # define module
4
+ module Koyomi::Helper
5
+ end
@@ -1,5 +1,5 @@
1
1
  # encoding: utf-8
2
2
 
3
3
  module Koyomi
4
- VERSION = "0.0.1"
4
+ VERSION = "0.0.2"
5
5
  end
data/test/units.rb CHANGED
@@ -2,3 +2,4 @@
2
2
 
3
3
  require File.expand_path("../units/test_month", __FILE__)
4
4
  require File.expand_path("../units/test_calendar", __FILE__)
5
+ require File.expand_path("../units/test_date", __FILE__)
@@ -0,0 +1,25 @@
1
+ # encoding: utf-8
2
+
3
+ class TestKoyomiDate < Test::Unit::TestCase
4
+ context "Koyomi::Date" do
5
+ setup do
6
+ @date = Date.new(2012, 12, 16)
7
+ @wdays = Koyomi::Calendar::WEEK_START_STRING
8
+ end # setup
9
+
10
+ should "correct week end" do
11
+ cal = Koyomi::Calendar.of(@date)
12
+
13
+ cal.each do |date|
14
+ @wdays.each do |wd|
15
+ if (date + 1).wday == @wdays.index(wd)
16
+ assert(date.week_end?(wd))
17
+ else
18
+ assert(!date.week_end?(wd))
19
+ end
20
+ end # each wd
21
+ end # each date
22
+ end # should "correct week end"
23
+
24
+ end # context "Koyomi::Date"
25
+ 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.1
4
+ version: 0.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -42,6 +42,9 @@ files:
42
42
  - koyomi.gemspec
43
43
  - lib/koyomi.rb
44
44
  - lib/koyomi/calendar.rb
45
+ - lib/koyomi/helper.rb
46
+ - lib/koyomi/helper/date.rb
47
+ - lib/koyomi/helper/init.rb
45
48
  - lib/koyomi/init.rb
46
49
  - lib/koyomi/month.rb
47
50
  - lib/koyomi/period.rb
@@ -49,6 +52,7 @@ files:
49
52
  - test/init.rb
50
53
  - test/units.rb
51
54
  - test/units/test_calendar.rb
55
+ - test/units/test_date.rb
52
56
  - test/units/test_month.rb
53
57
  homepage: ''
54
58
  licenses: []
@@ -78,5 +82,6 @@ test_files:
78
82
  - test/init.rb
79
83
  - test/units.rb
80
84
  - test/units/test_calendar.rb
85
+ - test/units/test_date.rb
81
86
  - test/units/test_month.rb
82
87
  has_rdoc: