zakuro 0.1.0 → 0.1.5

Sign up to get free protection for your applications and to get access to all the features.
Files changed (86) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +30 -12
  3. data/doc/operation/csv/month.csv +202 -202
  4. data/doc/operation/operation.xlsx +0 -0
  5. data/doc/operation/transfer.rb +6 -2
  6. data/lib/zakuro/calculation/base/multi_gengou.rb +101 -0
  7. data/lib/zakuro/calculation/base/multi_gengou_roller.rb +218 -0
  8. data/lib/zakuro/calculation/base/year.rb +107 -0
  9. data/lib/zakuro/calculation/cycle/abstract_remainder.rb +471 -0
  10. data/lib/zakuro/calculation/cycle/abstract_solar_term.rb +173 -0
  11. data/lib/zakuro/calculation/cycle/zodiac.rb +106 -0
  12. data/lib/zakuro/calculation/monthly/first_day.rb +45 -0
  13. data/lib/zakuro/calculation/monthly/initialized_month.rb +125 -0
  14. data/lib/zakuro/calculation/monthly/month.rb +187 -0
  15. data/lib/zakuro/calculation/monthly/month_label.rb +88 -0
  16. data/lib/zakuro/calculation/monthly/operated_month.rb +209 -0
  17. data/lib/zakuro/calculation/range/full_range.rb +210 -0
  18. data/lib/zakuro/calculation/range/operated_range.rb +144 -0
  19. data/lib/zakuro/calculation/range/operated_solar_terms.rb +201 -0
  20. data/lib/zakuro/calculation/range/transfer/western_date_allocation.rb +82 -0
  21. data/lib/zakuro/calculation/range/transfer/year_boundary.rb +146 -0
  22. data/lib/zakuro/calculation/specifier/single_day.rb +109 -0
  23. data/lib/zakuro/calculation/summary/single.rb +129 -0
  24. data/lib/zakuro/condition.rb +16 -13
  25. data/lib/zakuro/era/japan/gengou/parser.rb +1 -3
  26. data/lib/zakuro/era/japan/gengou/type.rb +3 -3
  27. data/lib/zakuro/era/japan/gengou/validator.rb +15 -13
  28. data/lib/zakuro/merchant.rb +2 -2
  29. data/lib/zakuro/operation/month/parser.rb +132 -36
  30. data/lib/zakuro/operation/month/type.rb +11 -10
  31. data/lib/zakuro/operation/month/validator.rb +332 -28
  32. data/lib/zakuro/operation/operation.rb +21 -0
  33. data/lib/zakuro/operation/yaml/month.yaml +1 -1
  34. data/lib/zakuro/output/error.rb +7 -6
  35. data/lib/zakuro/output/logger.rb +50 -49
  36. data/lib/zakuro/output/response.rb +145 -144
  37. data/lib/zakuro/result/operation.rb +64 -36
  38. data/lib/zakuro/tools/stringifier.rb +2 -2
  39. data/lib/zakuro/tools/typeof.rb +2 -2
  40. data/lib/zakuro/version.rb +1 -1
  41. data/lib/zakuro/version/abstract_version.rb +1 -1
  42. data/lib/zakuro/version/context.rb +23 -0
  43. data/lib/zakuro/version/senmyou/const/number.rb +51 -0
  44. data/lib/zakuro/version/senmyou/const/remainder.rb +43 -0
  45. data/lib/zakuro/version/senmyou/cycle/remainder.rb +61 -0
  46. data/lib/zakuro/version/senmyou/cycle/solar_term.rb +31 -0
  47. data/lib/zakuro/version/senmyou/monthly/lunar_phase.rb +166 -181
  48. data/lib/zakuro/version/senmyou/range/annual_range.rb +86 -159
  49. data/lib/zakuro/version/senmyou/senmyou.rb +10 -4
  50. data/lib/zakuro/version/senmyou/stella/lunar/adjustment.rb +237 -0
  51. data/lib/zakuro/version/senmyou/stella/lunar/localization.rb +45 -0
  52. data/lib/zakuro/version/senmyou/stella/lunar/location.rb +108 -0
  53. data/lib/zakuro/version/senmyou/stella/lunar/value.rb +89 -0
  54. data/lib/zakuro/version/senmyou/stella/origin/average_november.rb +34 -0
  55. data/lib/zakuro/version/senmyou/stella/origin/lunar_age.rb +62 -0
  56. data/lib/zakuro/version/senmyou/stella/origin/winter_solstice.rb +55 -0
  57. data/lib/zakuro/version/senmyou/stella/solar/adjustment.rb +93 -0
  58. data/lib/zakuro/version/senmyou/stella/solar/average.rb +175 -0
  59. data/lib/zakuro/version/senmyou/stella/solar/interval.rb +103 -0
  60. data/lib/zakuro/version/senmyou/stella/solar/location.rb +164 -0
  61. data/lib/zakuro/version/senmyou/stella/solar/value.rb +138 -0
  62. data/lib/zakuro/version/version_class_resolver.rb +62 -0
  63. data/lib/zakuro/version_factory.rb +2 -2
  64. metadata +38 -24
  65. data/lib/zakuro/cycle/abstract_remainder.rb +0 -456
  66. data/lib/zakuro/cycle/zodiac.rb +0 -103
  67. data/lib/zakuro/version/senmyou/base/era.rb +0 -83
  68. data/lib/zakuro/version/senmyou/base/multi_gengou.rb +0 -98
  69. data/lib/zakuro/version/senmyou/base/multi_gengou_roller.rb +0 -217
  70. data/lib/zakuro/version/senmyou/base/remainder.rb +0 -60
  71. data/lib/zakuro/version/senmyou/base/solar_term.rb +0 -76
  72. data/lib/zakuro/version/senmyou/base/year.rb +0 -104
  73. data/lib/zakuro/version/senmyou/monthly/first_day.rb +0 -44
  74. data/lib/zakuro/version/senmyou/monthly/initialized_month.rb +0 -48
  75. data/lib/zakuro/version/senmyou/monthly/month.rb +0 -181
  76. data/lib/zakuro/version/senmyou/monthly/month_label.rb +0 -87
  77. data/lib/zakuro/version/senmyou/monthly/operated_month.rb +0 -167
  78. data/lib/zakuro/version/senmyou/range/full_range.rb +0 -324
  79. data/lib/zakuro/version/senmyou/range/operated_range.rb +0 -105
  80. data/lib/zakuro/version/senmyou/range/operated_solar_terms.rb +0 -163
  81. data/lib/zakuro/version/senmyou/specifier/single_day_specifier.rb +0 -99
  82. data/lib/zakuro/version/senmyou/stella/lunar_orbit.rb +0 -332
  83. data/lib/zakuro/version/senmyou/stella/solar_average.rb +0 -214
  84. data/lib/zakuro/version/senmyou/stella/solar_orbit.rb +0 -394
  85. data/lib/zakuro/version/senmyou/stella/winter_solstice.rb +0 -106
  86. data/lib/zakuro/version/senmyou/summary/single.rb +0 -71
