zakuro 0.4.0 → 0.6.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (102) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +32 -3
  3. data/VERSION +1 -0
  4. data/doc/operation/csv/month.csv +21 -0
  5. data/doc/operation/operation.xlsx +0 -0
  6. data/lib/zakuro/calculation/base/gengou.rb +82 -0
  7. data/lib/zakuro/calculation/base/linear_gengou.rb +76 -0
  8. data/lib/zakuro/calculation/base/operated_year.rb +5 -15
  9. data/lib/zakuro/calculation/base/year.rb +26 -14
  10. data/lib/zakuro/calculation/cycle/abstract_remainder.rb +0 -13
  11. data/lib/zakuro/calculation/era/gengou/internal/connector.rb +107 -0
  12. data/lib/zakuro/calculation/era/gengou/internal/counter.rb +214 -0
  13. data/lib/zakuro/calculation/era/gengou/internal/publisher.rb +104 -0
  14. data/lib/zakuro/calculation/era/gengou/internal/reserve/list.rb +244 -0
  15. data/lib/zakuro/calculation/era/gengou/internal/reserve/range.rb +193 -0
  16. data/lib/zakuro/calculation/era/gengou/internal/reserve.rb +42 -0
  17. data/lib/zakuro/calculation/era/gengou/scroll.rb +211 -0
  18. data/lib/zakuro/calculation/era/version/internal/crawler.rb +51 -0
  19. data/lib/zakuro/calculation/era/version/internal/range.rb +39 -0
  20. data/lib/zakuro/calculation/era/version/version.rb +24 -0
  21. data/lib/zakuro/calculation/monthly/first_day.rb +3 -2
  22. data/lib/zakuro/calculation/monthly/month.rb +49 -2
  23. data/lib/zakuro/calculation/monthly/operated_month.rb +2 -2
  24. data/lib/zakuro/calculation/range/full_range.rb +68 -107
  25. data/lib/zakuro/calculation/range/operated_range.rb +13 -9
  26. data/lib/zakuro/calculation/range/operated_solar_terms.rb +36 -17
  27. data/lib/zakuro/calculation/range/transfer/gengou_scroller.rb +54 -0
  28. data/lib/zakuro/calculation/range/transfer/year_boundary.rb +25 -21
  29. data/lib/zakuro/calculation/specifier/internal/month.rb +84 -0
  30. data/lib/zakuro/calculation/specifier/multiple_day.rb +162 -0
  31. data/lib/zakuro/calculation/specifier/single_day.rb +15 -37
  32. data/lib/zakuro/calculation/summary/internal/operation.rb +98 -0
  33. data/lib/zakuro/calculation/summary/range.rb +120 -0
  34. data/lib/zakuro/calculation/summary/single.rb +45 -64
  35. data/lib/zakuro/condition.rb +14 -0
  36. data/lib/zakuro/era/japan/calendar.rb +133 -0
  37. data/lib/zakuro/era/japan/gengou/alignment/aligner.rb +106 -0
  38. data/lib/zakuro/era/japan/gengou/alignment/division.rb +165 -0
  39. data/lib/zakuro/era/japan/gengou/alignment/line.rb +156 -0
  40. data/lib/zakuro/era/japan/gengou/alignment/linear_gengou.rb +184 -0
  41. data/lib/zakuro/era/japan/gengou/alignment.rb +42 -0
  42. data/lib/zakuro/era/japan/gengou/resource/parser.rb +242 -0
  43. data/lib/zakuro/era/japan/gengou/resource/type.rb +292 -0
  44. data/lib/zakuro/era/japan/gengou/resource/validator.rb +346 -0
  45. data/lib/zakuro/era/japan/gengou/resource/yaml/set-001-until-south.yaml +1870 -0
  46. data/lib/zakuro/era/japan/gengou/resource/yaml/set-002-from-north.yaml +810 -0
  47. data/lib/zakuro/era/japan/gengou/resource/yaml/set-003-modern.yaml +50 -0
  48. data/lib/zakuro/era/japan/gengou/resource.rb +32 -0
  49. data/lib/zakuro/era/japan/gengou.rb +35 -74
  50. data/lib/zakuro/era/japan/version.rb +151 -0
  51. data/lib/zakuro/era/{western.rb → western/calendar.rb} +0 -0
  52. data/lib/zakuro/merchant.rb +50 -3
  53. data/lib/zakuro/operation/month/parser.rb +1 -1
  54. data/lib/zakuro/operation/month/type.rb +1 -1
  55. data/lib/zakuro/operation/month/validator.rb +2 -2
  56. data/lib/zakuro/operation/yaml/month.yaml +672 -0
  57. data/lib/zakuro/output/response.rb +5 -5
  58. data/lib/zakuro/result/result.rb +17 -0
  59. data/lib/zakuro/tools/typeconv.rb +38 -0
  60. data/lib/zakuro/tools/typeof.rb +4 -1
  61. data/lib/zakuro/version/context.rb +24 -3
  62. data/lib/zakuro/version/daien/cycle/remainder.rb +26 -0
  63. data/lib/zakuro/version/daien/daien.rb +1 -26
  64. data/lib/zakuro/version/daien/stella/solar/average.rb +2 -2
  65. data/lib/zakuro/version/genka/const/number.rb +43 -0
  66. data/lib/zakuro/version/genka/const/remainder.rb +33 -0
  67. data/lib/zakuro/version/genka/cycle/remainder.rb +92 -0
  68. data/lib/zakuro/version/genka/cycle/solar_term.rb +34 -0
  69. data/lib/zakuro/version/genka/genka.rb +1 -9
  70. data/lib/zakuro/version/genka/monthly/lunar_phase.rb +95 -0
  71. data/lib/zakuro/version/genka/range/annual_range.rb +77 -0
  72. data/lib/zakuro/version/genka/stella/origin/first_term.rb +55 -0
  73. data/lib/zakuro/version/genka/stella/origin/january.rb +49 -0
  74. data/lib/zakuro/version/genka/stella/solar/average.rb +41 -0
  75. data/lib/zakuro/version/gihou/cycle/remainder.rb +26 -0
  76. data/lib/zakuro/version/gihou/gihou.rb +1 -30
  77. data/lib/zakuro/version/gihou/stella/solar/average.rb +2 -2
  78. data/lib/zakuro/version/gregorio/gregorio.rb +1 -9
  79. data/lib/zakuro/version/houryaku/houryaku.rb +1 -9
  80. data/lib/zakuro/version/joukyou/joukyou.rb +1 -9
  81. data/lib/zakuro/version/kansei/kansei.rb +1 -9
  82. data/lib/zakuro/version/senmyou/README.md +11 -7
  83. data/lib/zakuro/version/senmyou/cycle/remainder.rb +26 -0
  84. data/lib/zakuro/version/senmyou/senmyou.rb +1 -30
  85. data/lib/zakuro/version/senmyou/stella/solar/average.rb +2 -2
  86. data/lib/zakuro/version/tenpou/tenpou.rb +1 -9
  87. data/lib/zakuro/version/version_class_resolver.rb +4 -0
  88. data/zakuro.gemspec +1 -3
  89. metadata +45 -16
  90. data/lib/zakuro/calculation/base/multi_gengou.rb +0 -101
  91. data/lib/zakuro/calculation/base/multi_gengou_roller.rb +0 -218
  92. data/lib/zakuro/calculation/range/transfer/western_date_allocation.rb +0 -82
  93. data/lib/zakuro/era/japan/gengou/parser.rb +0 -167
  94. data/lib/zakuro/era/japan/gengou/type.rb +0 -178
  95. data/lib/zakuro/era/japan/gengou/validator.rb +0 -236
  96. data/lib/zakuro/era/japan/reki.rb +0 -91
  97. data/lib/zakuro/era/japan/yaml/set-001-until-south.yaml +0 -1121
  98. data/lib/zakuro/era/japan/yaml/set-002-from-north.yaml +0 -485
  99. data/lib/zakuro/era/japan/yaml/set-003-modern.yaml +0 -28
  100. data/lib/zakuro/version/abstract_version.rb +0 -29
  101. data/lib/zakuro/version.rb +0 -7
  102. data/lib/zakuro/version_factory.rb +0 -59
