koyomi 0.0.4.1 → 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -38,16 +38,42 @@ Handling calendar by this gem.
38
38
  # weeks and week days.
39
39
 
40
40
  # nth week day.
41
- cal.nth_wday(1, :sat).to_s # => "2012-12-01"
41
+ #
42
+ # Version 0.0.5 or later, NOT compatible version 0.0.4.x.
43
+ # To get version 0.0.4 compatible result, use Koyomi::Month#nth_wday.
44
+ #
45
+ # (japanese)
46
+ # バージョン 0.0.5 以上では、バージョン 0.0.4.x と互換性がありません。
47
+ # 以前のバージョンと同様の結果を得るためには、 Koyomi::Month#nth_wday メソッドを利用して下さい。
48
+ #
49
+ cal.nth_wday(1, :sat).to_s
50
+ # => "2012-12-01"
51
+ cal.nth_wday(1, :tue).to_s
52
+ # => "2012-11-27"
53
+ cal.the_month.nth_wday(1, :tue).to_s
54
+ # => "2012-12-04"
42
55
 
43
56
  # cycle: every monday.
44
57
  # (japanese) 周期:毎週月曜
45
58
  cal.cycles(:every, :mon).collect { |d| d.to_s }
59
+ # => ["2012-11-26", "2012-12-03", "2012-12-10", "2012-12-17", "2012-12-24", "2012-12-31"]
60
+ #
61
+ # Version 0.0.5 or later, NOT compatible version 0.0.4.x.
62
+ # To get version 0.0.4 compatible result, use Koyomi::Month#cycles.
63
+ #
64
+ # (japanese)
65
+ # バージョン 0.0.5 以上では、バージョン 0.0.4.x と互換性がありません。
66
+ # 以前のバージョンと同様の結果を得るためには、 Koyomi::Month#cycles メソッドを利用して下さい。
67
+ #
68
+ cal.the_month.cycles(:every, :mon).collect { |d| d.to_s }
46
69
  # => ["2012-12-03", "2012-12-10", "2012-12-17", "2012-12-24", "2012-12-31"]
47
70
 
48
71
  # cycle: 1st, 3rd week's tuesday or friday.
49
72
  # (japanese) 周期:第1、第3の火曜と金曜
73
+ #
50
74
  cal.cycles([1, 3], [:tue, :fri]).collect { |d| d.to_s }
75
+ # => ["2012-11-27", "2012-11-30", "2012-12-11", "2012-12-14"]
76
+ cal.the_month.cycles([1, 3], [:tue, :fri]).collect { |d| d.to_s }
51
77
  # => ["2012-12-04", "2012-12-07", "2012-12-18", "2012-12-21"]
52
78
 
53
79
 
@@ -18,9 +18,10 @@ class Koyomi::Calendar < Koyomi::Period
18
18
 
19
19
  #--------------------#
20
20
  # instance methods
21
- attr_reader :year, :month, :koyomi_month
22
21
  attr_accessor :week_start
23
-
22
+ attr_reader :year, :month, :koyomi_month
23
+ attr_reader :weeks
24
+
24
25
  # initialize instance
25
26
  #
26
27
  # @param [Integer] year optional, use instance create date.
@@ -30,16 +31,16 @@ class Koyomi::Calendar < Koyomi::Period
30
31
  super()
31
32
  self.year = year||self.created_at.year
32
33
  self.month = month||self.created_at.month
33
- self.week_start = week_start||DEFAULT_WEEK_START
34
-
35
34
  self.koyomi_month = Koyomi::Month.new(self.month, self.year)
35
+ self.week_start = week_start||DEFAULT_WEEK_START
36
36
  end
37
37
 
38
38
  # set week_start
39
39
  #
40
40
  # @param [Object] value
41
41
  def week_start=(value)
42
- @week_start = self.class.windex(value)
42
+ self.setup_week_start(value)
43
+ @week_start
43
44
  end
44
45
 
45
46
  # first date of the calendar (NOT first date of the MONTH)
@@ -70,28 +71,13 @@ class Koyomi::Calendar < Koyomi::Period
70
71
  self.koyomi_month
71
72
  end
72
73
 
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
74
  # week day of nth week.
89
75
  #
90
76
  # @param [Integer] nth
91
77
  # @param [Object] wday_name
92
78
  # @return [Date]
93
79
  def nth_wday(nth, wday_name)
94
- self.koyomi_month.nth_wday(nth, wday_name)
80
+ self.weeks[nth - 1].wday(wday_name)
95
81
  end
96
82
 
97
83
  # week days
@@ -99,13 +85,7 @@ class Koyomi::Calendar < Koyomi::Period
99
85
  # @param [Object] wday_name