@@ -25,8 +25,6 @@ module Zakuro
25
25
  @date = date
26
26
  end
27
27
 
28
- # :reek:NilCheck
29
-
30
28
  #
31
29
  # 検証する
32
30
  #
@@ -36,7 +34,9 @@ module Zakuro
36
34
  #
37
35
  def self.validate(date:)
38
36
  failed = []
39
- return failed if date.nil? || date.is_a?(Date) || date.is_a?(String)
37
+ return failed unless date
38
+
39
+ return failed if date.is_a?(Date) || date.is_a?(String)
40
40
 
41
41
  failed.push("invalid date: #{date}")
42
42
  failed
@@ -64,7 +64,7 @@ module Zakuro
64
64
  @last = hash[:last]
65
65
  end
66
66
 
67
- # :reek:TooManyStatements { max_statements: 7 } and :reek:NilCheck
67
+ # :reek:TooManyStatements { max_statements: 7 }
68
68
 
69
69
  #
70
70
  # 検証する
@@ -75,7 +75,7 @@ module Zakuro
75
75
  #
76
76
  def self.validate(hash:)
77
77
  failed = []
78
- return failed if hash.nil?
78
+ return failed unless hash
79
79
 
80
80
  unless hash.is_a?(Hash)
81
81
  failed.push("invalid range type. #{hash}. should be hash")
