zakuro 0.6.0 → 0.6.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (50) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +16 -3
  3. data/VERSION +1 -1
  4. data/lib/zakuro/calculation/base/gengou.rb +4 -4
  5. data/lib/zakuro/calculation/base/linear_gengou.rb +6 -6
  6. data/lib/zakuro/calculation/era/gengou/internal/connector.rb +107 -0
  7. data/lib/zakuro/calculation/era/gengou/internal/counter.rb +214 -0
  8. data/lib/zakuro/calculation/era/gengou/internal/publisher.rb +104 -0
  9. data/lib/zakuro/calculation/era/gengou/internal/reserve/list.rb +244 -0
  10. data/lib/zakuro/calculation/era/gengou/internal/reserve/range.rb +193 -0
  11. data/lib/zakuro/calculation/{gengou → era/gengou}/internal/reserve.rb +6 -6
  12. data/lib/zakuro/calculation/{gengou → era/gengou}/scroll.rb +44 -95
  13. data/lib/zakuro/calculation/{version → era/version}/internal/crawler.rb +7 -7
  14. data/lib/zakuro/calculation/{version → era/version}/internal/range.rb +4 -4
  15. data/lib/zakuro/calculation/{version → era/version}/version.rb +3 -3
  16. data/lib/zakuro/calculation/monthly/month.rb +2 -2
  17. data/lib/zakuro/calculation/range/full_range.rb +18 -19
  18. data/lib/zakuro/calculation/range/operated_range.rb +3 -4
  19. data/lib/zakuro/calculation/specifier/internal/month.rb +84 -0
  20. data/lib/zakuro/calculation/specifier/multiple_day.rb +162 -0
  21. data/lib/zakuro/calculation/specifier/single_day.rb +2 -2
  22. data/lib/zakuro/calculation/summary/internal/operation.rb +98 -0
  23. data/lib/zakuro/calculation/summary/range.rb +120 -0
  24. data/lib/zakuro/calculation/summary/single.rb +45 -67
  25. data/lib/zakuro/condition.rb +14 -0
  26. data/lib/zakuro/era/japan/gengou/alignment/aligner.rb +106 -0
  27. data/lib/zakuro/era/japan/gengou/alignment/division.rb +165 -0
  28. data/lib/zakuro/era/japan/gengou/alignment/line.rb +156 -0
  29. data/lib/zakuro/era/japan/gengou/alignment/linear_gengou.rb +184 -0
  30. data/lib/zakuro/era/japan/gengou/alignment.rb +42 -0
  31. data/lib/zakuro/era/japan/gengou/resource/parser.rb +242 -0
  32. data/lib/zakuro/era/japan/gengou/resource/type.rb +292 -0
  33. data/lib/zakuro/era/japan/gengou/resource/validator.rb +346 -0
  34. data/lib/zakuro/era/japan/gengou/{yaml → resource/yaml}/set-001-until-south.yaml +2 -2
  35. data/lib/zakuro/era/japan/gengou/{yaml → resource/yaml}/set-002-from-north.yaml +2 -2
  36. data/lib/zakuro/era/japan/gengou/{yaml → resource/yaml}/set-003-modern.yaml +2 -2
  37. data/lib/zakuro/era/japan/gengou/resource.rb +32 -0
  38. data/lib/zakuro/era/japan/gengou.rb +34 -73
  39. data/lib/zakuro/era/japan/version.rb +18 -18
  40. data/lib/zakuro/merchant.rb +41 -3
  41. data/lib/zakuro/operation/month/validator.rb +1 -1
  42. data/lib/zakuro/result/result.rb +17 -0
  43. data/lib/zakuro/version/senmyou/README.md +11 -7
  44. metadata +32 -20
  45. data/lib/zakuro/calculation/gengou/internal/counter.rb +0 -129
  46. data/lib/zakuro/calculation/gengou/internal/reserve/interval.rb +0 -183
  47. data/lib/zakuro/calculation/gengou/internal/reserve/list.rb +0 -382
  48. data/lib/zakuro/era/japan/gengou/parser.rb +0 -237
  49. data/lib/zakuro/era/japan/gengou/type.rb +0 -285
  50. data/lib/zakuro/era/japan/gengou/validator.rb +0 -341