100
86
  # @return [Array<Date>]
101
87
  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
88
+ self.weeks.collect { |w| w.wday(wday_name) }
109
89
  end
110
90
 
111
91
  # cycle dates
@@ -114,11 +94,56 @@ class Koyomi::Calendar < Koyomi::Period
114
94
  # @param [Array<Object>|Object] wdays
115
95
  # @return [Array<Date>]
116
96
  def cycles(weeks, wdays)
117
- self.koyomi_month.cycles(weeks, wdays)
97
+ _dates = []
98
+ cycle_weeks_filter(weeks).each do |n|
99
+ [wdays].flatten.each do |w|
100
+ _dates << self.nth_wday(n, w)
101
+ end
102
+ end
103
+ _dates.sort
118
104
  end
119
105
 
120
106
  #--------------------#
121
107
  protected
122
108
 
123
109
  attr_writer :year, :month, :koyomi_month
124
- end
110
+
111
+ # setup week start
112
+ #
113
+ # @param [Object] value
114
+ def setup_week_start(value)
115
+ @week_start = self.class.windex(value)
116
+ self.setup_weeks(@week_start)
117
+ end
118
+
119
+ # setup weeks of the calendar.
120
+ #
121
+ # @return [Array<Week>]
122
+ def setup_weeks(week_start)
123
+ a_date = self.first
124
+ the_last = self.last
125
+ @weeks = []
126
+
127
+ while (a_date < the_last)
128
+ @weeks << Koyomi::Week.new(a_date, week_start)
129
+ a_date += WEEK_DAYS
130
+ end
131
+ @weeks
132
+ end
133
+
134
+ #--------------------#
135
+ private
136
+
137
+ # cycle weeks filter
138
+ #
139
+ # @param [Object] weeks
140
+ # @return [Iterator] Array or Range
141
+ def cycle_weeks_filter(weeks)
142
+ case
143
+ when weeks.to_s =~ /every/
144
+ (1..self.weeks.size)
145
+ else
146
+ [weeks].flatten
147
+ end
148
+ end
149
+ end
@@ -15,6 +15,14 @@ class Date
15
15
  self.class.week_ends?(self, week_start)
16
16
  end
17
17
 
18
+ # check week start?
19
+ #
20
+ # @param [Object] week_start
21
+ # @return [Boolean]
22
+ def week_start?(week_start = nil)
23
+ self.class.week_starts?(self, week_start)
24
+ end
25
+
18
26
  # week day index
19
27
  #
20
28
  # @return [Integer]
@@ -28,4 +36,32 @@ class Date
28
36
  def wday_name
29
37
  self.class.wday_name(self)
30
38
  end
39
+
40
+ # nth week day of the month
41
+ #
42
+ # @return [Integer]
43
+ def nth_month_week
44
+ _week = (self.day / WEEK_DAYS).to_i
45
+ _fraction = (self.day % WEEK_DAYS)
46
+ (_week + (_fraction == 0 ? 0 : 1))
47
+ end
48
+
49
+ # nth week, week day of the month
50
+ #
51
+ # @return [Array] [<Integer>nth_month_week, <Symbol>wday_name]
52
+ def nth_wday
53
+ [self.nth_month_week, self.wday_name]
54
+ end
55
+
56
+ # monthly information
57
+ #
58
+ # @return [Hash]
59
+ def month_info
60
+ _info = {}
61
+ _infos = self.nth_wday.dup
62
+ _info[:nth] = _infos.shift
63
+ _info[:wday] = _infos.shift
64
+
65
+ _info
66
+ end
31
67
  end
@@ -40,5 +40,14 @@ module Koyomi::Helper::Week
40
40
  def week_ends?(date, week_start = nil)
41
41
  Koyomi::Week.ends?(date, week_start)
42
42
  end
43
+
44
+ # week start?
45
+ #
46
+ # @param [Date] date
47
+ # @param [Object] week_start week start
48
+ # @return [Boolean]
49
+ def week_starts?(date, week_start = nil)
50
+ Koyomi::Week.starts?(date, week_start)
51
+ end
43
52
  end
44
53
  end
data/lib/koyomi/month.rb CHANGED
@@ -70,6 +70,22 @@ class Koyomi::Month < Koyomi::Period
70
70
  Koyomi::Week.new(a_date, a_date.wday).wday(wday_name)
71
71
  end
72
72
 
73
+
74
+ # week days
75
+ #
76
+ # @param [Object] wday_name
77
+ # @return [Array<Date>]
78
+ def wdays(wday_name)
79
+ _dates = []
80
+ a_date = self.nth_wday(1, wday_name)
81
+
82
+ while (a_date.month == self.month)
83
+ _dates << a_date
84
+ a_date += Koyomi::Week::DAYS
85
+ end
86
+ _dates.sort
87
+ end
88
+
73
89
  #--------------------#