@@ -0,0 +1,184 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative '../../../western/calendar'
4
+
5
+ require_relative '../resource/type'
6
+
7
+ # :nodoc:
8
+ module Zakuro
9
+ # :nodoc:
10
+ module Japan
11
+ # :nodoc:
12
+ module Alignment
13
+ #
14
+ # LinearGengou 直列元号
15
+ #
16
+ class LinearGengou
17
+ INVALID_YEAR = -1
18
+
19
+ # @return [Western::Calendar] 開始日
20
+ attr_reader :start_date
21
+ # @return [Western::Calendar] 終了日
22
+ attr_reader :last_date
23
+ # @return [Gengou] 元号
24
+ attr_reader :gengou
25
+
26
+ #
27
+ # 初期化
28
+ #
29
+ # @param [Western::Calendar] start_date 開始日
30
+ # @param [Western::Calendar] last_date 終了日
31
+ # @param [Resource::Gengou] gengou 元号
32
+ #
33
+ def initialize(start_date: Western::Calendar.new, last_date: Western::Calendar.new,
34
+ gengou: Resource::Gengou.new)
35
+ @gengou = gengou
36
+ @start_date = start_date.invalid? ? native_start_date : start_date
37
+ @last_date = last_date.invalid? ? native_last_date : last_date
38
+ end
39
+
40
+ #
41
+ # 元号名を取得する
42
+ #
43
+ # @return [String] 元号名
44
+ #
45
+ def name
46
+ @gengou.name
47
+ end
48
+
49
+ #
50
+ # 不正か
51
+ #
52
+ # @return [True] 不正
53
+ # @return [False] 不正なし
54
+ #
55
+ def invalid?
56
+ @gengou.invalid?
57
+ end
58
+
59
+ #
60
+ # 範囲内か
61
+ #
62
+ # @param [Western::Calendar] date 西暦日
63
+ #
64
+ # @return [True] 範囲内
65
+ # @return [False] 範囲外
66
+ #
67
+ def include?(date: Western::Calendar.new)
68
+ return false if invalid?
69
+
70
+ return false if @start_date.invalid?
71
+
72
+ return false if @last_date.invalid?
73
+
74
+ return false if date < @start_date
75
+
76
+ return false if date > @last_date
77
+
78
+ true
79
+ end
80
+
81
+ #
82
+ # 完全に範囲内か
83
+ #
84
+ # @param [Western::Calendar] start_date 開始日
85
+ # @param [Western::Calendar] last_date 終了日
86
+ #
87
+ # @return [True] 範囲内
88
+ # @return [False] 範囲外あり
89
+ #
90
+ def in?(start_date:, last_date:)
91
+ @start_date <= start_date && last_date <= @last_date
92
+ end
93
+
94
+ #
95
+ # 完全に範囲外か
96
+ #
97
+ # @param [Western::Calendar] start_date 開始日
98
+ # @param [Western::Calendar] last_date 終了日
99
+ #
100
+ # @return [True] 範囲外
101
+ # @return [False] 範囲内あり
102
+ #
103
+ def out?(start_date:, last_date:)
104
+ # 範囲より前
105
+ return true if start_date < @start_date && last_date < @start_date
106
+
107
+ # 範囲より後
108
+ return true if @last_date < start_date && @last_date < last_date
109
+
110
+ false
111
+ end
112
+
113
+ #
114
+ # 完全に範囲を超えているか
115
+ #
116
+ # @param [Western::Calendar] start_date 開始日
117
+ # @param [Western::Calendar] last_date 終了日
118
+ #
119
+ # @return [True] 完全超過
120
+ # @return [True] 完全超過せず
121
+ #
122
+ def covered?(start_date:, last_date:)
123
+ start_date < @start_date && @last_date < last_date
124
+ end
125
+
126
+ #
127
+ # 元は1繋ぎであった元号が別の行に存在するか(設定値から変更されているか)?
128
+ #
129
+ # @return [True] 存在する
130
+ # @return [False] 存在しない
131
+ #
132
+ def changed?
133
+ return true if change_start_date?
134
+
135
+ return true if change_last_date?
136
+
137
+ false
138
+ end
139
+
140
+ #
141
+ # 開始日が設定された開始日と異なるか(行が変更されているか)
142
+ #
143
+ # @return [True] 異なる
144
+ # @return [False] 同一
145
+ #
146
+ def change_start_date?
147
+ return false if invalid?
148
+
149
+ @start_date != native_start_date
150
+ end
151
+
152
+ #
153
+ # 終了日が設定された終了日と異なるか(行が変更されているか)
154
+ #
155
+ # @return [True] 異なる
156
+ # @return [False] 同一
157
+ #
158
+ def change_last_date?
159
+ return false if invalid?
160
+
161
+ @last_date != native_last_date
162
+ end
163
+
164
+ #
165
+ # 設定された元号の開始日を取得する
166
+ #
167
+ # @return [Western::Calendar]設定された元号の開始日
168
+ #
169
+ def native_start_date
170
+ @gengou.both_start_date.western
171
+ end
172
+
173
+ #
174
+ # 設定された元号の終了日を取得する
175
+ #
176
+ # @return [Western::Calendar] 設定された元号の終了日
177
+ #
178
+ def native_last_date
179
+ @gengou.last_date
180
+ end
181
+ end
182
+ end
183
+ end
184
+ end
@@ -0,0 +1,42 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative './alignment/aligner'
4
+
5
+ require_relative './resource'
6
+
7
+ # :nodoc:
8
+ module Zakuro
9
+ #
10
+ # Japan 和暦
11
+ #
12
+ module Japan
13
+ #
14
+ # Alignment 整列
15
+ #
16
+ module Alignment
17
+ # @return [Integer] 1行目元号
18
+ FIRST_LINE = Aligner::FIRST_LINE
19
+
20
+ # @return [Integer] 2行目元号
21
+ SECOND_LINE = Aligner::SECOND_LINE
22
+
23
+ # @return [Aligner] 整列結果
24
+ SUMMARY = Aligner.new(resources: Resource::LIST)
25
+
26
+ #
27
+ # 指定した範囲内の元号を取得する
28
+ #
29
+ # @param [Integer] line 行
30
+ # @param [Western::Calendar] start_date 開始日
31
+ # @param [Western::Calendar] last_date 終了日
32
+ #
33
+ # @return [Array<LinearGengou>] 元号
34
+ #
35
+ def self.get(line: FIRST_LINE,
36
+ start_date: Western::Calendar.new, last_date: Western::Calendar.new)
37
+
38
+ SUMMARY.get(line: line, start_date: start_date, last_date: last_date)
39
+ end
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,242 @@
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
+ #
15
+ # Resource yaml解析結果
16
+ #
17
+ module Resource
18
+ # :reek:TooManyInstanceVariables { max_instance_variables: 5 }
19
+
20
+ #
21
+ # Parser yaml解析
22
+ #
23
+ module Parser
24
+ #
25
+ # GengouParser 元号情報の検証/展開を行う
26
+ #
27
+ class GengouParser
28
+ # @return [Integer] 要素位置
29
+ attr_reader :index
30
+ # @return [String] 元号名
31
+ attr_reader :name
32
+ # @return [Hash<String, String>] 開始年
33
+ attr_reader :both_start_year
34
+ # @return [Hash<String, String>] 開始日
35
+ attr_reader :both_start_date
36
+
37
+ #
38
+ # 初期化
39
+ #
40
+ # @param [Hash<String, Strin>] hash 元号情報
41
+ # @param [Integer] index (元号セット内での)元号の要素位置
42
+ #
43
+ def initialize(hash:, index:)
44
+ @index = index
45
+ @name = hash['name']
46
+ @both_start_year = hash['start_year']
47
+ @both_start_date = hash['start_date']
48
+ end
49
+
50
+ #
51
+ # 元号情報を生成する
52
+ #
53
+ # @return [Gengou] 元号情報
54
+ #
55
+ def create
56
+ both_start_year = Both::YearParser.new(hash: @both_start_year).create
57
+ both_start_date = Both::DateParser.new(hash: @both_start_date).create
58
+
59
+ Gengou.new(name: @name, both_start_year: both_start_year,
60
+ both_start_date: both_start_date)
61
+ end
62
+ end
63
+
64
+ #
65
+ # SetParser 元号セット情報の検証/展開
66
+ #
67
+ class SetParser
68
+ # @return [String] 元号セットID
69
+ attr_reader :id
70
+ # @return [String] 元号セット名
71
+ attr_reader :name
72
+ # @return [Hash<String, String>] 終了年
73
+ attr_reader :both_last_year
74
+ # @return [Hash<String, String>] 終了日
75
+ attr_reader :both_last_date
76
+ # @return [Array<Hash<String, String>>] 元号情報
77
+ attr_reader :list
78
+
79
+ #
80
+ # 初期化
81
+ #
82
+ # @param [Hash<String, Object>] hash 元号セット情報
83
+ #
84
+ def initialize(hash:)
85
+ @id = hash['id']
86
+ @name = hash['name']
87
+ @both_last_year = hash['last_year']
88
+ @both_last_date = hash['last_date']
89
+ @list = hash['list']
90
+ end
91
+
92
+ #
93
+ # 元号セット情報を生成する
94
+ #
95
+ # @return [Set] 元号セット情報
96
+ #
97
+ def create
98
+ both_last_date = Both::DateParser.new(hash: @both_last_date).create
99
+ list = create_list
100
+ Set.new(
101
+ id: @id, name: @name, both_last_date: both_last_date, list: list
102
+ )
103
+ end
104
+
105
+ # :reek:TooManyStatements { max_statements: 7 }
106
+
107
+ #
108
+ # 元号情報を生成する
109
+ #
110
+ # @return [Array<Gengou>] 元号情報
111
+ #
112
+ def create_list
113
+ result = []
114
+ @list.each_with_index do |li, index|
115
+ gengou = GengouParser.new(hash: li, index: index).create
116
+ next_index = index + 1
117
+ gengou = calc_last_date_on_gengou_data(next_index: next_index,
118
+ gengou: gengou)
119
+ result.push(gengou)
120
+ end
121
+
122
+ result
123
+ end
124
+
125
+ # :reek:TooManyStatements { max_statements: 6 }
126
+
127
+ #
128
+ # 次の元号の開始日から、元号の終了日に変換する
129
+ #
130
+ # @param [Integer] next_index 次の元号の要素位置
131
+ # @param [String] gengou 次回開始日
132
+ #
133
+ # @return [Gengou] 元号情報
134
+ #
135
+ def calc_last_date_on_gengou_data(next_index:, gengou:)
136
+ if next_index >= @list.size
137
+ gengou.write_last_year(last_year: @both_last_year['western'])
138
+ last_date = Western::Calendar.parse(str: @both_last_date['western'])
139
+ gengou.write_last_date(last_date: last_date)
140
+ return gengou
141
+ end
142
+ next_item = @list[next_index]
143
+ gengou.convert_next_start_year_to_last_year(
144
+ next_start_year: next_item['start_year']['western']
145
+ )
146
+ gengou.convert_next_start_date_to_last_date(
147
+ next_start_date: next_item['start_date']['western']
148
+ )
149
+ gengou
150
+ end
151
+ end
152
+
153
+ #
154
+ # 和暦/西暦
155
+ #
156
+ module Both
157
+ #
158
+ # YearParser 年
159
+ #
160
+ class YearParser
161
+ # @return [Integer] 和暦元号年
162
+ attr_reader :japan
163
+ # @return [Integer] 西暦年
164
+ attr_reader :western
165
+
166
+ #
167
+ # 初期化
168
+ #
169
+ # @param [Hash<String, Object>] hash 年情報
170
+ #
171
+ def initialize(hash:)
172
+ @japan = hash['japan']
173
+ @western = hash['western']
174
+ end
175
+
176
+ #
177
+ # 年情報を生成する
178
+ #
179
+ # @return [Both::Year] 年情報
180
+ #
181
+ def create
182
+ japan = @japan.to_i
183
+ western = @western.to_i
184
+
185
+ Japan::Resource::Both::Year.new(japan: japan, western: western)
186
+ end
187
+ end
188
+
189
+ #
190
+ # DateParser 日
191
+ #
192
+ class DateParser
193
+ # @return [Japan::Calendar] 和暦日
194
+ attr_reader :japan
195
+ # @return [Western::Calendar] 西暦日
196
+ attr_reader :western
197
+
198
+ #
199
+ # 初期化
200
+ #
201
+ # @param [Hash<String, Object>] hash 日情報
202
+ #
203
+ def initialize(hash:)
204
+ @japan = hash['japan']
205
+ @western = hash['western']
206
+ end
207
+
208
+ #
209
+ # 日情報を生成する
210
+ #
211
+ # @return [Both::Date] 日情報
212
+ #
213
+ def create
214
+ japan = Japan::Calendar.parse(text: @japan)
215
+ western = Western::Calendar.parse(str: @western)
216
+
217
+ Japan::Resource::Both::Date.new(japan: japan, western: western)
218
+ end
219
+ end
220
+ end
221
+
222
+ #
223
+ # 解析/展開する
224
+ #
225
+ # @param [String] filepath 元号セットファイルパス
226
+ #
227
+ # @return [Set] 元号セット情報
228
+ #
229
+ def self.run(filepath: '')
230
+ yaml = YAML.load_file(filepath)
231
+
232
+ failed = Validator.run(yaml_hash: yaml)
233
+
234
+ raise ArgumentError, failed.join("\n") unless failed.empty?
235
+
236
+ parser = SetParser.new(hash: yaml)
237
+ parser.create
238
+ end
239
+ end
240
+ end
241
+ end
242
+ end