zakuro 0.6.0 → 0.7.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (142) hide show
  1. checksums.yaml +4 -4
  2. data/Makefile +3 -0
  3. data/README.md +58 -16
  4. data/VERSION +1 -1
  5. data/doc/dropped_date.md +93 -0
  6. data/doc/error.md +45 -0
  7. data/doc/operation/transfer.rb +8 -8
  8. data/lib/zakuro/calculation/base/day.rb +52 -0
  9. data/lib/zakuro/calculation/base/gengou.rb +50 -4
  10. data/lib/zakuro/calculation/base/linear_gengou.rb +6 -6
  11. data/lib/zakuro/calculation/base/operated_year.rb +3 -3
  12. data/lib/zakuro/calculation/base/year.rb +4 -4
  13. data/lib/zakuro/calculation/cycle/abstract_remainder.rb +5 -3
  14. data/lib/zakuro/calculation/era/gengou/abstract_scroll.rb +209 -0
  15. data/lib/zakuro/calculation/era/gengou/dated_scroll.rb +30 -0
  16. data/lib/zakuro/calculation/era/gengou/internal/connector.rb +107 -0
  17. data/lib/zakuro/calculation/era/gengou/internal/counter.rb +214 -0
  18. data/lib/zakuro/calculation/era/gengou/internal/publisher.rb +104 -0
  19. data/lib/zakuro/calculation/era/gengou/internal/reserve/abstract_list.rb +230 -0
  20. data/lib/zakuro/calculation/era/gengou/internal/reserve/abstract_range.rb +193 -0
  21. data/lib/zakuro/calculation/era/gengou/internal/reserve/dated_list.rb +48 -0
  22. data/lib/zakuro/calculation/era/gengou/internal/reserve/dated_range.rb +30 -0
  23. data/lib/zakuro/calculation/era/gengou/internal/reserve/empty_link.rb +158 -0
  24. data/lib/zakuro/calculation/era/gengou/internal/reserve/named_list.rb +125 -0
  25. data/lib/zakuro/calculation/era/gengou/internal/reserve/named_range.rb +75 -0
  26. data/lib/zakuro/calculation/{gengou → era/gengou}/internal/reserve.rb +6 -7
  27. data/lib/zakuro/calculation/era/gengou/named_scroll.rb +30 -0
  28. data/lib/zakuro/calculation/{version → era/version}/internal/crawler.rb +7 -7
  29. data/lib/zakuro/calculation/{version → era/version}/internal/range.rb +4 -4
  30. data/lib/zakuro/calculation/{version → era/version}/version.rb +3 -3
  31. data/lib/zakuro/calculation/monthly/initialized_month.rb +1 -1
  32. data/lib/zakuro/calculation/monthly/month.rb +44 -5
  33. data/lib/zakuro/calculation/monthly/operated_month.rb +2 -2
  34. data/lib/zakuro/calculation/option/dropped_date/abstract_parameter.rb +44 -0
  35. data/lib/zakuro/calculation/option/dropped_date/location.rb +135 -0
  36. data/lib/zakuro/calculation/range/{full_range.rb → abstract_full_range.rb} +29 -27
  37. data/lib/zakuro/calculation/range/{operated_range.rb → abstract_operation_range.rb} +11 -14
  38. data/lib/zakuro/calculation/range/dated_full_range.rb +32 -0
  39. data/lib/zakuro/calculation/range/dated_operation_range.rb +33 -0
  40. data/lib/zakuro/calculation/range/medieval_annual_range.rb +2 -2
  41. data/lib/zakuro/calculation/range/named_full_range.rb +38 -0
  42. data/lib/zakuro/calculation/range/named_operation_range.rb +36 -0
  43. data/lib/zakuro/calculation/range/operated_solar_terms.rb +9 -11
  44. data/lib/zakuro/calculation/range/transfer/year_boundary.rb +6 -6
  45. data/lib/zakuro/calculation/stella/solar/abstract_location.rb +2 -0
  46. data/lib/zakuro/calculation/summary/internal/day.rb +40 -0
  47. data/lib/zakuro/calculation/summary/internal/month.rb +92 -0
  48. data/lib/zakuro/calculation/summary/{single.rb → internal/operation.rb} +6 -40
  49. data/lib/zakuro/calculation/summary/internal/option.rb +85 -0
  50. data/lib/zakuro/calculation/summary/japan/range.rb +158 -0
  51. data/lib/zakuro/calculation/summary/japan/single.rb +118 -0
  52. data/lib/zakuro/calculation/summary/japan/specifier/single_day.rb +96 -0
  53. data/lib/zakuro/calculation/summary/western/range.rb +124 -0
  54. data/lib/zakuro/calculation/summary/western/single.rb +113 -0
  55. data/lib/zakuro/calculation/summary/western/specifier/multiple_day.rb +174 -0
  56. data/lib/zakuro/calculation/summary/western/specifier/single_day.rb +98 -0
  57. data/lib/zakuro/calculation/type/optional.rb +46 -0
  58. data/lib/zakuro/condition.rb +75 -22
  59. data/lib/zakuro/context/context.rb +44 -0
  60. data/lib/zakuro/context/option.rb +117 -0
  61. data/lib/zakuro/context/version_class_resolver.rb +110 -0
  62. data/lib/zakuro/era/japan/gengou/alignment/aligner.rb +125 -0
  63. data/lib/zakuro/era/japan/gengou/alignment/division.rb +168 -0
  64. data/lib/zakuro/era/japan/gengou/alignment/line.rb +175 -0
  65. data/lib/zakuro/era/japan/gengou/alignment/linear_gengou.rb +187 -0
  66. data/lib/zakuro/era/japan/gengou/alignment.rb +55 -0
  67. data/lib/zakuro/era/japan/gengou/resource/parser.rb +245 -0
  68. data/lib/zakuro/era/japan/gengou/resource/type.rb +299 -0
  69. data/lib/zakuro/era/japan/gengou/resource/validator.rb +347 -0
  70. data/lib/zakuro/era/japan/gengou/{yaml → resource/yaml}/set-001-until-south.yaml +2 -2
  71. data/lib/zakuro/era/japan/gengou/{yaml → resource/yaml}/set-002-from-north.yaml +2 -2
  72. data/lib/zakuro/era/japan/gengou/{yaml → resource/yaml}/set-003-modern.yaml +2 -2
  73. data/lib/zakuro/era/japan/gengou/resource.rb +33 -0
  74. data/lib/zakuro/era/japan/gengou.rb +25 -79
  75. data/lib/zakuro/era/japan/version.rb +18 -18
  76. data/lib/zakuro/era/western/calendar.rb +13 -9
  77. data/lib/zakuro/exception/case/pattern.rb +71 -0
  78. data/lib/zakuro/exception/case/preset.rb +51 -0
  79. data/lib/zakuro/exception/case/template.rb +48 -0
  80. data/lib/zakuro/exception/cause.rb +28 -0
  81. data/lib/zakuro/exception/exception.rb +37 -0
  82. data/lib/zakuro/exception/zakuro_error.rb +46 -0
  83. data/lib/zakuro/gateway/locale/date.rb +127 -0
  84. data/lib/zakuro/gateway/locale/range.rb +67 -0
  85. data/lib/zakuro/gateway/range.rb +99 -0
  86. data/lib/zakuro/gateway/single.rb +70 -0
  87. data/lib/zakuro/merchant.rb +84 -12
  88. data/lib/zakuro/operation/month/parser.rb +11 -9
  89. data/lib/zakuro/operation/month/type.rb +31 -31
  90. data/lib/zakuro/operation/month/validator.rb +50 -50
  91. data/lib/zakuro/operation/yaml/month.yaml +3736 -3736
  92. data/lib/zakuro/output/logger.rb +17 -3
  93. data/lib/zakuro/output/response.rb +24 -57
  94. data/lib/zakuro/result/data/day.rb +43 -0
  95. data/lib/zakuro/result/data/gengou.rb +35 -0
  96. data/lib/zakuro/result/data/month.rb +63 -0
  97. data/lib/zakuro/result/data/option/abstract_option.rb +37 -0
  98. data/lib/zakuro/result/data/option/dropped_date/calculation.rb +47 -0
  99. data/lib/zakuro/result/data/option/dropped_date/option.rb +47 -0
  100. data/lib/zakuro/result/data/option/dropped_date/solar_term.rb +48 -0
  101. data/lib/zakuro/result/data/single_day.rb +56 -0
  102. data/lib/zakuro/result/data/solar_term.rb +35 -0
  103. data/lib/zakuro/result/data/year.rb +45 -0
  104. data/lib/zakuro/result/operation/bundle.rb +44 -0
  105. data/lib/zakuro/result/operation/month/annotation.rb +40 -0
  106. data/lib/zakuro/result/operation/month/bundle.rb +36 -0
  107. data/lib/zakuro/result/operation/month/history.rb +54 -0
  108. data/lib/zakuro/result/result.rb +21 -2
  109. data/lib/zakuro/tools/stringifier.rb +26 -3
  110. data/lib/zakuro/version/daien/cycle/remainder.rb +2 -2
  111. data/lib/zakuro/version/daien/option/dropped_date/parameter.rb +31 -0
  112. data/lib/zakuro/version/daien/range/annual_range.rb +1 -1
  113. data/lib/zakuro/version/daien/stella/lunar/adjustment.rb +2 -0
  114. data/lib/zakuro/version/daien/stella/lunar/value.rb +2 -0
  115. data/lib/zakuro/version/genka/cycle/remainder.rb +2 -2
  116. data/lib/zakuro/version/genka/option/dropped_date/parameter.rb +31 -0
  117. data/lib/zakuro/version/genka/range/annual_range.rb +1 -1
  118. data/lib/zakuro/version/gihou/cycle/remainder.rb +2 -2
  119. data/lib/zakuro/version/gihou/option/dropped_date/parameter.rb +31 -0
  120. data/lib/zakuro/version/gihou/range/annual_range.rb +1 -1
  121. data/lib/zakuro/version/gihou/stella/lunar/adjustment.rb +2 -0
  122. data/lib/zakuro/version/gihou/stella/lunar/value.rb +2 -0
  123. data/lib/zakuro/version/senmyou/README.md +11 -7
  124. data/lib/zakuro/version/senmyou/const/remainder.rb +5 -0
  125. data/lib/zakuro/version/senmyou/cycle/remainder.rb +37 -2
  126. data/lib/zakuro/version/senmyou/option/dropped_date/parameter.rb +33 -0
  127. data/lib/zakuro/version/senmyou/range/annual_range.rb +1 -1
  128. data/lib/zakuro/version/senmyou/stella/lunar/adjustment.rb +2 -0
  129. data/lib/zakuro/version/senmyou/stella/lunar/value.rb +2 -0
  130. metadata +85 -24
  131. data/lib/zakuro/calculation/gengou/internal/counter.rb +0 -129
  132. data/lib/zakuro/calculation/gengou/internal/reserve/interval.rb +0 -183
  133. data/lib/zakuro/calculation/gengou/internal/reserve/list.rb +0 -382
  134. data/lib/zakuro/calculation/gengou/scroll.rb +0 -262
  135. data/lib/zakuro/calculation/specifier/single_day.rb +0 -87
  136. data/lib/zakuro/era/japan/gengou/parser.rb +0 -237
  137. data/lib/zakuro/era/japan/gengou/type.rb +0 -285
  138. data/lib/zakuro/era/japan/gengou/validator.rb +0 -341
  139. data/lib/zakuro/result/data.rb +0 -187
  140. data/lib/zakuro/result/operation.rb +0 -114
  141. data/lib/zakuro/version/context.rb +0 -44
  142. data/lib/zakuro/version/version_class_resolver.rb +0 -74