@@ -106,8 +106,6 @@ module Zakuro
106
106
  @columns = columns
107
107
  end
108
108
 
109
- # :reek:NilCheck
110
-
111
109
  #
112
110
  # 検証する
113
111
  #
@@ -118,7 +116,10 @@ module Zakuro
118
116
  def self.validate(columns:)
119
117
  # TODO: 列内容のバリデーション
120
118
  failed = []
121
- return failed if columns.nil? || columns.is_a?(Array)
119
+
120
+ return failed unless columns
121
+
122
+ return failed if columns.is_a?(Array)
122
123
 
123
124
  failed.push("invalid columns type. #{columns}. should be array")
124
125
 
@@ -148,7 +149,6 @@ module Zakuro
148
149
  end
149
150
 
150
151
  # TODO: オプションキーのバリデーション
151
- # :reek:NilCheck
152
152
 
153
153
  #
154
154
  # 検証する
@@ -159,7 +159,9 @@ module Zakuro
159
159
  #
160
160
  def self.validate(options:)
161
161
  failed = []
162
- return failed if options.nil? || options.is_a?(Hash)
162
+ return failed unless options
163
+
164
+ return failed if options.is_a?(Hash)
163
165
 
164
166
  failed.push("invalid options type. #{options}. should be hash")
165
167
 
@@ -222,8 +224,6 @@ module Zakuro
222
224
  failed
223
225
  end
224
226
 
225
- # :reek:NilCheck
226
-
227
227
  #
228
228
  # 上書きする
229
229
  #
@@ -233,7 +233,10 @@ module Zakuro
233
233
  instance_variables.each do |var|
234
234
  key = var.to_s.delete('@')
235
235
  val = hash[key.intern]
236
- instance_variable_set(var, val) unless val.nil?
236
+
237
+ next unless val
238
+
239
+ instance_variable_set(var, val)
237
240
  end
238
241
  end
239
242
  end
@@ -48,8 +48,6 @@ module Zakuro
48
48
  @start_year = hash['start_year']
49
49
  end
50
50
 
51
- # :reek:NilCheck
52
-
53
51
  #
54
52
  # 元号情報を生成する
55
53
  #
@@ -58,7 +56,7 @@ module Zakuro
58
56
  def create
59
57
  start_date = Western::Calendar.parse(str: @start_date)
60
58
  new_year_date = Western::Calendar.parse(str: @new_year_date)
61
- start_year = @start_year.nil? ? 1 : @start_year
59
+ start_year = @start_year || 1
62
60
 
63
61
  Gengou.new(name: @name, start_date: start_date, new_year_date: new_year_date,
64
62
  year: start_year)
@@ -58,8 +58,6 @@ module Zakuro
58
58
  nil
59
59
  end
60
60
 
61
- # :reek:NilCheck
62
-
63
61
  #
64
62
  # 日付が有効かどうかを確認する
65
63
  #
@@ -69,7 +67,9 @@ module Zakuro
69
67
  # @return [False] 無効
70
68
  #
71
69
  def self.valid_date(date:)
72
- !date.nil? && date.is_a?(Western::Calendar)
70
+ return false unless date
71
+
72
+ date.is_a?(Western::Calendar)
73
73
  end
74
74
 
75
75
  #
@@ -60,8 +60,6 @@ module Zakuro
60
60
  failed
61
61
  end
62
62
 
63
- # :reek:NilCheck
64
-
65
63
  #
66
64
  # IDを検証する
67
65
  #
@@ -69,7 +67,9 @@ module Zakuro
69
67
  # @return [False] 正しくない
70
68
  #
71
69
  def id?
72
- !(@id.nil? || !@id.is_a?(Integer))
70
+ return false unless @id
71
+
72
+ @id.is_a?(Integer)
73
73
  end