74
90
  protected
75
91
 
@@ -1,5 +1,5 @@
1
1
  # encoding: utf-8
2
2
 
3
3
  module Koyomi
4
- VERSION = "0.0.4.1"
4
+ VERSION = "0.0.5"
5
5
  end
@@ -90,22 +90,46 @@ class TestKoyomiCalendar < Test::Unit::TestCase
90
90
  end # should "have weeks"
91
91
 
92
92
  should "respond to nth_wday" do
93
- assert_equal(Date.new(2012, 12, 1), @cal.nth_wday(1, :sat))
93
+ assert_equal(Date.new(2012,12,1), @cal.nth_wday(1, :sat))
94
+
95
+ # version 0.0.5 or later
96
+ assert_equal(Date.new(2012,11,27), @cal.nth_wday(1, :tue))
94
97
  end # should "respond to nth_wday"
95
98
 
96
99
  should "respond to wdays" do
97
- assert_equal(5, @cal.wdays(:sat).size)
98
- assert_equal(4, @cal.wdays(:fri).size)
100
+ # version 0.0.4.x
101
+ #assert_equal(5, @cal.wdays(:sat).size)
102
+ #assert_equal(4, @cal.wdays(:fri).size)
103
+
104
+ # version 0.0.5 or later
105
+ assert_equal(6, @cal.wdays(:sat).size)
106
+ assert_equal(6, @cal.wdays(:fri).size)
99
107
  end # should "respond to wdays"
100
108
 
101
109
  should "respond to cycles" do
102
110
  dates = @cal.cycles([1, 3], [:tue, :fri])
103
-
104
111
  assert_equal(4, dates.size)
105
112
 
106
113
  dates.each do |date|
107
114
  assert((date.wday_name == :tue) || (date.wday_name == :fri))
108
115
  end
116
+
117
+ # version 0.0.5 or later
118
+ dates = @cal.cycles(:every, [:tue, :fri])
119
+ assert_equal(12, dates.size)
120
+
121
+ dates.each do |date|
122
+ assert((date.wday_name == :tue) || (date.wday_name == :fri))
123
+ end
124
+
125
+ @cal.week_start = :tue
126
+ dates = @cal.cycles(:every, [:tue, :fri])
127
+ assert_equal(10, dates.size)
128
+
129
+ dates.each do |date|
130
+ assert((date.wday_name == :tue) || (date.wday_name == :fri))
131
+ end
132
+
109
133
  end # should "respond to cycles"
110
134
 
111
135
  end # context "week day methods"
@@ -9,7 +9,7 @@ class TestKoyomiDate < Test::Unit::TestCase
9
9
  @wdays = Date::WEEK_WDAYS
10
10
  end # setup
11
11
 
12
- should "correct week end" do
12
+ should "correct week start, end" do
13
13
  cal = Koyomi::Calendar.of(@date)
14
14
 
15
15
  cal.each do |date|
@@ -19,9 +19,24 @@ class TestKoyomiDate < Test::Unit::TestCase
19
19
  else
20
20
  assert(!date.week_end?(wd))
21
21
  end
22
+
23
+ if (date).wday == @wdays.index(wd)
24
+ assert(date.week_start?(wd))
25
+ else
26
+ assert(!date.week_start?(wd))
27
+ end
22
28
  end # each wd
23
29
  end # each date
24
30
  end # should "correct week end"
25
31
 
32
+ should "respond to nth_wday" do
33
+ assert_equal(3, @date.nth_month_week)
34
+ end # should "respond to nth_wday"
35
+
36
+ should "respond to info" do
37
+ assert_equal(3, @date.month_info[:nth])
38
+ assert_equal(:sun, @date.month_info[:wday])
39
+ end
40
+
26
41
  end # context "Koyomi::Date"
27
42
  end
@@ -26,6 +26,12 @@ class TestKoyomiMonth < Test::Unit::TestCase
26
26
  assert_equal(Date.new(2012, 12, 1), month.nth_wday(1, :sat))
27
27
  end
28
28
 
29
+ should "respond to wdays" do
30
+ month = Koyomi::Month.new(12, 2012)
31
+ assert_equal(5, @month.wdays(:sat).size)
32
+ assert_equal(4, @month.wdays(:fri).size)
33
+ end # should "respond to wdays"
34
+
29
35
  should "respond to cycles" do
30
36
  month = Koyomi::Month.new(12, 2012)
31
37
 
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.1
4
+ version: 0.0.5
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors: