koyomi 0.2.0 → 0.2.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 2424c6cda5e48fb64ecee69b6fb8b292b0a13e3a
4
- data.tar.gz: b21e94aff1c9fe720a79ce9bd7bd076d679749d3
3
+ metadata.gz: 8ae7adb3bc47e5ebb1cea544a321182ab5106fb1
4
+ data.tar.gz: 6638ed782cae88821e1ba1fb6b18b518759e0d93
5
5
  SHA512:
6
- metadata.gz: da9ef2356634f30e02edd0794a6e12d0a3ca0c762611a2a4aae879755f85849a57141c3e53def14b6293289cd6ddc112b3bad36aa68e5f4c8a81860f9c14f389
7
- data.tar.gz: a90d16687d127b8182c082859e8ad78e30c5c97227a80de6d03bf7ca93ba69e15b886c4ceae56b3682129db01a9f6b168230e1e2955085289a85c942c88e6526
6
+ metadata.gz: d045c99a50eb34a4cf763cbb7df3b1d30eba817ed69f807f0866ee762a1688ab9ca62da43fa0051f8888d0845f425237484a458cae20aed3fc1f5aa8bd51eb19
7
+ data.tar.gz: 0d8ff0eb4086862779caa7fefe8df57574273ee906a5ad5ed48c92c00529e1561f79f965c308af708f2037ed6cbdabda5281ace1964d4efb7e1998f5da89d9db
data/.gitignore CHANGED
@@ -20,3 +20,4 @@ tmp
20
20
  vendor/bundle
21
21
  log