74
74
 
75
75
  #
@@ -79,7 +79,9 @@ module Zakuro
79
79
  # @return [False] 正しくない
80
80
  #
81
81
  def name?
82
- !(@name.nil? || !@name.is_a?(String))
82
+ return false unless @name
83
+
84
+ @name.is_a?(String)
83
85
  end
84
86
 
85
87
  #
@@ -92,8 +94,6 @@ module Zakuro
92
94
  Western::Calendar.valid_date_string(str: @end_date)
93
95
  end
94
96
 
95
- # :reek:NilCheck
96
-
97
97
  #
98
98
  # 元号情報を検証する
99
99
  #
@@ -101,7 +101,9 @@ module Zakuro
101
101
  # @return [False] 正しくない
102
102
  #
103
103
  def list?
104
- (!@list.nil? || @list.is_a?(Array))
104
+ return false unless @list
105
+
106
+ @list.is_a?(Array)
105
107
  end
106
108
 
107
109
  #
@@ -121,6 +123,8 @@ module Zakuro
121
123
  end
122
124
  end
123
125
 
126
+ # :reek:TooManyInstanceVariables { max_instance_variables: 5 }
127
+
124
128
  #
125
129
  # Gengou 元号情報
126
130
  #
@@ -172,8 +176,6 @@ module Zakuro
172
176
  failed
173
177
  end
174
178
 
175
- # :reek:NilCheck
176
-
177
179
  #
178
180
  # 元号名を検証する
179
181
  #
@@ -181,7 +183,9 @@ module Zakuro
181
183
  # @return [False] 正しくない
182
184
  #
183
185
  def name?
184
- (!@name.nil? || @name.is_a?(String))
186
+ return false unless @name
187
+
188
+ @name.is_a?(String)
185
189
  end
186
190
 
187
191
  #
@@ -204,8 +208,6 @@ module Zakuro
204
208
  Western::Calendar.valid_date_string(str: @new_year_date)
205
209
  end
206
210
 
207
- # :reek:NilCheck
208
-
209
211
  #
210
212
  # 元号年を検証する
211
213
  #
@@ -213,7 +215,7 @@ module Zakuro
213
215
  # @return [False] 正しくない
214
216
  #
215
217
  def year?
216
- return true if @start_year.nil?
218
+ return true unless @start_year
217
219
 
218
220
  @start_year.is_a?(Integer)
219
221
  end
@@ -22,7 +22,7 @@ module Zakuro
22
22
  #
23
23
  def initialize(condition: {})
24
24
  failed = Condition.validate(hash: condition)
25
- raise ZakuroError, failed.join('\n') unless failed.empty?
25
+ raise Output::ZakuroError, failed.join('\n') unless failed.empty?
26
26
 
27
27
  @condition = Condition.new(hash: condition)
28
28
  end
@@ -36,7 +36,7 @@ module Zakuro
36
36
  #
37
37
  def offer(condition: {})
38
38
  failed = Condition.validate(hash: condition)
39
- raise ZakuroError, failed.join('\n') unless failed.empty?
39
+ raise Output::ZakuroError, failed.join('\n') unless failed.empty?
40
40
 
41
41
  @condition.rewrite(hash: condition)
42
42
 
@@ -130,42 +130,30 @@ module Zakuro
130
130
  # @return [Array<MonthHistory>] 変更履歴
131
131
  #
132
132
  def self.load(yaml_hash: {})
133
- annotations = {}
134
- relations = {}
135
- histories = create_histories(yaml_hash: yaml_hash, annotations: annotations,
136
- relations: relations)
133
+ histories = create_histories(yaml_hash: yaml_hash)
137
134
 
138
- add_annotations(histories: histories, annotations: annotations, relations: relations)
135
+ annotation_parser = AnnotationParser.new(yaml_hash: yaml_hash)
136
+ annotation_parser.create
137
+
138
+ add_annotations(histories: histories, annotation_parser: annotation_parser)
139
139
  end
140
140
 
