koyomi 0.0.3 → 0.0.4

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 CHANGED
@@ -19,19 +19,36 @@ Or install it yourself as:
19
19
  ## Usage
20
20
 
21
21
  Handling calendar by this gem.
22
+ (japanese) カレンダーを取り扱うための gem です。
22
23
 
23
24
  require "koyomi"
24
25
  cal = Koyomi::Calendar.new(2012, 12, :mon)
25
26
  cal.first.to_s # => "2012-11-26"
26
27
 
28
+ cal.first.week_end? # => false
29
+ cal.first.week_end?(:tue) # => true
30
+
27
31
  month = cal.the_month
28
32
  month.first.to_s # => "2012-12-01"
29
33
 
30
34
  week = Koyomi::Week.new(month.first, :tue)
31
35
  week.first.to_s # => "2012-11-27"
32
36
 
33
- cal.first.week_end? # => false
34
- cal.first.week_end?(:tue) # => true
37
+ # weeks and week days.
38
+
39
+ # nth week day.
40
+ cal.nth_wday(1, :sat).to_s # => "2012-12-01"
41
+
42
+ # cycle: 1st, 3rd week's tuesday or friday.
43
+ # (japanese) 周期:第1、第3の火曜と金曜
44
+ cal.cycles([1, 3], [:tue, :fri]).collect { |d| d.to_s }
45
+ # => ["2012-12-04", "2012-12-07", "2012-12-18", "2012-12-21"]
46
+
47
+ # cycle: every montday.
48
+ # (japanese) 周期:毎週月曜
49
+ cal.cycles(:every, [:mon]).collect { |d| d.to_s }
50
+ # => ["2012-12-03", "2012-12-10", "2012-12-17", "2012-12-24", "2012-12-31"]
51
+
35
52
 
36
53
  ## Contributing
37
54
 
@@ -70,6 +70,53 @@ class Koyomi::Calendar < Koyomi::Period
70
70
  self.koyomi_month
71
71
  end
72
72
 
73
+ # weeks of the calendar.
74
+ #
75
+ # @return [Array<Week>]
76
+ def weeks
77
+ a_date = self.first
78
+ the_last = self.last
79
+ weeks = []
80
+
81
+ while (a_date < the_last)
82
+ weeks << Koyomi::Week.new(a_date, self.week_start)
83
+ a_date += WEEK_DAYS
84
+ end
85
+ weeks
86
+ end
87
+
88
+ # week day of nth week.
89
+ #
90
+ # @param [Integer] nth
91
+ # @param [Object] wday_name
92
+ # @return [Date]
93
+ def nth_wday(nth, wday_name)
94
+ self.koyomi_month.nth_wday(nth, wday_name)
95
+ end
96
+
97
+ # week days
98
+ #
99
+ # @param [Object] wday_name
100
+ # @return [Array<Date>]
101
+ def wdays(wday_name)
102
+ _wdays = []
103
+ a_date = self.nth_wday(1, wday_name)
104
+ while ((a_date.month == self.month))
105
+ _wdays << a_date
106
+ a_date += WEEK_DAYS
107
+ end
108
+ _wdays
109
+ end
110
+
111
+ # cycle dates
112
+ #
113
+ # @param [Array<Integer>|Integer] weeks
114
+ # @param [Array<Object>|Object] wdays
115
+ # @return [Array<Date>]
116
+ def cycles(weeks, wdays)
117
+ self.koyomi_month.cycles(weeks, wdays)
118
+ end
119
+
73
120
  #--------------------#
74
121
  protected
75
122
 
data/lib/koyomi/month.rb CHANGED
@@ -44,6 +44,32 @@ class Koyomi::Month < Koyomi::Period
44
44
  self.class.of(self.first - 1)
45
45
  end
46
46
 
47
+ # week days of nth weeks.
48
+ #
49
+ # @param [Integer|Array<Integer>] nth
50
+ # @param [Object|Array<Object>] wday_name
51
+ # @return [Array<Date>]
52
+ def cycles(weeks, wdays)
53
+ _dates = []
54
+ cycle_weeks_filter(weeks).each do |n|
55
+ [wdays].flatten.each do |w|
56
+ a_date = self.nth_wday(n, w)
57
+ _dates << a_date if a_date.month == self.month
58
+ end
59
+ end
60
+ _dates.sort
61
+ end
62
+
63
+ # week day of nth week.
64
+ #
65
+ # @param [Integer] nth
66
+ # @param [Object] wday_name
67
+ # @return [Date]
68
+ def nth_wday(nth, wday_name)
69
+ a_date = Date.new(self.year, self.month, 1) + ((nth - 1) * Koyomi::Week::DAYS)
70
+ Koyomi::Week.new(a_date, a_date.wday).wday(wday_name)
71
+ end
72
+
47
73
  #--------------------#
48
74
  protected
49
75
 
