zakuro 0.1.0 → 0.1.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.
Binary file
@@ -5,12 +5,16 @@ require 'yaml'
5
5
 
6
6
  table = CSV.read('csv/month.csv', headers: true)
7
7
 
8
+ # :reek:UtilityFunction
9
+
8
10
  def month_str_to_number(str)
9
11
  return str if str == '-'
10
12
 
11
13
  str.match('([0-9]{1,2})')[0]
12
14
  end
13
15
 
16
+ # :reek:UtilityFunction
17
+
14
18
  def month_str_to_leaped(str)
15
19
  return str if str == '-'
16
20
 
@@ -23,8 +27,8 @@ result = []
23
27
  table.each do |row|
24
28
  hash = {
25
29
  'id' => row['ID'],
26
- 'relation_id' => row['関連内容ID'],
27
- 'parent_id' => row['ID'],
30
+ 'relation_id' => row['関連注釈ID'],
31
+ 'parent_id' => row['親注釈ID'],
28
32
  'page' => row['頁数'],
29
33
  'number' => row['注番'],
30
34
  'japan_date' => row['和暦'],
@@ -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
@@ -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
+ 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