141
141
  #
142
142
  # 変更履歴を読み込む
143
143
  #
144
144
  # @param [Hash] yaml_hash 設定ファイルテキスト
145
- # @param [Hash] annotations 注釈(空)
146
- # @param [Hash] relations 関連ID設定(空)
147
145
  #
148
146
  # @return [Array<MonthHistory>] 変更履歴
149
147
  #
150
- def self.create_histories(yaml_hash: {}, annotations: {}, relations: {})
148
+ def self.create_histories(yaml_hash: {})
151
149
  result = []
152
150
  yaml_hash.each do |month|
153
- id = month['id']
154
- annotations[id] = Annotation.new(
155
- id: id,
156
- description: Operation::TypeParser.text(str: month['description']),
157
- note: Operation::TypeParser.text(str: month['note'])
158
- )
159
- relation_id = month['relation_id']
160
-
161
- relations[id] = relation_id unless Operation::TypeParser.invalid?(str: relation_id)
162
-
163
151
  next unless Operation::TypeParser.modified?(str: month['modified'])
164
152
 
165
- history = create_history(yaml_hash: month)
166
- result.push(history)
153
+ result.push(
154
+ create_history(yaml_hash: month)
155
+ )
167
156
  end
168
-
169
157
  result
170
158
  end
171
159
  private_class_method :create_histories
@@ -177,21 +165,22 @@ module Zakuro
177
165
  reference = Reference.new(page: yaml_hash['page'].to_i, number: yaml_hash['number'].to_i,
178
166
  japan_date: yaml_hash['japan_date'])
179
167
  MonthHistory.new(id: yaml_hash['id'],
180
- parent_id: yaml_hash['parent_id'],
168
+ parent_id: Operation::TypeParser.text(str: yaml_hash['parent_id']),
181
169
  reference: reference,
182
170
  western_date: western_date,
183
171
  diffs: diffs)
184
172
  end
185
173
  private_class_method :create_history
186
174
 
187
- def self.add_annotations(histories: [], annotations: {}, relations: {})
175
+ def self.add_annotations(annotation_parser:, histories: [])
188
176
  result = []
189
177
  histories.each do |history|
178
+ id = history.id
190
179
  result.push(
191
180
  MonthHistory.new(
192
- id: history.id, reference: history.reference, western_date: history.western_date,
193
- annotations: create_history_annnotations(history: history, annotations: annotations,
194
- relations: relations),
181
+ id: id, parent_id: history.parent_id, reference: history.reference,
182
+ western_date: history.western_date,
183
+ annotations: annotation_parser.specify(id: id),
195
184
  diffs: history.diffs
196
185
  )
197
186
  )
@@ -214,11 +203,100 @@ module Zakuro
214
203
 
215
204
  def self.add_annotation(history_annotations: [], annotations: {}, id: '')
216
205
  annotation = annotations.fetch(id, Annotation.new)
217
- history_annotations.push(annotation) unless annotation.invalid?
206
+ return if annotation.invalid?
207
+
208
+ history_annotations.push(annotation)
218
209
  end
219
210
  private_class_method :add_annotation
220
211
  end
221
212
 