@@ -66,6 +92,20 @@ class Koyomi::Month < Koyomi::Period
66
92
  #--------------------#
67
93
  private
68
94
 
95
+ # filter cycle weeks
96
+ #
97
+ # @param [Object] weeks
98
+ # @return [Array|Range]
99
+ def cycle_weeks_filter(weeks)
100
+ case
101
+ when weeks.to_s =~ /every/
102
+ _weeks = (1..max_week)
103
+ else
104
+ _weeks = [weeks].flatten
105
+ end
106
+ _weeks
107
+ end
108
+
69
109
  # a date next month of the date
70
110
  #
71
111
  # @param [Date] date
@@ -82,4 +122,13 @@ class Koyomi::Month < Koyomi::Period
82
122
  a_date = a_date_next_month(date)
83
123
  Date.new(a_date.year, a_date.month, 1)
84
124
  end
125
+
126
+ # maximumn of weeks
127
+ #
128
+ # @return [Integer]
129
+ def max_week
130
+ _weeks = (self.last.day / Koyomi::Week::DAYS).to_i
131
+ _fraction = (self.last.day % Koyomi::Week::DAYS)
132
+ (_weeks + (_fraction == 0 ? 0 : 1))
133
+ end
85
134
  end
@@ -1,5 +1,5 @@
1
1
  # encoding: utf-8
2
2
 
3
3
  module Koyomi
4
- VERSION = "0.0.3"
4
+ VERSION = "0.0.4"
5
5
  end
@@ -11,7 +11,7 @@ class TestKoyomiCalendar < Test::Unit::TestCase
11
11
  should "have koyomi month" do
12
12
  assert(@calendar.koyomi_month.is_a?(Koyomi::Month))
13
13
  assert(@calendar.the_month.is_a?(Koyomi::Month))
14
- end
14
+ end # should "have koyomi month"
15
15
 
16
16
  should "have week start" do
17
17
  assert(@calendar.week_start.is_a?(Numeric))
@@ -25,7 +25,7 @@ class TestKoyomiCalendar < Test::Unit::TestCase
25
25
  assert_raise(RuntimeError) do
26
26
  @calendar.week_start = 7
27
27
  end
28
- end
28
+ end # should "have week start"
29
29
 
30
30
  context "handle correct week" do
31
31
  setup do
@@ -75,6 +75,40 @@ class TestKoyomiCalendar < Test::Unit::TestCase
75
75
  end
76
76
 
77
77
  end # context "handle correct week"
78
-
78
+
79
+ context "week methods" do
80
+
81
+ setup do
82
+ @cal = Koyomi::Calendar.new(2012, 12, :mon)
83
+ end
84
+
85
+ should "have weeks" do
86
+ assert_equal(6, @cal.weeks.size)
87
+
88
+ cal = Koyomi::Calendar.new(2012, 12, :tue)
89
+ assert_equal(5, cal.weeks.size)
90
+ end # should "have weeks"
91
+
92
+ should "respond to nth_wday" do
93
+ assert_equal(Date.new(2012, 12, 1), @cal.nth_wday(1, :sat))
94
+ end # should "respond to nth_wday"
95
+
96
+ should "respond to wdays" do
97
+ assert_equal(5, @cal.wdays(:sat).size)
98
+ assert_equal(4, @cal.wdays(:fri).size)
99
+ end # should "respond to wdays"
100
+
101
+ should "respond to cycles" do
102
+ dates = @cal.cycles([1, 3], [:tue, :fri])
103
+
104
+ assert_equal(4, dates.size)
105
+
106
+ dates.each do |date|
107
+ assert((date.wday_name == :tue) || (date.wday_name == :fri))
108
+ end
109
+ end # should "respond to cycles"
110
+
111
+ end # context "week day methods"
112
+
79
113
  end # context "Koyomi::Calendar"
80
114
  end
@@ -21,5 +21,22 @@ class TestKoyomiMonth < Test::Unit::TestCase
21
21
  assert(@month.prev.is_a?(Koyomi::Month))
22
22
  end
23
23
 
24
+ should "respond to nth_wday" do
25
+ month = Koyomi::Month.new(12, 2012)
26
+ assert_equal(Date.new(2012, 12, 1), month.nth_wday(1, :sat))
27
+ end
28
+
29
+ should "respond to cycles" do
30
+ month = Koyomi::Month.new(12, 2012)
31
+
32
+ assert_equal(4, month.cycles([1,3], [:tue, :fri]).size)
33
+
34
+ dates = month.cycles(:every, [:mon, :thu])
35
+ assert_equal(9, dates.size)
36
+
37
+ dates = month.cycles(:every, [:mon, :sun])
38
+ assert_equal(10, dates.size)
39
+ end
40
+
24
41
  end # context "Koyomi::Month"
25
42
  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.3
4
+ version: 0.0.4
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-23 00:00:00.000000000 Z
12
+ date: 2012-12-31 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: shoulda