@@ -1,237 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require_relative '../../western/calendar'
4
- require_relative './type'
5
- require_relative './validator'
6
- require 'yaml'
7
-
8
- # :nodoc:
9
- module Zakuro
10
- #
11
- # Japan 和暦
12
- #
13
- module Japan
14
- # :reek:TooManyInstanceVariables { max_instance_variables: 5 }
15
-
16
- #
17
- # Parser yaml解析
18
- #
19
- module Parser
20
- #
21
- # GengouParser 元号情報の検証/展開を行う
22
- #
23
- class GengouParser
24
- # @return [Integer] 要素位置
25
- attr_reader :index
26
- # @return [String] 元号名
27
- attr_reader :name
28
- # @return [Hash<String, String>] 開始年
29
- attr_reader :both_start_year
30
- # @return [Hash<String, String>] 開始日
31
- attr_reader :both_start_date
32
-
33
- #
34
- # 初期化
35
- #
36
- # @param [Hash<String, Strin>] hash 元号情報
37
- # @param [Integer] index (元号セット内での)元号の要素位置
38
- #
39
- def initialize(hash:, index:)
40
- @index = index
41
- @name = hash['name']
42
- @both_start_year = hash['start_year']
43
- @both_start_date = hash['start_date']
44
- end
45
-
46
- #
47
- # 元号情報を生成する
48
- #
49
- # @return [Gengou] 元号情報
50
- #
51
- def create
52
- both_start_year = Both::YearParser.new(hash: @both_start_year).create
53
- both_start_date = Both::DateParser.new(hash: @both_start_date).create
54
-
55
- Gengou.new(name: @name, both_start_year: both_start_year,
56
- both_start_date: both_start_date)
57
- end
58
- end
59
-
60
- #
61
- # SetParser 元号セット情報の検証/展開
62
- #
63
- class SetParser
64
- # @return [String] 元号セットID
65
- attr_reader :id
66
- # @return [String] 元号セット名
67
- attr_reader :name
68
- # @return [Hash<String, String>] 終了年
69
- attr_reader :both_end_year
70
- # @return [Hash<String, String>] 終了日
71
- attr_reader :both_end_date
72
- # @return [Array<Hash<String, String>>] 元号情報
73
- attr_reader :list
74
-
75
- #
76
- # 初期化
77
- #
78
- # @param [Hash<String, Object>] hash 元号セット情報
79
- #
80
- def initialize(hash:)
81
- @id = hash['id']
82
- @name = hash['name']
83
- @both_end_year = hash['end_year']
84
- @both_end_date = hash['end_date']
85
- @list = hash['list']
86
- end
87
-
88
- #
89
- # 元号セット情報を生成する
90
- #
91
- # @return [Set] 元号セット情報
92
- #
93
- def create
94
- both_end_date = Both::DateParser.new(hash: @both_end_date).create
95
- list = create_list
96
- Set.new(
97
- id: @id, name: @name, both_end_date: both_end_date, list: list
98
- )
99
- end
100
-
101
- # :reek:TooManyStatements { max_statements: 7 }
102
-
103
- #
104
- # 元号情報を生成する
105
- #
106
- # @return [Array<Gengou>] 元号情報
107
- #
108
- def create_list
109
- result = []
110
- @list.each_with_index do |li, index|
111
- gengou = GengouParser.new(hash: li, index: index).create
112
- next_index = index + 1
113
- gengou = calc_end_date_on_gengou_data(next_index: next_index,
114
- gengou: gengou)
115
- result.push(gengou)
116
- end
117
-
118
- result
119
- end
120
-
121
- # :reek:TooManyStatements { max_statements: 6 }
122
-
123
- #
124
- # 次の元号の開始日から、元号の終了日に変換する
125
- #
126
- # @param [Integer] next_index 次の元号の要素位置
127
- # @param [String] gengou 次回開始日
128
- #
129
- # @return [Gengou] 元号情報
130
- #
131
- def calc_end_date_on_gengou_data(next_index:, gengou:)
132
- if next_index >= @list.size
133
- gengou.write_end_year(end_year: @both_end_year['western'])
134
- end_date = Western::Calendar.parse(str: @both_end_date['western'])
135
- gengou.write_end_date(end_date: end_date)
136
- return gengou
137
- end
138
- next_item = @list[next_index]
139
- gengou.convert_next_start_year_to_end_year(
140
- next_start_year: next_item['start_year']['western']
141
- )
142
- gengou.convert_next_start_date_to_end_date(
143
- next_start_date: next_item['start_date']['western']
144
- )
145
- gengou
146
- end
147
- end
148
-
149
- #
150
- # 和暦/西暦
151
- #
152
- module Both
153
- #
154
- # YearParser 年
155
- #
156
- class YearParser
157
- # @return [Integer] 和暦元号年
158
- attr_reader :japan
159
- # @return [Integer] 西暦年
160
- attr_reader :western
161
-
162
- #
163
- # 初期化
164
- #
165
- # @param [Hash<String, Object>] hash 年情報
166
- #
167
- def initialize(hash:)
168
- @japan = hash['japan']
169
- @western = hash['western']
170
- end
171
-
172
- #
173
- # 年情報を生成する
174
- #
175
- # @return [Both::Year] 年情報
176
- #
177
- def create
178
- japan = @japan.to_i
179
- western = @western.to_i
180
-
181
- Japan::Both::Year.new(japan: japan, western: western)
182
- end
183
- end
184
-
185
- #
186
- # DateParser 日
187
- #
188
- class DateParser
189
- # @return [Japan::Calendar] 和暦日
190
- attr_reader :japan
191
- # @return [Western::Calendar] 西暦日
192
- attr_reader :western
193
-
194
- #
195
- # 初期化
196
- #
197
- # @param [Hash<String, Object>] hash 日情報
198
- #
199
- def initialize(hash:)
200
- @japan = hash['japan']
201
- @western = hash['western']
202
- end
203
-
204
- #
205
- # 日情報を生成する
206
- #
207
- # @return [Both::Date] 日情報
208
- #
209
- def create
210
- japan = Japan::Calendar.parse(text: @japan)
211
- western = Western::Calendar.parse(str: @western)
212
-
213
- Japan::Both::Date.new(japan: japan, western: western)
214
- end
215
- end
216
- end
217
-
218
- #
219
- # 解析/展開する
220
- #
221
- # @param [String] filepath 元号セットファイルパス
222
- #
223
- # @return [Set] 元号セット情報
224
- #
225
- def self.run(filepath: '')
226
- yaml = YAML.load_file(filepath)
227
-
228
- failed = Validator.run(yaml_hash: yaml)
229
-
230
- raise ArgumentError, failed.join("\n") unless failed.empty?
231
-
232
- parser = SetParser.new(hash: yaml)
233
- parser.create
234
- end
235
- end
236
- end
237
- end
@@ -1,285 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require_relative '../../western/calendar'
4
-
5
- # :nodoc:
6
- module Zakuro
7
- #
8
- # Japan 和暦
9
- #
10
- module Japan
11
- #
12
- # Gengou 元号情報
13
- #
14
- class Gengou
15
- # @return [String] 元号名
16
- attr_reader :name
17
- # @return [Both::Year] 開始年(和暦/西暦)
18
- attr_reader :both_start_year
19
- # @return [Both::Date] 開始日(和暦/西暦)
20
- attr_reader :both_start_date
21
- # @return [Integer] 終了年
22
- attr_reader :end_year
23
- # @return [Western::Calendar] 終了日
24
- attr_reader :end_date
25
-
26
- #
27
- # 初期化
28
- #
29
- # @param [String] name 元号名
30
- # @param [Both::Year] both_start_year 開始年(和暦/西暦)
31
- # @param [Both::Date] both_start_date 開始日(和暦/西暦)
32
- # @param [Integer] end_date 終了年
33
- # @param [Western::Calendar] end_date 終了日
34
- #
35
- def initialize(name: '', both_start_year: Both::Year.new,
36
- both_start_date: Both::Date.new,
37
- end_date: Western::Calendar.new,
38
- end_year: Both::Year::INVALID)
39
- @name = name
40
- @both_start_year = both_start_year
41
- @both_start_date = both_start_date
42
- @end_year = end_year
43
- @end_date = end_date
44
- end
45
-
46
- #
47
- # 終了年を更新する
48
- #
49
- # @param [Integer] end_year 終了年
50
- #
51
- def write_end_year(end_year:)
52
- unless Gengou.valid_year(year: end_year)
53
- raise ArgumentError, "invalid year format. [#{end_year}]"
54
- end
55
-
56
- @end_year = end_year
57
- end
58
-
59
- #
60
- # 終了日を更新する
61
- #
62
- # @param [Western::Calendar] end_date 終了日
63
- #
64
- def write_end_date(end_date:)
65
- unless Gengou.valid_date(date: end_date)
66
- raise ArgumentError, "invalid date format. [#{end_date}]"
67
- end
68
-
69
- @end_date = end_date
70
- nil
71
- end
72
-
73
- #
74
- # 年が不正なしかどうかを確認する
75
- #
76
- # @param [Integer] year 年
77
- #
78
- # @return [True] 不正なし
79
- # @return [False] 不正
80
- #
81
- def self.valid_year(year:)
82
- return false unless year
83
-
84
- year.is_a?(Integer)
85
- end
86
-
87
- #
88
- # 日付が不正なしかどうかを確認する
89
- #
90
- # @param [Western::Calendar] date 日付
91
- #
92
- # @return [True] 不正なし
93
- # @return [False] 不正
94
- #
95
- def self.valid_date(date:)
96
- return false unless date
97
-
98
- date.is_a?(Western::Calendar)
99
- end
100
-
101
- #
102
- # 次の元号の開始年から、元号の終了年に変換する
103
- #
104
- # @param [Integer] next_start_year 次回開始年
105
- #
106
- def convert_next_start_year_to_end_year(next_start_year:)
107
- if @both_start_year.western >= next_start_year
108
- @end_year = next_start_year
109
- return
110
- end
111
-
112
- @end_year = next_start_year - 1
113
-
114
- nil
115
- end
116
-
117
- #
118
- # 次の元号の開始日から、元号の終了日に変換する
119
- #
120
- # @param [String] next_start_date 次回開始日
121
- #
122
- def convert_next_start_date_to_end_date(next_start_date: '')
123
- raise ArgumentError, 'empty string cannot convert' if next_start_date.empty?
124
-
125
- start_date = Western::Calendar.parse(str: next_start_date)
126
- @end_date = start_date - 1
127
-
128
- nil
129
- end
130
-
131
- #
132
- # 指定した日が元号に含まれるか
133
- #
134
- # @param [Western::Calendar] date 日
135
- #
136
- # @return [True] 含まれる
137
- # @return [False] 含まれない
138
- #
139
- def include?(date:)
140
- date >= @both_start_date.western && date <= @end_date
141
- end
142
-
143
- #
144
- # 不正か
145
- #
146
- # @return [True] 不正
147
- # @return [False] 不正なし
148
- #
149
- def invalid?
150
- @both_start_year.japan == -1 || @both_start_year.invalid? ||
151
- @both_start_date.invalid? || @end_date.invalid?
152
- end
153
-
154
- #
155
- # 1元号年を追加する
156
- #
157
- # def next_year
158
- # @year += 1 unless invalid?
159
- # nil
160
- # end
161
-
162
- def to_s
163
- "name: #{@name}, both_start_year: #{@both_start_year.format}, " \
164
- "both_start_date: #{@both_start_date.format}, end_date: #{@end_date.format}"
165
- end
166
- end
167
-
168
- #
169
- # Set 元号セット
170
- #
171
- class Set
172
- # @return [Integer] 不正値
173
- INVALID = -1
174
- # @return [Integer] 元号セットID
175
- attr_reader :id
176
- # @return [String] 元号セット名
177
- attr_reader :name
178
- # @return [Both::Date] 元号セットでの終了年
179
- attr_reader :both_end_year
180
- # @return [Both::Date] 元号セットでの終了日
181
- attr_reader :both_end_date
182
- # @return [Array<Gengou>] 元号リスト
183
- attr_reader :list
184
-
185
- #
186
- # 初期化
187
- #
188
- # @param [Integer] id 元号セットID
189
- # @param [String] name 元号セット名
190
- # @param [Western::Calendar] end_date 元号セットでの終了日
191
- # @param [Array<Gengou>] list 元号リスト
192
- #
193
- def initialize(id: INVALID, name: '', both_end_year: Both::Year.new,
194
- both_end_date: Both::Date.new, list: [])
195
- @id = id
196
- @name = name
197
- @both_end_year = both_end_year
198
- @both_end_date = both_end_date
199
- @list = list
200
- end
201
-
202
- #
203
- # 指定した日付を含む元号を返す
204
- #
205
- # @param [Western::Calendar] date 日
206
- #
207
- # @return [Gengou] 元号
208
- #
209
- def include_item(date:)
210
- @list.each do |item|
211
- return item if item.include?(date: date)
212
- end
213
-
214
- Gengou.new
215
- end
216
-
217
- #
218
- # 元号セットが不正かどうかを確認する
219
- #
220
- # @return [True] 正しくない
221
- # @return [False] 正しい
222
- #
223
- def invalid?
224
- @id == INVALID
225
- end
226
- end
227
-
228
- #
229
- # 和暦/西暦
230
- #
231
- module Both
232
- #
233
- # Year 年
234
- #
235
- class Year
236
- # @return [Integer] 不正値
237
- INVALID = -1
238
- # @return [Integer] 和暦元号年
239
- attr_reader :japan
240
- # @return [Integer] 西暦年
241
- attr_reader :western
242
-
243
- def initialize(japan: INVALID, western: INVALID)
244
- @japan = japan
245
- @western = western
246
- end
247
-
248
- #
249
- # 不正か
250
- #
251
- # @return [True] 不正
252
- # @return [False] 不正なし
253
- #
254
- def invalid?
255
- @japan == INVALID || @western == INVALID
256
- end
257
- end
258
-
259
- #
260
- # Date 日
261
- #
262
- class Date
263
- # @return [Japan::Calendar] 和暦日
264
- attr_reader :japan
265
- # @return [Western::Calendar] 西暦日
266
- attr_reader :western
267
-
268
- def initialize(japan: Japan::Calendar.new, western: Western::Calendar.new)
269
- @japan = japan
270
- @western = western
271
- end
272
-
273
- #
274
- # 不正か
275
- #
276
- # @return [True] 不正
277
- # @return [False] 不正なし
278
- #
279
- def invalid?
280
- @japan.invalid? || @western.invalid?
281
- end
282
- end
283
- end
284
- end
285
- end