defoker 0.0.1

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/defoker.gemspec ADDED
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'defoker/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'defoker'
8
+ spec.version = Defoker::VERSION
9
+ spec.authors = ['tbpgr']
10
+ spec.email = ['tbpgr@tbpgr.jp']
11
+ spec.summary = %q(Create variaous kind of date-base format named folders.)
12
+ spec.description = %q(Create variaous kind of date-base format named folders.)
13
+ spec.homepage = 'https://github.com/tbpgr/defoker'
14
+ spec.license = 'MIT'
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ['lib']
20
+
21
+ spec.add_development_dependency 'bundler', '~> 1.6'
22
+ spec.add_development_dependency 'rake', '~> 10.0'
23
+ spec.add_development_dependency 'yard'
24
+ spec.add_development_dependency 'timecop'
25
+ end
Binary file
@@ -0,0 +1,165 @@
1
+ # encoding: utf-8
2
+ require 'date'
3
+
4
+ module Defoker
5
+ # DateBaseName provides methods that various kind of date-base formated name.
6
+ # date base, month base, year base, and etc...
7
+ class DateBaseName
8
+ # Get date-base formated name.
9
+ # if additional is empty, return date YYYYMMDD.
10
+ # if additional is not empty, return date YYYYMMDD_additional.
11
+ #
12
+ # @example only date
13
+ # DateBaseName.new.to_yyyymmdd(Date.new(2014,9,9)) #=> "20140909"
14
+ # @example date with additional text
15
+ # DateBaseName.new.to_yyyymmdd(Date.new(2014,9,9), additional: 'hoge') #=> "20140909_hoge"
16
+ #
17
+ # @param [Date,DateTime] date target date
18
+ # @param [String] additional additional text.
19
+ # If you want to get only date, you can omit this option.
20
+ # @return [String] date-based formated name
21
+ #
22
+ def to_yyyymmdd(date, additional: '')
23
+ DateBaseNameValidators.validate_date(date)
24
+ v = date.strftime('%Y%m%d')
25
+ return v if additional.empty?
26
+ "#{v}_#{additional}"
27
+ end
28
+
29
+ # Get date-base formated name list.
30
+ # if additional is empty, return date YYYYMMDD.
31
+ # if additional is not empty, return date YYYYMMDD_additional.
32
+ #
33
+ # @example only date
34
+ # DateBaseName.new.to_yyyymmdd_list(Date.new(2014,9,9), Date.new(2014,9,12))
35
+ # => ["20140909","20140910","20140911","20140912"]
36
+ # @example date with additional text
37
+ # DateBaseName.new.to_yyyymmdd_list(Date.new(2014,9,9), count: Date.new(2014,9,12), additional: 'hoge')
38
+ # => ["20140909_hoge", "20140910_hoge", "20140911_hoge", "20140912_hoge"]
39
+ # @example range
40
+ # DateBaseName.new.to_yyyymmdd_list(Date.new(2014,9,9)..Date.new(2014,9,12), additional: 'hoge')
41
+ # => ["20140909_hoge", "20140910_hoge", "20140911_hoge", "20140912_hoge"]
42
+ #
43
+ # @param [Date,DateTime,Range] day date or from-count range
44
+ # @param [Date] count count date.
45
+ # @param [String] additional additional text.
46
+ # If you want count get only date, you can omit this option.
47
+ # @return [Array(String)] date-based formated names
48
+ def to_yyyymmdd_list(day, count: nil, additional: '')
49
+ DateBaseNameValidators.validate_date(day)
50
+ count.times.with_object([]) do |i, memo|
51
+ memo << to_yyyymmdd(day + i, additional: additional)
52
+ end
53
+ end
54
+
55
+ # Get month-base formated name.
56
+ # if additional is empty, return month YYYYMM.
57
+ # if additional is not empty, return month YYYYMM_additional.
58
+ #
59
+ # @example only month
60
+ # DateBaseName.new.to_yyyymm(Date.new(2014,9)) #=> "201409"
61
+ # @example month with additional text
62
+ # DateBaseName.new.to_yyyymm(Date.new(2014,9), additional: 'hoge') #=> "201409_hoge"
63
+ #
64
+ # @param [Date,DateTime] month target month
65
+ # @param [String] additional additional text.
66
+ # If you want to get only month, you can omit this option.
67
+ # @return [String] month-based formated name
68
+ #
69
+ def to_yyyymm(month, additional: '')
70
+ DateBaseNameValidators.validate_date(month)
71
+ v = month.strftime('%Y%m')
72
+ return v if additional.empty?
73
+ "#{v}_#{additional}"
74
+ end
75
+
76
+ # Get month-base formated name list.
77
+ # if additional is empty, return month YYYYMM.
78
+ # if additional is not empty, return month YYYYMM_additional.
79
+ #
80
+ # @example only month
81
+ # DateBaseName.new.to_yyyymm_list(Date.new(2014,10), count: 4))
82
+ # => ["201410","201411","20141","201501"]
83
+ # @example only month (omit count. default = 3)
84
+ # DateBaseName.new.to_yyyymm_list(Date.new(2014,10)))
85
+ # => ["201410","201411","20141"]
86
+ # @example month with additional text
87
+ # DateBaseName.new.to_yyyymm_list(Date.new(2014,9,9), count: 4, additional: 'hoge')
88
+ # => ["201410_hoge","201411_hoge","20141_hoge","201501_hoge"]
89
+ #
90
+ # @param [Date,DateTime] month month
91
+ # @param [Fixnum] count month count. default = 3.
92
+ # @param [String] additional additional text.
93
+ # If you want to get only month, you can omit this option.
94
+ # @return [Array(String)] month-based formated names
95
+ def to_yyyymm_list(month, count: 3, additional: '')
96
+ DateBaseNameValidators.validate_date(month)
97
+ count.times.with_object([]) do |i, memo|
98
+ memo << to_yyyymm(month >> i, additional: additional)
99
+ end
100
+ end
101
+
102
+ # Get year-base formated name.
103
+ # if additional is empty, return year YYYY.
104
+ # if additional is not empty, return year YYYY_additional.
105
+ #
106
+ # @example only year
107
+ # DateBaseName.new.to_yyyy(Date.new(2014)) #=> "2014"
108
+ # @example year with additional text
109
+ # DateBaseName.new.to_yyyy(Date.new(2014), additional: 'hoge') #=> "2014_hoge"
110
+ #
111
+ # @param [Date,DateTime] year target year
112
+ # @param [String] additional additional text.
113
+ # If you want to get only year, you can omit this option.
114
+ # @return [String] year-based formated name
115
+ #
116
+ def to_yyyy(year, additional: '')
117
+ DateBaseNameValidators.validate_date(year)
118
+ v = year.strftime('%Y')
119
+ return v if additional.empty?
120
+ "#{v}_#{additional}"
121
+ end
122
+
123
+ # Get year-base formated name list.
124
+ # if additional is empty, return year YYYY.
125
+ # if additional is not empty, return year YYYY_additional.
126
+ #
127
+ # @example only year
128
+ # DateBaseName.new.to_yyyymm_list(Date.new(2014), count: 4))
129
+ # => ["2014","2015","2016","2017"]
130
+ # @example only year (omit count. default = 3)
131
+ # DateBaseName.new.to_yyyymm_list(Date.new(2014)))
132
+ # => ["2014","2015","2016"]
133
+ # @example year with additional text
134
+ # DateBaseName.new.to_yyyymm_list(Date.new(2014), count: 4, additional: 'hoge')
135
+ # => ["2014_hoge","2015_hoge","2016_hoge","2017_hoge"]
136
+ #
137
+ # @param [Date,DateTime] year year
138
+ # @param [Fixnum] count year count. default = 3.
139
+ # @param [String] additional additional text.
140
+ # If you want to get only year, you can omit this option.
141
+ # @return [Array(String)] year-based formated names
142
+ def to_yyyy_list(year, count: 3, additional: '')
143
+ DateBaseNameValidators.validate_date(year)
144
+ count.times.with_object([]) do |i, memo|
145
+ memo << to_yyyy(year >> (i * 12), additional: additional)
146
+ end
147
+ end
148
+ end
149
+
150
+ # InvalidRange
151
+ class InvalidRange < StandardError; end
152
+
153
+ # Validators
154
+ class DateBaseNameValidators
155
+ def self.validate_date(date)
156
+ return if [Date, DateTime].include?(date.class)
157
+ fail ArgumentError, "invalid argument type #{date.class}"
158
+ end
159
+
160
+ def self.validate_date_range(date)
161
+ return if [Date, DateTime, Range].include?(date.class)
162
+ fail ArgumentError, "invalid argument type #{date.class}"
163
+ end
164
+ end
165
+ end
@@ -0,0 +1,3 @@
1
+ module Defoker
2
+ VERSION = '0.0.1'
3
+ end
@@ -0,0 +1,113 @@
1
+ # encoding: utf-8
2
+ require 'date_base_name'
3
+ require 'date'
4
+
5
+ module Defoker
6
+ # rubocop disable LineLength
7
+
8
+ # Defoker Core
9
+ class Core
10
+ # Get today folder name
11
+ #
12
+ # @param [String] additional additional name
13
+ # @return [String] today folder name
14
+ def today(additional: '')
15
+ DateBaseName.new.to_yyyymmdd(Date.today, additional: additional)
16
+ end
17
+
18
+ # Get tommorow folder name
19
+ #
20
+ # @param [String] additional additional name
21
+ # @return [String] tommorow folder name
22
+ def tommorow(additional: '')
23
+ DateBaseName.new.to_yyyymmdd(Date.today + 1, additional: additional)
24
+ end
25
+
26
+ # Get yesterday folder name
27
+ #
28
+ # @param [String] additional additional name
29
+ # @return [String] yesterday folder name
30
+ def yesterday(additional: '')
31
+ DateBaseName.new.to_yyyymmdd(Date.today - 1, additional: additional)
32
+ end
33
+
34
+ # Get days folder name list
35
+ #
36
+ # @param [String] date yyyymmdd format string
37
+ # @param [String] additional additional name
38
+ # @return [String] yesterday folder name
39
+ def days(date, count: 3, additional: '')
40
+ date = Date.new(date[0..3].to_i, date[4..5].to_i, date[6..7].to_i)
41
+ DateBaseName.new.to_yyyymmdd_list(date, count: count, additional: additional)
42
+ end
43
+
44
+ # Get this month folder name
45
+ #
46
+ # @param [String] additional additional name
47
+ # @return [String] this month folder name
48
+ def this_month(additional: '')
49
+ DateBaseName.new.to_yyyymm(Date.today, additional: additional)
50
+ end
51
+
52
+ # Get next month folder name
53
+ #
54
+ # @param [String] additional additional name
55
+ # @return [String] next month folder name
56
+ def next_month(additional: '')
57
+ DateBaseName.new.to_yyyymm(Date.today >> 1, additional: additional)
58
+ end
59
+
60
+ # Get previous month folder name
61
+ #
62
+ # @param [String] additional additional name
63
+ # @return [String] previous month folder name
64
+ def previous_month(additional: '')
65
+ DateBaseName.new.to_yyyymm(Date.today << 1, additional: additional)
66
+ end
67
+
68
+ # Get months folder name list
69
+ #
70
+ # @param [String] month yyyymmdd format string
71
+ # @param [String] additional additional name
72
+ # @return [String] yesterday folder name
73
+ def months(month, count: 3, additional: '')
74
+ month = Date.new(month[0..3].to_i, month[4..5].to_i)
75
+ DateBaseName.new.to_yyyymm_list(month, count: count, additional: additional)
76
+ end
77
+
78
+ # Get this year folder name
79
+ #
80
+ # @param [String] additional additional name
81
+ # @return [String] this year folder name
82
+ def this_year(additional: '')
83
+ DateBaseName.new.to_yyyy(Date.today, additional: additional)
84
+ end
85
+
86
+ # Get next year folder name
87
+ #
88
+ # @param [String] additional additional name
89
+ # @return [String] next year folder name
90
+ def next_year(additional: '')
91
+ DateBaseName.new.to_yyyy(Date.today >> 12, additional: additional)
92
+ end
93
+
94
+ # Get previous year folder name
95
+ #
96
+ # @param [String] additional additional name
97
+ # @return [String] previous year folder name
98
+ def previous_year(additional: '')
99
+ DateBaseName.new.to_yyyy(Date.today << 12, additional: additional)
100
+ end
101
+
102
+ # Get years folder name list
103
+ #
104
+ # @param [String] year yyyymmdd format string
105
+ # @param [String] additional additional name
106
+ # @return [String] yesterday folder name
107
+ def years(year, count: 3, additional: '')
108
+ year = Date.new(year[0..3].to_i)
109
+ DateBaseName.new.to_yyyy_list(year, count: count, additional: additional)
110
+ end
111
+ end
112
+ end
113
+ # rubocop enable LineLength
data/rubocop-todo.yml ADDED
@@ -0,0 +1,26 @@
1
+ # This configuration was generated by `rubocop --auto-gen-config`
2
+ # on 2014-10-02 00:42:00 +0900 using RuboCop version 0.21.0.
3
+ # The point is for the user to remove these configuration records
4
+ # one by one as the offenses are removed from the code base.
5
+ # Note that changes in the inspected code, or installation of new
6
+ # versions of RuboCop, may require this file to be generated again.
7
+
8
+ # Offense count: 1
9
+ Documentation:
10
+ Enabled: false
11
+
12
+ # Offense count: 1
13
+ EmptyLinesAroundAccessModifier:
14
+ Enabled: false
15
+
16
+ # Offense count: 38
17
+ LineLength:
18
+ Max: 159
19
+
20
+ # Offense count: 2
21
+ RegexpLiteral:
22
+ MaxSlashes: 0
23
+
24
+ # Offense count: 36
25
+ UnusedMethodArgument:
26
+ Enabled: false
@@ -0,0 +1,482 @@
1
+ # encoding: utf-8
2
+ require 'spec_helper'
3
+ require 'date_base_name'
4
+ require 'date'
5
+
6
+ describe Defoker::DateBaseName do
7
+ context :to_yyyymmdd do
8
+ cases = [
9
+ {
10
+ case_no: 1,
11
+ case_title: 'no addition, 1digit month, 1digit day case',
12
+ date: Date.new(2014, 9, 9),
13
+ additional: '',
14
+ expected: '20140909'
15
+ },
16
+ {
17
+ case_no: 2,
18
+ case_title: 'no addition, 2digit month, 1digit day case',
19
+ date: Date.new(2014, 12, 9),
20
+ additional: '',
21
+ expected: '20141209'
22
+ },
23
+ {
24
+ case_no: 3,
25
+ case_title: 'no addition, 1digit month, 2digit day case',
26
+ date: Date.new(2014, 9, 12),
27
+ additional: '',
28
+ expected: '20140912'
29
+ },
30
+ {
31
+ case_no: 4,
32
+ case_title: 'with addition case',
33
+ date: Date.new(2014, 9, 12),
34
+ additional: 'hoge',
35
+ expected: '20140912_hoge'
36
+ },
37
+ {
38
+ case_no: 5,
39
+ case_title: 'omit addition case',
40
+ date: Date.new(2014, 9, 12),
41
+ additional: nil,
42
+ expected: '20140912'
43
+ },
44
+ {
45
+ case_no: 6,
46
+ case_title: 'invalid date case',
47
+ date: '20140912',
48
+ expect_error: true
49
+ },
50
+ {
51
+ case_no: 7,
52
+ case_title: 'datetime case',
53
+ date: DateTime.new(2014, 9, 12),
54
+ additional: nil,
55
+ expected: '20140912'
56
+ }
57
+ ]
58
+
59
+ cases.each do |c|
60
+ it "|case_no=#{c[:case_no]}|case_title=#{c[:case_title]}" do
61
+ begin
62
+ case_before c
63
+
64
+ # -- given --
65
+ defoker = Defoker::DateBaseName.new
66
+
67
+ # -- when --
68
+ actual =
69
+ if c[:additional]
70
+ defoker.to_yyyymmdd(c[:date], additional: c[:additional])
71
+ else
72
+ if c[:expect_error]
73
+ expect { defoker.to_yyyymmdd(c[:date]) }.to raise_error(ArgumentError)
74
+ next
75
+ end
76
+ defoker.to_yyyymmdd(c[:date])
77
+ end
78
+
79
+ # -- then --
80
+ expect(actual).to eq(c[:expected])
81
+ ensure
82
+ case_after c
83
+ end
84
+ end
85
+
86
+ def case_before(c)
87
+ # implement each case before
88
+ end
89
+
90
+ def case_after(c)
91
+ # implement each case after
92
+ end
93
+ end
94
+ end
95
+
96
+ context :to_yyyymmdd_list do
97
+ cases = [
98
+ {
99
+ case_no: 1,
100
+ case_title: 'no addition 4days case',
101
+ day: Date.new(2014, 9, 9),
102
+ count: 4,
103
+ expected: %w(20140909 20140910 20140911 20140912)
104
+ },
105
+ {
106
+ case_no: 2,
107
+ case_title: 'no addition 1day case',
108
+ day: Date.new(2014, 9, 12),
109
+ count: 1,
110
+ expected: ['20140912']
111
+ },
112
+ {
113
+ case_no: 3,
114
+ case_title: 'with addition 4days case',
115
+ day: Date.new(2014, 9, 9),
116
+ count: 4,
117
+ additional: 'hoge',
118
+ expected: %w(20140909_hoge 20140910_hoge 20140911_hoge 20140912_hoge)
119
+ },
120
+ {
121
+ case_no: 4,
122
+ case_title: 'invalid date case',
123
+ day: '20140912',
124
+ expect_error: true
125
+ }
126
+ ]
127
+
128
+ cases.each do |c|
129
+ it "|case_no=#{c[:case_no]}|case_title=#{c[:case_title]}" do
130
+ begin
131
+ case_before c
132
+
133
+ # -- given --
134
+ defoker = Defoker::DateBaseName.new
135
+
136
+ # -- when --
137
+ actual =
138
+ if c[:count] && c[:additional]
139
+ defoker.to_yyyymmdd_list(c[:day], count: c[:count], additional: c[:additional])
140
+ elsif c[:count]
141
+ defoker.to_yyyymmdd_list(c[:day], count: c[:count])
142
+ elsif c[:additional]
143
+ defoker.to_yyyymmdd_list(c[:day], additional: c[:additional])
144
+ else
145
+ if c[:expect_error]
146
+ expect { defoker.to_yyyymmdd_list(c[:day]) }.to raise_error(ArgumentError)
147
+ next
148
+ end
149
+ defoker.to_yyyymmdd_list(c[:day])
150
+ end
151
+
152
+ # -- then --
153
+ expect(actual).to eq(c[:expected])
154
+ ensure
155
+ case_after c
156
+ end
157
+ end
158
+
159
+ def case_before(c)
160
+ # implement each case before
161
+ end
162
+
163
+ def case_after(c)
164
+ # implement each case after
165
+ end
166
+ end
167
+ end
168
+
169
+ context :to_yyyymm do
170
+ cases = [
171
+ {
172
+ case_no: 1,
173
+ case_title: 'no addition, 1digit month',
174
+ month: Date.new(2014, 9),
175
+ additional: '',
176
+ expected: '201409'
177
+ },
178
+ {
179
+ case_no: 2,
180
+ case_title: 'no addition, 2digit month',
181
+ month: Date.new(2014, 12),
182
+ additional: '',
183
+ expected: '201412'
184
+ },
185
+ {
186
+ case_no: 4,
187
+ case_title: 'with addition case',
188
+ month: Date.new(2014, 9),
189
+ additional: 'hoge',
190
+ expected: '201409_hoge'
191
+ },
192
+ {
193
+ case_no: 5,
194
+ case_title: 'omit addition case',
195
+ month: Date.new(2014, 9),
196
+ additional: nil,
197
+ expected: '201409'
198
+ },
199
+ {
200
+ case_no: 6,
201
+ case_title: 'invalid month case',
202
+ month: '201409',
203
+ expect_error: true
204
+ },
205
+ {
206
+ case_no: 7,
207
+ case_title: 'datetime case',
208
+ month: DateTime.new(2014, 9),
209
+ additional: nil,
210
+ expected: '201409'
211
+ }
212
+ ]
213
+
214
+ cases.each do |c|
215
+ it "|case_no=#{c[:case_no]}|case_title=#{c[:case_title]}" do
216
+ begin
217
+ case_before c
218
+
219
+ # -- given --
220
+ defoker = Defoker::DateBaseName.new
221
+
222
+ # -- when --
223
+ actual =
224
+ if c[:additional]
225
+ defoker.to_yyyymm(c[:month], additional: c[:additional])
226
+ else
227
+ if c[:expect_error]
228
+ expect { defoker.to_yyyymm(c[:month]) }.to raise_error(ArgumentError)
229
+ next
230
+ end
231
+ defoker.to_yyyymm(c[:month])
232
+ end
233
+
234
+ # -- then --
235
+ expect(actual).to eq(c[:expected])
236
+ ensure
237
+ case_after c
238
+ end
239
+ end
240
+
241
+ def case_before(c)
242
+ # implement each case before
243
+ end
244
+
245
+ def case_after(c)
246
+ # implement each case after
247
+ end
248
+ end
249
+ end
250
+
251
+ context :to_yyyymm_list do
252
+ cases = [
253
+ {
254
+ case_no: 1,
255
+ case_title: 'no addition 4months case',
256
+ month: Date.new(2014, 10),
257
+ count: 4,
258
+ expected: %w(201410 201411 201412 201501)
259
+ },
260
+ {
261
+ case_no: 2,
262
+ case_title: 'no addition 1month case',
263
+ month: Date.new(2014, 9),
264
+ count: 1,
265
+ expected: ['201409']
266
+ },
267
+ {
268
+ case_no: 3,
269
+ case_title: 'with addition 4months case',
270
+ month: Date.new(2014, 10),
271
+ count: 4,
272
+ additional: 'hoge',
273
+ expected: %w(201410_hoge 201411_hoge 201412_hoge 201501_hoge)
274
+ },
275
+ {
276
+ case_no: 4,
277
+ case_title: 'no addition, no count, 3months case',
278
+ month: Date.new(2014, 10),
279
+ expected: %w(201410 201411 201412)
280
+ },
281
+ {
282
+ case_no: 5,
283
+ case_title: 'invalid month case',
284
+ month: '201409',
285
+ expect_error: true
286
+ }
287
+ ]
288
+
289
+ cases.each do |c|
290
+ it "|case_no=#{c[:case_no]}|case_title=#{c[:case_title]}" do
291
+ begin
292
+ case_before c
293
+
294
+ # -- given --
295
+ defoker = Defoker::DateBaseName.new
296
+
297
+ # -- when --
298
+ actual =
299
+ if c[:count] && c[:additional]
300
+ defoker.to_yyyymm_list(c[:month], count: c[:count], additional: c[:additional])
301
+ elsif c[:count]
302
+ defoker.to_yyyymm_list(c[:month], count: c[:count])
303
+ elsif c[:additional]
304
+ defoker.to_yyyymm_list(c[:month], additional: c[:additional])
305
+ else
306
+ if c[:expect_error]
307
+ expect { defoker.to_yyyymm_list(c[:month]) }.to raise_error(ArgumentError)
308
+ next
309
+ end
310
+ defoker.to_yyyymm_list(c[:month])
311
+ end
312
+
313
+ # -- then --
314
+ expect(actual).to eq(c[:expected])
315
+ ensure
316
+ case_after c
317
+ end
318
+ end
319
+
320
+ def case_before(c)
321
+ # implement each case before
322
+ end
323
+
324
+ def case_after(c)
325
+ # implement each case after
326
+ end
327
+ end
328
+ end
329
+
330
+ context :to_yyyy do
331
+ cases = [
332
+ {
333
+ case_no: 1,
334
+ case_title: 'no addition case',
335
+ year: Date.new(2014),
336
+ additional: '',
337
+ expected: '2014'
338
+ },
339
+ {
340
+ case_no: 2,
341
+ case_title: 'with addition case',
342
+ year: Date.new(2014),
343
+ additional: 'hoge',
344
+ expected: '2014_hoge'
345
+ },
346
+ {
347
+ case_no: 3,
348
+ case_title: 'omit addition case',
349
+ year: Date.new(2014),
350
+ expected: '2014'
351
+ },
352
+ {
353
+ case_no: 4,
354
+ case_title: 'invalid year case',
355
+ year: '2014',
356
+ expect_error: true
357
+ },
358
+ {
359
+ case_no: 5,
360
+ case_title: 'datetime case',
361
+ year: DateTime.new(2014),
362
+ additional: nil,
363
+ expected: '2014'
364
+ }
365
+ ]
366
+
367
+ cases.each do |c|
368
+ it "|case_no=#{c[:case_no]}|case_title=#{c[:case_title]}" do
369
+ begin
370
+ case_before c
371
+
372
+ # -- given --
373
+ defoker = Defoker::DateBaseName.new
374
+
375
+ # -- when --
376
+ actual =
377
+ if c[:additional]
378
+ defoker.to_yyyy(c[:year], additional: c[:additional])
379
+ else
380
+ if c[:expect_error]
381
+ expect { defoker.to_yyyy(c[:year]) }.to raise_error(ArgumentError)
382
+ next
383
+ end
384
+ defoker.to_yyyy(c[:year])
385
+ end
386
+
387
+ # -- then --
388
+ expect(actual).to eq(c[:expected])
389
+ ensure
390
+ case_after c
391
+ end
392
+ end
393
+
394
+ def case_before(c)
395
+ # implement each case before
396
+ end
397
+
398
+ def case_after(c)
399
+ # implement each case after
400
+ end
401
+ end
402
+ end
403
+
404
+ context :to_yyyy_list do
405
+ cases = [
406
+ {
407
+ case_no: 1,
408
+ case_title: 'no addition 4years case',
409
+ year: Date.new(2014),
410
+ count: 4,
411
+ expected: %w(2014 2015 2016 2017)
412
+ },
413
+ {
414
+ case_no: 2,
415
+ case_title: 'no addition 1year case',
416
+ year: Date.new(2014),
417
+ count: 1,
418
+ expected: ['2014']
419
+ },
420
+ {
421
+ case_no: 3,
422
+ case_title: 'with addition 4years case',
423
+ year: Date.new(2014, 10),
424
+ count: 4,
425
+ additional: 'hoge',
426
+ expected: %w(2014_hoge 2015_hoge 2016_hoge 2017_hoge)
427
+ },
428
+ {
429
+ case_no: 4,
430
+ case_title: 'no addition, no count, 3years case',
431
+ year: Date.new(2014),
432
+ expected: %w(2014 2015 2016)
433
+ },
434
+ {
435
+ case_no: 5,
436
+ case_title: 'invalid year case',
437
+ from_or_range: '2014',
438
+ expect_error: true
439
+ }
440
+ ]
441
+
442
+ cases.each do |c|
443
+ it "|case_no=#{c[:case_no]}|case_title=#{c[:case_title]}" do
444
+ begin
445
+ case_before c
446
+
447
+ # -- given --
448
+ defoker = Defoker::DateBaseName.new
449
+
450
+ # -- when --
451
+ actual =
452
+ if c[:count] && c[:additional]
453
+ defoker.to_yyyy_list(c[:year], count: c[:count], additional: c[:additional])
454
+ elsif c[:count]
455
+ defoker.to_yyyy_list(c[:year], count: c[:count])
456
+ elsif c[:additional]
457
+ defoker.to_yyyy_list(c[:year], additional: c[:additional])
458
+ else
459
+ if c[:expect_error]
460
+ expect { defoker.to_yyyy_list(c[:year]) }.to raise_error(ArgumentError)
461
+ next
462
+ end
463
+ defoker.to_yyyy_list(c[:year])
464
+ end
465
+
466
+ # -- then --
467
+ expect(actual).to eq(c[:expected])
468
+ ensure
469
+ case_after c
470
+ end
471
+ end
472
+
473
+ def case_before(c)
474
+ # implement each case before
475
+ end
476
+
477
+ def case_after(c)
478
+ # implement each case after
479
+ end
480
+ end
481
+ end
482
+ end