213
+ #
214
+ # AnnotationParser 注釈解析
215
+ #
216
+ class AnnotationParser
217
+ # @return [Hash<String, Annotation>] 注釈
218
+ attr_reader :annotations
219
+ # @return [Hash<String, String>] 関連注釈の対応関係
220
+ attr_reader :relations
221
+
222
+ #
223
+ # 初期化
224
+ #
225
+ # @param [Hash] yaml_hash 設定ファイルテキスト
226
+ #
227
+ def initialize(yaml_hash: {})
228
+ @annotations = {}
229
+ @relations = {}
230
+ @yaml_hash = yaml_hash
231
+ end
232
+
233
+ #
234
+ # 注釈を生成する
235
+ #
236
+ def create
237
+ @yaml_hash.each do |month|
238
+ AnnotationParser.resolve_history(
239
+ hash: month, annotations: @annotations, relations: @relations
240
+ )
241
+ end
242
+ end
243
+
244
+ #
245
+ # 月別履歴情報から注釈情報を取り出す
246
+ #
247
+ # @param [<Type>] hash 月別履歴情報yaml
248
+ # @param [Hash<String, Annotation>] annotations 注釈
249
+ # @param [Hash<String, String>] relations 関連注釈の対応関係
250
+ #
251
+ def self.resolve_history(hash: {}, annotations: {}, relations: {})
252
+ id = hash['id']
253
+ annotations[id] = Annotation.new(
254
+ id: id,
255
+ description: Operation::TypeParser.text(str: hash['description']),
256
+ note: Operation::TypeParser.text(str: hash['note'])
257
+ )
258
+ relation_id = hash['relation_id']
259
+
260
+ return if Operation::TypeParser.invalid?(str: relation_id)
261
+
262
+ relations[id] = relation_id
263
+ end
264
+
265
+ #
266
+ # 注釈を特定する
267
+ #
268
+ # @param [String] id ID
269
+ #
270
+ # @return [Array<Annotation>] 注釈
271
+ #
272
+ def specify(id: '')
273
+ ids = [id, relations.fetch(id, '')]
274
+ specify_by_ids(ids: ids)
275
+ end
276
+
277
+ private
278
+
279
+ def specify_by_ids(ids: [])
280
+ annotations = []
281
+ ids.each do |id|
282
+ add_annotation(id: id, annotations: annotations)
283
+ end
284
+
285
+ annotations
286
+ end
287
+
288
+ def add_annotation(id: '', annotations: [])
289
+ annotation = annotation(id: id)
290
+ return if annotation.invalid?
291
+
292
+ annotations.push(annotation)
293
+ end
294
+
295
+ def annotation(id: '')
296
+ @annotations.fetch(id, Annotation.new)
297
+ end
298
+ end
299
+
222
300
  #
223
301
  # MonthDiffsParser 月情報(差分)解析
224
302
  #
@@ -259,19 +337,37 @@ module Zakuro
259
337
  private_class_method :create_destination_solar_term
260
338
 
261
339
  def self.create_month(yaml_hash: {})
262
- number = yaml_hash['number']
263
- leaped = yaml_hash['leaped']
264
- days = yaml_hash['days']
265
-
266
340
  Month.new(
267
- number: Number.new(calc: Operation::TypeParser.month_number(str: number['calc']),
268
- actual: Operation::TypeParser.month_number(str: number['actual'])),
269
- leaped: Leaped.new(calc: Operation::TypeParser.month_leaped(str: leaped['calc']),
270
- actual: Operation::TypeParser.month_leaped(str: leaped['actual'])),
271
- days: Days.new(calc: days['calc'], actual: days['actual'])
341
+ number: create_month_number(yaml_hash: yaml_hash['number']),
342
+ leaped: create_month_leaped(yaml_hash: yaml_hash['leaped']),
343
+ is_many_days: create_month_day(yaml_hash: yaml_hash['days'])
272
344
  )
273
345
  end
274
346
  private_class_method :create_month
347
+
348
+ def self.create_month_number(yaml_hash: {})
349
+ Number.new(
350
+ calc: Operation::TypeParser.month_number(str: yaml_hash['calc']),
351
+ actual: Operation::TypeParser.month_number(str: yaml_hash['actual'])
352
+ )
353
+ end
354
+ private_class_method :create_month_number
355
+
356
+ def self.create_month_leaped(yaml_hash: {})
357
+ Leaped.new(
358
+ calc: Operation::TypeParser.month_leaped(str: yaml_hash['calc']),
359
+ actual: Operation::TypeParser.month_leaped(str: yaml_hash['actual'])
360
+ )
361
+ end
362
+ private_class_method :create_month_leaped
363
+
364
+ def self.create_month_day(yaml_hash: {})
365
+ Days.new(
366
+ calc: yaml_hash['calc'],
367
+ actual: yaml_hash['actual']
368
+ )
369
+ end
370
+ private_class_method :create_month_day
275
371
  end
276
372
  end
277
373
  end