22
22
  log/*.log
23
+ Gemfile.lock
data/README.md CHANGED
@@ -18,64 +18,60 @@ Or install it yourself as:
18
18
 
19
19
  ## Usage
20
20
 
21
- Handling calendar by this gem.
22
-
23
- (japanese) カレンダーを取り扱うための gem です。
24
-
25
- require "koyomi"
26
- cal = Koyomi::Calendar.new(2012, 12, :mon)
27
- cal.first.to_s # => "2012-11-26"
28
-
29
- cal.first.week_end? # => false
30
- cal.first.week_end?(:tue) # => true
31
-
32
- month = cal.the_month
33
- month.first.to_s # => "2012-12-01"
34
-
35
- week = Koyomi::Week.new(month.first, :tue)
36
- week.first.to_s # => "2012-11-27"
37
-
38
- # weeks and week days.
39
-
40
- # nth week day.
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"
55
-
56
- # cycle: every monday.
57
- # (japanese) 周期:毎週月曜
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 }
69
- # => ["2012-12-03", "2012-12-10", "2012-12-17", "2012-12-24", "2012-12-31"]
70
-
71
- # cycle: 1st, 3rd week's tuesday or friday.
72
- # (japanese) 周期:第1、第3の火曜と金曜
73
- #
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 }
77
- # => ["2012-12-04", "2012-12-07", "2012-12-18", "2012-12-21"]
21
+ ``` ruby
22
+ require "koyomi"
78
23
 
24
+ ## Koyomi::Calendar
25
+ cal = Koyomi::Calendar.new(2015, 12, :mon)
26
+ # The calendar starts monday.
27
+ # 30 1 2 3 4 5 6
28
+ # 7 8 9 10 11 12 13
29
+ # 14 15 16 17 18 19 20
30
+ # 21 22 23 24 25 26 27
31
+ # 28 29 30 31 1 2 3
32
+
33
+ cal.first.class
34
+ # => Date
35
+
36
+ cal.first.week_end?
37
+ # => false
38
+
39
+ cal.first.week_end?(:tue)
40
+ # => true
41
+
42
+ cal.first.to_s
43
+ # => "2015-11-30"
44
+
45
+ cal.range.first.to_s
46
+ # => "2015-11-30"
47
+
48
+ cal.range.last.to_s
49
+ # => "2016-01-03"
50
+
51
+ ## Koyomi::Month
52
+ month = cal.the_month
53
+ # The month
54
+ # 1 2 3 4 5 6
55
+ # 7 8 9 10 11 12 13
56
+ # 14 15 16 17 18 19 20
57
+ # 21 22 23 24 25 26 27
58
+ # 28 29 30 31
59
+
60
+ month.first.class
61
+ # => Date
62
+
63
+ month.first.to_s
64
+ # => "2015-12-01"
65
+
66
+ ## searching dates
67
+ cal.nth_wday(1, :mon)
68
+ # => #<Date: 2015-11-30 ((2457357j,0s,0n),+0s,2299161j)>
69
+ month.nth_wday(1, :mon)
70
+ # => #<Date: 2015-12-07 ((2457364j,0s,0n),+0s,2299161j)>
71
+
72
+ ```
73
+
74
+ Some examples in [Wiki](wiki/examples).
79
75
 
80
76
  ## Contributing
81
77
 
@@ -18,7 +18,7 @@ class Koyomi::Calendar < Koyomi::Period
18
18
 
19
19
  #--------------------#
20
20
  # instance methods
21
- attr_accessor :week_start
21
+ attr_reader :week_start
22
22
  attr_reader :year, :month, :koyomi_month
23
23
  attr_reader :weeks
24
24
 
@@ -28,19 +28,14 @@ class Koyomi::Calendar < Koyomi::Period
28
28
  # @param [Integer] month optional, use instance create date.
29
29
  # @param [Object] week_start weekday which week starts with. optional, use DEFAULT_WEEK_START.
30
30
  def initialize(year = nil, month = nil, week_start = Koyomi::Week::DEFAULT_START)
31
- super()
32
- self.year = year||self.created_at.year
33
- self.month = month||self.created_at.month
34
- self.koyomi_month = Koyomi::Month.new(self.month, self.year)
35
- self.week_start = week_start
36
- end
37
-
38
- # set week_start
39
- #
40
- # @param [Object] value
41
- def week_start=(value)
42
- self.setup_week_start(value)
43
- @week_start
31
+ _today = Date.today
32
+ @year = year||_today.year
33
+ @month = month||_today.month
34
+ @koyomi_month = Koyomi::Month.new(@month, @year)
35
+
36
+ _week = Koyomi::Week.new(@koyomi_month.first, @week_start)
37
+ super(_week.first, _week.last)
38
+ setup_week_start(week_start)
44
39
  end
45
40
 
46
41
  # first date of the calendar (NOT first date of the MONTH)
@@ -57,11 +52,12 @@ class Koyomi::Calendar < Koyomi::Period
57
52
  Koyomi::Week.new(self.koyomi_month.last, self.week_start).last
58
53
  end
59
54
 
60
- # range of the calendar.
55
+ # set week_start
61
56
  #
62
- # @return [Range]
63
- def range
64
- Range.new(self.first, self.last)
57
+ # @param [Object] value
58
+ def week_start=(value)
59
+ setup_week_start(value)
60
+ @week_start
65
61
  end
66
62
 
67
63
  # Koyomi::Month of the calendar's month.
@@ -109,12 +105,15 @@ class Koyomi::Calendar < Koyomi::Period
109
105
 
110
106
  attr_writer :year, :month, :koyomi_month
111
107
 
108
+ #--------------------#
109
+ private
110
+
112
111
  # setup week start
113
112
  #
114
113
  # @param [Object] value
115
114
  def setup_week_start(value)
116
115
  @week_start = self.class.windex(value)
117
- self.setup_weeks(@week_start)
116
+ setup_weeks(@week_start)
118
117
  end
119
118
 
120
119
  # setup weeks of the calendar.
@@ -132,9 +131,6 @@ class Koyomi::Calendar < Koyomi::Period
132
131
  @weeks
133
132
  end
134
133
 
135
- #--------------------#
136
- private
137
-
138
134
  # cycle weeks filter
139
135
  #
140
136
  # @param [Object] weeks
@@ -15,11 +15,27 @@ class Koyomi::Month < Koyomi::Period
15
15
  self.new(date.month, date.year)
16
16
  end
17
17
 
18
+ # a date next month of the date
19
+ #
20
+ # @param [Date] date
21
+ # @return [Date]
22
+ def self.a_date_of_next_month(date)
23
+ date + 32
24
+ end
25
+
26
+ # first date of next month
27
+ #
28
+ # @param [Date] date
29
+ # @return [Date]
30
+ def self.first_date_of_next_month(date)
31
+ a_date = self.a_date_of_next_month(date)
32
+ Date.new(a_date.year, a_date.month, 1)
33
+ end
34
+
18
35
  #--------------------#
19
36
  # instance methods
20
37
 
21
38
  attr_reader :year, :month
22
- attr_reader :first, :last
23
39
 
24
40
  # initialize instance.
25
41
  #
@@ -27,11 +43,10 @@ class Koyomi::Month < Koyomi::Period
27
43
  # @param [Integer] year optional, default use the year of instance created.
28
44
  def initialize(month = nil, year = nil)
29
45
  super()
30
- @year = year||self.created_at.year
31
- @month = month||self.created_at.month
32
- @first = Date.new(self.year, self.month, 1)
33
- @last = (first_date_of_next_month - 1)
34
- @range = Range.new(self.first, self.last)
46
+ @year = year||created_at.year
47
+ @month = month||created_at.month
48
+ @first = Date.new(@year, @month, 1)
49
+ @last = (self.class.first_date_of_next_month(@first) - 1)
35
50
  end
36
51
 
37
52
  # next month
@@ -164,23 +179,6 @@ class Koyomi::Month < Koyomi::Period
164
179
  _weeks
165
180
  end
166
181
 
167
- # a date next month of the date
168
- #
169
- # @param [Date] date
170
- # @return [Date]
171
- def a_date_of_next_month
172
- self.first + 32
173
- end
174
-
175
- # first date of next month
176
- #
177
- # @param [Date] date
178
- # @return [Date]
179
- def first_date_of_next_month
180
- a_date = a_date_of_next_month
181
- Date.new(a_date.year, a_date.month, 1)
182
- end
183
-
184
182
  # maximumn of weeks
185
183
  #
186
184
  # @return [Integer]
@@ -3,25 +3,27 @@
3
3
  require "date"
4
4
 
5
5
  class Koyomi::Period
6
- attr_reader :range
7
-
6
+ attr_reader :first, :last
7
+
8
8
  #--------------------#
9
9
  # instance methods
10
-
11
- def initialize
10
+
11
+ def initialize(first = nil, last = nil)
12
12
  super()
13
- self.created_at = Date.today
13
+ @created_at = Date.today
14
+ @first = first||@created_at
15
+ @last = last||@created_at
14
16
  end
15
-
16
- #--------------------#
17
- protected
18
-
19
- attr_accessor :created_at
20
- attr_writer :range
21
-
17
+
18
+ def range
19
+ (first .. last)
20
+ end
21
+
22
22
  #--------------------#
23
23
  private
24
-
24
+
25
+ attr_reader :created_at
26
+
25
27
  # throw uniplemeted method to self range.
26
28
  def method_missing(name, *args, &block)
27
29
  begin
@@ -30,4 +32,4 @@ class Koyomi::Period
30
32
  super
31
33
  end
32
34
  end
33
- end
35
+ end
@@ -1,5 +1,5 @@
1
1
  # encoding: utf-8
2
2
 
3
3
  module Koyomi
4
- VERSION = "0.2.0"
4
+ VERSION = "0.2.2"
5
5
  end
@@ -73,8 +73,8 @@ class Koyomi::Week < Koyomi::Period
73
73
  # @param [Object] start_wday optionail, default use Koyomi::Week::DEFAULT_START
74
74
  def initialize(date = nil, start_wday = DEFAULT_START)
75
75
  super()
76
- self.date = date||self.created_at
77
- self.start_wday = start_wday
76
+ @start_wday = start_wday
77
+ setup_range(date||created_at)
78
78
  end
79
79
 
80
80
  # sepified week day
@@ -84,48 +84,27 @@ class Koyomi::Week < Koyomi::Period
84
84
  def wday(wday_name)
85
85
  diff = self.class.windex(wday_name) - self.class.windex(self.start_wday)
86
86
  factor = diff + ((diff < 0) ? DAYS : 0)
87
- self.range.first + factor
87
+ first + factor
88
88
  end
89
89
 
90
- # start date
91
- #
92
- # @return [Date] date
93
- def starts
94
- self.range.first
95
- end
96
-
97
- # end date
98
- #
99
- # @return [Date] date
100
- def ends
101
- self.range.last
102
- end
90
+ alias_method :starts, :first
91
+ alias_method :ends, :last
103
92
 
104
93
  #--------------------#
105
94
  protected
106
95
 
107
- attr_accessor :date
108
96
  attr_reader :start_wday
109
97
 
110
- # set week starts
111
- #
112
- # @param [Object] value
113
- def start_wday=(value)
114
- @start_wday = value
115
- self.setup_range
116
- @start_wday
117
- end
98
+ #--------------------#
99
+ private
118
100
 
119
101
  # setup week range with given week start
120
- def setup_range
121
- diff = self.date.wday - self.class.windex(self.start_wday)
122
- starts = self.date - (diff + ((diff < 0) ? DAYS : 0))
123
- @range = Range.new(starts, starts + DAYS - 1)
102
+ def setup_range(date)
103
+ diff = date.wday - self.class.windex(start_wday)
104
+ @first = date - (diff + ((diff < 0) ? DAYS : 0))
105
+ @last = @first + DAYS - 1
124
106
  end
125
107
 
126
- #--------------------#
127
- private
128
-
129
108
  def method_missing(name, *args, &block)
130
109
  case
131
110
  when WDAYS.include?(name.to_s.to_sym)
@@ -18,7 +18,6 @@ class Koyomi::Year < Koyomi::Period
18
18
  # instance methods
19
19
 
20
20
  attr_reader :year
21
- attr_reader :first, :last
22
21
 
23
22
  # initialize instance.
24
23
  #
@@ -28,7 +27,6 @@ class Koyomi::Year < Koyomi::Period
28
27
  @year = year||created_at.year
29
28
  @first = Date.new(@year, 1, 1)
30
29
  @last = Date.new(@year, 12, 31)
31
- @range = (@first .. @last)
32
30
  end
33
31
 
34
32
  (1..12).each do |m|
@@ -12,6 +12,13 @@ describe Koyomi::Calendar do
12
12
  its(:month) { is_expected.to eq date.month }
13
13
  end
14
14
 
15
+ context "initialized with 2015-12-01, week start monday." do
16
+ subject { described_class.new(*given) }
17
+ let(:given) { [2015, 12, :mon] }
18
+ its(:first) { is_expected.to eq Date.new(2015, 11, 30) }
19
+ its(:last) { is_expected.to eq Date.new(2016, 1, 3) }
20
+ end
21
+
15
22
  describe "#koyomi_month" do
16
23
  subject { calendar.koyomi_month }
17
24
 
@@ -1,8 +1,9 @@
1
1
  require "koyomi_helper"
2
2
 
3
3
  describe Koyomi::Month do
4
- let(:month) { described_class.new(today.month, today.year) }
4
+ let(:month) { described_class.new(date.month, date.year) }
5
5
  let!(:today) { Date.today }
6
+ let(:date) { today }
6
7
 
7
8
  describe "#range" do
8
9
  subject { month.range }
@@ -0,0 +1,37 @@
1
+ require "koyomi_helper"
2
+
3
+ describe Koyomi::Period do
4
+ let(:period) { described_class.new(*initialize_with) }
5
+
6
+ describe ".new" do
7
+ subject { period }
8
+
9
+ context "no arguments given" do
10
+ let(:initialize_with) { nil }
11
+ it { expect { subject }.not_to raise_error }
12
+
13
+ describe "#range" do
14
+ subject{ period.range }
15
+ its("count") { is_expected.to eq 1 }
16
+ its("first") { is_expected.to be_kind_of Date}
17
+ end
18
+ end
19
+
20
+ context "first and last given" do
21
+ let(:initialize_with) { [Date.new(2015, 12, 1), Date.new(2015, 12, 31)] }
22
+ it { expect { subject }.not_to raise_error }
23
+ end
24
+ end
25
+
26
+ context "initialized normaly" do
27
+ let(:initialize_with) { [first, last] }
28
+ let(:first) { Date.new(2015, 12, 1) }
29
+ let(:last) { Date.new(2015, 12, 31) }
30
+
31
+ describe "#range" do
32
+ subject { period.range }
33
+ its("first") { is_expected.to eq first }
34
+ its("last") { is_expected.to eq last }
35
+ end
36
+ end
37
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: koyomi
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.2.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - sekizo
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-11-25 00:00:00.000000000 Z
11
+ date: 2015-12-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rspec
@@ -63,7 +63,6 @@ files:
63
63
  - ".rspec"
64
64
  - CHANGELOG.md
65
65
  - Gemfile
66
- - Gemfile.lock
67
66
  - Guardfile
68
67
  - LICENSE.txt
69
68
  - README.md
@@ -90,6 +89,7 @@ files:
90
89
  - spec/lib/koyomi/date_spec.rb
91
90
  - spec/lib/koyomi/month_cycle_spec.rb
92
91
  - spec/lib/koyomi/month_spec.rb
92
+ - spec/lib/koyomi/period_spec.rb
93
93
  - spec/lib/koyomi/version_spec.rb
94
94
  - spec/lib/koyomi/week_spec.rb
95
95
  - spec/lib/koyomi/year_spec.rb
@@ -126,6 +126,7 @@ test_files:
126
126
  - spec/lib/koyomi/date_spec.rb
127
127
  - spec/lib/koyomi/month_cycle_spec.rb
128
128
  - spec/lib/koyomi/month_spec.rb
129
+ - spec/lib/koyomi/period_spec.rb
129
130
  - spec/lib/koyomi/version_spec.rb
130
131
  - spec/lib/koyomi/week_spec.rb
131
132
  - spec/lib/koyomi/year_spec.rb
@@ -1,73 +0,0 @@
1
- PATH
2
- remote: .
3
- specs:
4
- koyomi (0.2.0)
5
-
6
- GEM
7
- remote: https://rubygems.org/
8
- specs:
9
- coderay (1.1.0)
10
- diff-lcs (1.2.5)
11
- ffi (1.9.10)
12
- formatador (0.2.5)
13
- guard (2.13.0)
14
- formatador (>= 0.2.4)
15
- listen (>= 2.7, <= 4.0)
16
- lumberjack (~> 1.0)
17
- nenv (~> 0.1)
18
- notiffany (~> 0.0)
19
- pry (>= 0.9.12)
20
- shellany (~> 0.0)
21
- thor (>= 0.18.1)
22
- guard-compat (1.2.1)
23
- guard-rspec (4.6.4)
24
- guard (~> 2.1)
25
- guard-compat (~> 1.1)
26
- rspec (>= 2.99.0, < 4.0)
27
- listen (3.0.4)
28
- rb-fsevent (>= 0.9.3)
29
- rb-inotify (>= 0.9)
30
- lumberjack (1.0.9)
31
- method_source (0.8.2)
32
- nenv (0.2.0)
33
- notiffany (0.0.8)
34
- nenv (~> 0.1)
35
- shellany (~> 0.0)
36
- pry (0.10.3)
37
- coderay (~> 1.1.0)
38
- method_source (~> 0.8.1)
39
- slop (~> 3.4)
40
- rb-fsevent (0.9.6)
41
- rb-inotify (0.9.5)
42
- ffi (>= 0.5.0)
43
- rspec (3.4.0)
44
- rspec-core (~> 3.4.0)
45
- rspec-expectations (~> 3.4.0)
46
- rspec-mocks (~> 3.4.0)
47
- rspec-core (3.4.0)
48
- rspec-support (~> 3.4.0)
49
- rspec-expectations (3.4.0)
50
- diff-lcs (>= 1.2.0, < 2.0)
51
- rspec-support (~> 3.4.0)
52
- rspec-its (1.2.0)
53
- rspec-core (>= 3.0.0)
54
- rspec-expectations (>= 3.0.0)
55
- rspec-mocks (3.4.0)
56
- diff-lcs (>= 1.2.0, < 2.0)
57
- rspec-support (~> 3.4.0)
58
- rspec-support (3.4.0)
59
- shellany (0.0.1)
60
- slop (3.6.0)
61
- thor (0.19.1)
62
-
63
- PLATFORMS
64
- ruby
65
-
66
- DEPENDENCIES
67
- guard-rspec
68
- koyomi!
69
- rspec
70
- rspec-its
71
-
72
- BUNDLED WITH
73
- 1.10.6