@@ -0,0 +1,230 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative '../../../../../era/japan/gengou/resource'
4
+ require_relative '../../../../../era/japan/gengou'
5
+ require_relative '../../../../../era/japan/calendar'
6
+ require_relative '../../../../../era/western/calendar'
7
+
8
+ require_relative '../counter'
9
+
10
+ require_relative './empty_link'
11
+
12
+ # :nodoc:
13
+ module Zakuro
14
+ # :nodoc:
15
+ module Calculation
16
+ # :nodoc:
17
+ module Gengou
18
+ # :nodoc:
19
+ module Reserve
20
+ # AbstractList
21
+ #
22
+ # 抽象予約元号一覧
23
+ #
24
+ class AbstractList
25
+ # @return [Integer] 不正年
26
+ INVALID_YEAR = -1
27
+ # @return [Integer] 最大月日数
28
+ MAX_MONTH_DAYS = 30
29
+
30
+ # @return [Symbol] 行番号
31
+ attr_reader :index
32
+ # @return [Western::Calendar] 開始日
33
+ attr_reader :start_date
34
+ # @return [Western::Calendar] 終了日
35
+ attr_reader :last_date
36
+ # @return [Array<Japan::Alignment::LinearGengou>] 予約元号一覧
37
+ attr_reader :list
38
+
39
+ #
40
+ # 初期化
41
+ #
42
+ # @param [Integer] index n行目元号
43
+ # @param [Western::Calendar] start_date 開始日
44
+ # @param [Western::Calendar] last_date 終了日
45
+ #
46
+ def initialize(index:, start_date: Western::Calendar.new,
47
+ last_date: Western::Calendar)
48
+ @index = index
49
+ @start_date = start_date.clone
50
+ @last_date = last_date.clone
51
+ @list = []
52
+
53
+ update
54
+ end
55
+
56
+ #
57
+ # 元号を取得する
58
+ #
59
+ # @param [Western::Calendar] western_date 西暦日
60
+ #
61
+ # @return [Gengou::Counter] 加算元号
62
+ #
63
+ def get(western_date: Western::Calendar.new)
64
+ @list.each do |linear_gengou|
65
+ next if linear_gengou.out?(start_date: western_date, last_date: western_date)
66
+
67
+ Gengou::Counter.new(gengou: linear_gengou.gengou).clone
68
+ end
69
+
70
+ Gengou::Counter.new
71
+ end
72
+
73
+ #
74
+ # 範囲内元号を取得する
75
+ #
76
+ # @param [Western::Calendar] start_date 西暦開始日
77
+ # @param [Western::Calendar] last_date 西暦終了日
78
+ #
79
+ # @return [Array<Gengou::Counter>] 範囲内元号
80
+ #
81
+ def collect(start_date: Western::Calendar.new, last_date: Western::Calendar.new)
82
+ result = []
83
+
84
+ @list.each do |linear_gengou|
85
+ next if linear_gengou.out?(start_date: start_date, last_date: last_date)
86
+
87
+ result.push(
88
+ Gengou::Counter.new(
89
+ gengou: linear_gengou.gengou.clone,
90
+ start_date: linear_gengou.start_date.clone,
91
+ last_date: linear_gengou.last_date.clone
92
+ )
93
+ )
94
+ end
95
+
96
+ EmptyLink.fill(counters: result, start_date: start_date, last_date: last_date)
97
+
98
+ result
99
+ end
100
+
101
+ #
102
+ # 和暦開始日を取得する
103
+ #
104
+ # @return [Japan::Calendar] 和暦開始日
105
+ #
106
+ def japan_start_date
107
+ return Japan::Calendar.new if invalid?
108
+
109
+ @list[0].gengou.both_start_date.japan.clone
110
+ end
111
+
112
+ #
113
+ # 西暦開始日を取得する
114
+ #
115
+ # @return [Western::Calendar] 西暦開始日
116
+ #
117
+ def western_start_date
118
+ return Western::Calendar.new if invalid?
119
+
120
+ @list[0].gengou.both_start_date.western.clone
121
+ end
122
+
123
+ #
124
+ # 西暦終了日を取得する
125
+ #
126
+ # @return [Western::Calendar] 西暦開始日
127
+ #
128
+ def western_last_date
129
+ return Western::Calendar.new if invalid?
130
+
131
+ @list[-1].gengou.last_date.clone
132
+ end
133
+
134
+ #
135
+ # 西暦開始年を取得する
136
+ #
137
+ # @return [Integer] 西暦開始年
138
+ #
139
+ def western_start_year
140
+ return INVALID_YEAR if invalid?
141
+
142
+ @list[0].gengou.both_start_year.western.clone
143
+ end
144
+
145
+ #
146
+ # 西暦終了年を取得する
147
+ #
148
+ # @return [Integer] 西暦終了年
149
+ #
150
+ def western_last_year
151
+ return INVALID_YEAR if invalid?
152
+
153
+ return INVALID_YEAR if @list.size.zero?
154
+
155
+ @list[-1].gengou.last_year
156
+ end
157
+
158
+ #
159
+ # 不正か
160
+ #
161
+ # @return [True] 不正
162
+ # @return [False] 不正なし
163
+ #
164
+ def invalid?
165
+ return true unless @list
166
+
167
+ return true if @list.size.zero?
168
+
169
+ false
170
+ end
171
+
172
+ #
173
+ # 設定された元号の開始日を取得する
174
+ #
175
+ # @return [Western::Calendar]設定された元号の開始日
176
+ #
177
+ def native_start_date
178
+ return Western::Calendar.new if @list.size.zero?
179
+
180
+ @list[0].native_start_date
181
+ end
182
+
183
+ #
184
+ # 開始日が設定された開始日と異なるか(行が変更されているか)
185
+ #
186
+ # @return [True] 異なる
187
+ # @return [False] 同一
188
+ #
189
+ def change_start_date?
190
+ return false if @list.size.zero?
191
+
192
+ @list[0].change_start_date?
193
+ end
194
+
195
+ private
196
+
197
+ #
198
+ # 予約元号一覧を更新する
199
+ #
200
+ def update
201
+ # override
202
+ end
203
+
204
+ #
205
+ # 元号を取得する
206
+ #
207
+ # @param [Western::Calendar] start_date 開始日
208
+ # @param [Western::Calendar] last_date 終了日
209
+ #
210
+ # @return [Array<Japan::Alignment::LinearGengou>] 元号
211
+ #
212
+ def line(start_date:, last_date:)
213
+ Japan::Gengou.line(line: @index, start_date: start_date, last_date: last_date)
214
+ end
215
+
216
+ #
217
+ # 行番号に変換する
218
+ #
219
+ # @param [True, False] first 1行目/2行目
220
+ #
221
+ # @return [<Integer] 行番号
222
+ #
223
+ def parse_index(first: true)
224
+ first ? Japan::Gengou::FIRST_LINE : Japan::Gengou::SECOND_LINE
225
+ end
226
+ end
227
+ end
228
+ end
229
+ end
230
+ end
@@ -0,0 +1,193 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative '../../../../../era/western/calendar'
4
+
5
+ require_relative './dated_list'
6
+
7
+ # :nodoc:
8
+ module Zakuro
9
+ # :nodoc:
10
+ module Calculation
11
+ # :nodoc:
12
+ module Gengou
13
+ # :nodoc:
14
+ module Reserve
15
+ #
16
+ # AbstractRange 予約済み計算範囲
17
+ #
18
+ class AbstractRange
19
+ # @return [List] 1行目元号
20
+ attr_reader :first_list
21
+ # @return [List] 2行目元号
22
+ attr_reader :second_list
23
+
24
+ #
25
+ # 初期化
26
+ #
27
+ # @param [Western::Calendar] start_date 西暦開始日
28
+ # @param [Western::Calendar] last_date 西暦終了日
29
+ #
30
+ def initialize(start_date: Western::Calendar.new, last_date: Western::Calendar.new)
31
+ last_date = start_date.clone if last_date.invalid?
32
+
33
+ @first_list = DatedList.new(first: true, start_date: start_date, last_date: last_date)
34
+ @second_list = DatedList.new(first: false, start_date: start_date, last_date: last_date)
35
+
36
+ renew(last_date: last_date)
37
+ end
38
+
39
+ #
40
+ # 再初期化
41
+ # 含まれる最初の元号が別の行にまたがっている場合に開始日を前倒しする
42
+ #
43
+ # @param [Western::Calendar] last_date 西暦終了日
44
+ #
45
+ def renew(last_date: Western::Calendar.new)
46
+ native_start_date = Western::Calendar.new
47
+ [@first_list, @second_list].each do |list|
48
+ next unless list.change_start_date?
49
+
50
+ if native_start_date.invalid?
51
+ native_start_date = list.native_start_date.clone
52
+ next
53
+ end
54
+
55
+ next if native_start_date <= list.native_start_date
56
+
57
+ native_start_date = list.native_start_date.clone
58
+ end
59
+
60
+ return if native_start_date.invalid?
61
+
62
+ @first_list = DatedList.new(first: true, start_date: native_start_date,
63
+ last_date: last_date)
64
+ @second_list = DatedList.new(first: false, start_date: native_start_date,
65
+ last_date: last_date)
66
+ end
67
+
68
+ #
69
+ # 不正か
70
+ #
71
+ # @return [True] 不正
72
+ # @return [False] 不正なし
73
+ #
74
+ def invalid?
75
+ @first_list.invalid?
76
+ end
77
+
78
+ #
79
+ # 範囲内元号(1行目元号)を取得する
80
+ #
81
+ # @param [Western::Calendar] start_date 西暦開始日
82
+ # @param [Western::Calendar] last_date 西暦終了日
83
+ #
84
+ # @return [Array<Gengou::Counter>] 範囲内元号(1行目元号)
85
+ #
86
+ def collect_first(start_date: Western::Calendar.new,
87
+ last_date: Western::Calendar.new)
88
+ @first_list.collect(start_date: start_date, last_date: last_date)
89
+ end
90
+
91
+ #
92
+ # 範囲内元号(2行目元号)を取得する
93
+ #
94
+ # @param [Western::Calendar] start_date 西暦開始日
95
+ # @param [Western::Calendar] last_date 西暦終了日
96
+ #
97
+ # @return [Array<Gengou::Counter>] 範囲内元号(2行目元号)
98
+ #
99
+ def collect_second(start_date: Western::Calendar.new,
100
+ last_date: Western::Calendar.new)
101
+ @second_list.collect(start_date: start_date, last_date: last_date)
102
+ end
103
+
104
+ #
105
+ # 最古の元号から和暦開始日を取得する
106
+ #
107
+ # @return [Japan::Calendar] 和暦開始日
108
+ #
109
+ def japan_start_date
110
+ list = oldest_list
111
+
112
+ return Japan::Calendar.new if list.invalid?
113
+
114
+ list.japan_start_date
115
+ end
116
+
117
+ #
118
+ # 最古の元号から西暦開始日を取得する
119
+ #
120
+ # @return [Western::Calendar] 西暦開始日
121
+ #
122
+ def western_start_date
123
+ list = oldest_list
124
+
125
+ return Western::Calendar.new if list.invalid?
126
+
127
+ list.western_start_date
128
+ end
129
+
130
+ #
131
+ # 開始西暦年を取得する
132
+ #
133
+ # @return [Integer] 開始西暦年
134
+ #
135
+ def western_start_year
136
+ first_start_year = @first_list.western_start_year
137
+ second_start_year = @second_list.western_start_year
138
+
139
+ return first_start_year if @first_list.invalid?
140
+
141
+ return first_start_year if @second_list.invalid?
142
+
143
+ return first_start_year if first_start_year < second_start_year
144
+
145
+ second_start_year
146
+ end
147
+
148
+ #
149
+ # 終了西暦年を取得する
150
+ #
151
+ # @return [Integer] 終了西暦年
152
+ #
153
+ def western_last_year
154
+ first_last_year = @first_list.western_last_year
155
+ second_last_year = @second_list.western_last_year
156
+
157
+ return first_last_year if @first_list.invalid?
158
+
159
+ return first_last_year if @second_list.invalid?
160
+
161
+ return first_last_year if first_last_year > second_last_year
162
+
163
+ second_last_year
164
+ end
165
+
166
+ private
167
+
168
+ #
169
+ # 最古の元号を取得する
170
+ #
171
+ # @return [List] 最古の元号
172
+ #
173
+ def oldest_list
174
+ return @first_list if @first_list.invalid?
175
+
176
+ return @first_list if @second_list.invalid?
177
+
178
+ first_western_date = @first_list.western_start_date
179
+ second_western_date = @second_list.western_start_date
180
+
181
+ return @first_list if first_western_date.invalid?
182
+
183
+ return @first_list if second_western_date.invalid?
184
+
185
+ return @first_list if first_western_date < second_western_date
186
+
187
+ @second_list
188
+ end
189
+ end
190
+ end
191
+ end
192
+ end
193
+ end
@@ -0,0 +1,48 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative './abstract_list'
4
+
5
+ # :nodoc:
6
+ module Zakuro
7
+ # :nodoc:
8
+ module Calculation
9
+ # :nodoc:
10
+ module Gengou
11
+ # :nodoc:
12
+ module Reserve
13
+ # List
14
+ #
15
+ # 予約元号一覧
16
+ #
17
+ class DatedList < AbstractList
18
+ #
19
+ # 初期化
20
+ #
21
+ # @param [True, False] first true:1行目元号, false:2行目元号
22
+ # @param [Western::Calendar] start_date 開始日
23
+ # @param [Western::Calendar] last_date 終了日
24
+ #
25
+ def initialize(first: true, start_date: Western::Calendar.new,
26
+ last_date: Western::Calendar)
27
+ @index = parse_index(first: first)
28
+ @start_date = start_date.clone
29
+ @last_date = last_date.invalid? ? start_date.clone : last_date.clone
30
+ super(index: @index, start_date: start_date, last_date: last_date)
31
+ end
32
+
33
+ #
34
+ # 予約元号一覧を更新する
35
+ #
36
+ def update
37
+ # 開始日の30日前に前の元号がある場合は、前の元号を設定する
38
+ start_date = @start_date.clone - (MAX_MONTH_DAYS + 1)
39
+ # 開始日の30日後に次の元号がある場合は、次の元号を設定する
40
+ last_date = @last_date.clone + (MAX_MONTH_DAYS + 1)
41
+
42
+ @list |= line(start_date: start_date, last_date: last_date)
43
+ end
44
+ end
45
+ end
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,30 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative './abstract_range'
4
+
5
+ # :nodoc:
6
+ module Zakuro
7
+ # :nodoc:
8
+ module Calculation
9
+ # :nodoc:
10
+ module Gengou
11
+ # :nodoc:
12
+ module Reserve
13
+ #
14
+ # DatedRange 予約済み計算範囲
15
+ #
16
+ class DatedRange < AbstractRange
17
+ #
18
+ # 初期化
19
+ #
20
+ # @param [Western::Calendar] start_date 西暦開始日
21
+ # @param [Western::Calendar] last_date 西暦終了日
22
+ #
23
+ def initialize(start_date: Western::Calendar.new, last_date: Western::Calendar.new)
24
+ super(start_date: start_date, last_date: last_date)
25
+ end
26
+ end
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,158 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative '../../../../../era/japan/gengou/resource'
4
+ require_relative '../../../../../era/japan/gengou'
5
+ require_relative '../../../../../era/japan/calendar'
6
+ require_relative '../../../../../era/western/calendar'
7
+
8
+ require_relative '../counter'
9
+
10
+ # :nodoc:
11
+ module Zakuro
12
+ # :nodoc:
13
+ module Calculation
14
+ # :nodoc:
15
+ module Gengou
16
+ # :nodoc:
17
+ module Reserve
18
+ # EmptyLink
19
+ #
20
+ # 空元号連結
21
+ #
22
+ # * 開始日と終了日の間は常に元号で満たす必要がある
23
+ # * もし元号が存在しない期間があれば、空の元号を生成する
24
+ #
25
+ module EmptyLink
26
+ #
27
+ # 空元号で満たす
28
+ #
29
+ # @param [Array<Gengou::Counter>] counters 加算元号リスト
30
+ # @param [Western::Calendar] start_date 開始日
31
+ # @param [Western::Calendar] last_date 終了日
32
+ #
33
+ def self.fill(counters:, start_date: Western::Calendar.new,
34
+ last_date: Western::Calendar.new)
35
+ if counters.size.zero?
36
+ counters.push(
37
+ create_empty_counter(
38
+ start_date: start_date.clone,
39
+ last_date: last_date.clone
40
+ )
41
+ )
42
+ return counters
43
+ end
44
+
45
+ fill_both_ends(counters: counters, start_date: start_date, last_date: last_date)
46
+
47
+ fill_middle(counters: counters)
48
+ end
49
+
50
+ #
51
+ # リストの両端を空元号で満たす
52
+ #
53
+ # @param [Array<Gengou::Counter>] counters 加算元号リスト
54
+ # @param [Western::Calendar] start_date 開始日
55
+ # @param [Western::Calendar] last_date 終了日
56
+ #
57
+ def self.fill_both_ends(counters:, start_date: Western::Calendar.new,
58
+ last_date: Western::Calendar.new)
59
+ fill_by_start(counters: counters, start_date: start_date)
60
+
61
+ fill_by_last(counters: counters, last_date: last_date)
62
+ end
63
+ private_class_method :fill_both_ends
64
+
65
+ #
66
+ # 先頭要素の手前を空元号で満たす
67
+ #
68
+ # @param [Array<Gengou::Counter>] counters 加算元号リスト
69
+ # @param [Western::Calendar] start_date 開始日
70
+ #
71
+ def self.fill_by_start(counters:, start_date:)
72
+ return unless start_date < counters[0].start_date
73
+
74
+ counters.unshift(
75
+ create_empty_counter(
76
+ start_date: start_date.clone,
77
+ last_date: counters[0].start_date.clone - 1
78
+ )
79
+ )
80
+ end
81
+ private_class_method :fill_by_start
82
+
83
+ #
84
+ # 最終要素の手前を空元号で満たす
85
+ #
86
+ # @param [Array<Gengou::Counter>] counters 加算元号リスト
87
+ # @param [Western::Calendar] last_date 終了日
88
+ #
89
+ def self.fill_by_last(counters:, last_date:)
90
+ return unless last_date > counters[-1].last_date
91
+
92
+ counters.push(
93
+ create_empty_counter(
94
+ start_date: counters[-1].last_date.clone + 1,
95
+ last_date: last_date.clone
96
+ )
97
+ )
98
+ end
99
+ private_class_method :fill_by_last
100
+
101
+ #
102
+ # 元号間を空元号で満たす
103
+ #
104
+ # @param [Array<Gengou::Counter>] counters 加算元号リスト
105
+ #
106
+ def self.fill_middle(counters:)
107
+ return if counters.size.zero?
108
+
109
+ size = counters.size - 1
110
+ (0..size).reverse_each do |index|
111
+ break if index <= 0
112
+
113
+ insert(counters: counters, index: index)
114
+ end
115
+ end
116
+ private_class_method :fill_middle
117
+
118
+ #
119
+ # 対象要素に空元号を追加する
120
+ #
121
+ # @param [Array<Gengou::Counter>] counters 加算元号リスト
122
+ # @param [Integer] index 要素番号
123
+ #
124
+ def self.insert(counters:, index:)
125
+ before_last_date = counters[index - 1].last_date.clone
126
+ current_start_date = counters[index].start_date.clone
127
+
128
+ return if (before_last_date.clone + 1) == current_start_date
129
+
130
+ counters.insert(
131
+ index,
132
+ create_empty_counter(
133
+ start_date: before_last_date + 1, last_date: current_start_date - 1
134
+ )
135
+ )
136
+ end
137
+ private_class_method :insert
138
+
139
+ #
140
+ # 空元号を生成する
141
+ #
142
+ # @param [Western::Calendar] start_date 西暦開始日
143
+ # @param [Western::Calendar] last_date 西暦終了日
144
+ #
145
+ # @return [Gengou::Counter] 加算元号(空元号)
146
+ #
147
+ def self.create_empty_counter(start_date:, last_date:)
148
+ Gengou::Counter.new(
149
+ gengou: Japan::Gengou::Resource::Gengou.new,
150
+ start_date: start_date, last_date: last_date
151
+ )
152
+ end
153
+ private_class_method :create_empty_counter
154
+ end
155
+ end
156
+ end
157
+ end
158
+ end