zakuro 0.6.1 → 0.8.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (181) hide show
  1. checksums.yaml +4 -4
  2. data/Makefile +3 -0
  3. data/README.md +46 -17
  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 +49 -3
  10. data/lib/zakuro/calculation/base/linear_gengou.rb +5 -5
  11. data/lib/zakuro/calculation/base/operated_year.rb +26 -24
  12. data/lib/zakuro/calculation/base/year.rb +7 -7
  13. data/lib/zakuro/calculation/cycle/abstract_remainder.rb +55 -47
  14. data/lib/zakuro/calculation/cycle/abstract_solar_term.rb +28 -24
  15. data/lib/zakuro/calculation/cycle/zodiac.rb +22 -20
  16. data/lib/zakuro/calculation/era/gengou/{scroll.rb → abstract_scroll.rb} +44 -37
  17. data/lib/zakuro/calculation/era/gengou/dated_scroll.rb +30 -0
  18. data/lib/zakuro/calculation/era/gengou/internal/connector.rb +4 -4
  19. data/lib/zakuro/calculation/era/gengou/internal/counter.rb +16 -16
  20. data/lib/zakuro/calculation/era/gengou/internal/publisher.rb +74 -72
  21. data/lib/zakuro/calculation/era/gengou/internal/reserve/{list.rb → abstract_list.rb} +50 -64
  22. data/lib/zakuro/calculation/era/gengou/internal/reserve/{range.rb → abstract_range.rb} +73 -50
  23. data/lib/zakuro/calculation/era/gengou/internal/reserve/dated_list.rb +48 -0
  24. data/lib/zakuro/calculation/era/gengou/internal/reserve/dated_range.rb +30 -0
  25. data/lib/zakuro/calculation/era/gengou/internal/reserve/empty_link.rb +156 -0
  26. data/lib/zakuro/calculation/era/gengou/internal/reserve/named_list.rb +125 -0
  27. data/lib/zakuro/calculation/era/gengou/internal/reserve/named_range.rb +73 -0
  28. data/lib/zakuro/calculation/era/gengou/internal/reserve.rb +23 -22
  29. data/lib/zakuro/calculation/era/gengou/named_scroll.rb +30 -0
  30. data/lib/zakuro/calculation/era/version/internal/crawler.rb +39 -22
  31. data/lib/zakuro/calculation/era/version/version.rb +12 -10
  32. data/lib/zakuro/calculation/monthly/abstract_lunar_phase.rb +13 -9
  33. data/lib/zakuro/calculation/monthly/const.rb +18 -0
  34. data/lib/zakuro/calculation/monthly/initialized_month.rb +40 -38
  35. data/lib/zakuro/calculation/monthly/month.rb +55 -16
  36. data/lib/zakuro/calculation/monthly/month_label.rb +4 -4
  37. data/lib/zakuro/calculation/monthly/operated_month.rb +37 -86
  38. data/lib/zakuro/calculation/monthly/operated_solar_term.rb +80 -0
  39. data/lib/zakuro/calculation/option/dropped_date/abstract_parameter.rb +44 -0
  40. data/lib/zakuro/calculation/option/dropped_date/location.rb +135 -0
  41. data/lib/zakuro/calculation/range/{full_range.rb → abstract_full_range.rb} +45 -29
  42. data/lib/zakuro/calculation/range/abstract_operation_range.rb +193 -0
  43. data/lib/zakuro/calculation/range/dated_full_range.rb +32 -0
  44. data/lib/zakuro/calculation/range/dated_operation_range.rb +33 -0
  45. data/lib/zakuro/calculation/range/medieval_annual_range.rb +64 -63
  46. data/lib/zakuro/calculation/range/named_full_range.rb +38 -0
  47. data/lib/zakuro/calculation/range/named_operation_range.rb +36 -0
  48. data/lib/zakuro/calculation/range/operated_solar_term.rb +220 -0
  49. data/lib/zakuro/calculation/range/transfer/gengou_scroller.rb +32 -29
  50. data/lib/zakuro/calculation/range/transfer/year_boundary.rb +115 -117
  51. data/lib/zakuro/calculation/stella/lunar/abstract_location.rb +1 -1
  52. data/lib/zakuro/calculation/stella/lunar/choukei_value.rb +48 -46
  53. data/lib/zakuro/calculation/stella/solar/abstract_average.rb +31 -26
  54. data/lib/zakuro/calculation/stella/solar/abstract_location.rb +7 -5
  55. data/lib/zakuro/calculation/stella/solar/choukei_value.rb +114 -114
  56. data/lib/zakuro/calculation/summary/internal/day.rb +42 -0
  57. data/lib/zakuro/calculation/{specifier → summary}/internal/month.rb +33 -12
  58. data/lib/zakuro/calculation/summary/internal/operation.rb +68 -67
  59. data/lib/zakuro/calculation/summary/internal/option.rb +101 -0
  60. data/lib/zakuro/calculation/summary/japan/range.rb +179 -0
  61. data/lib/zakuro/calculation/summary/japan/single.rb +118 -0
  62. data/lib/zakuro/calculation/summary/japan/specifier/single_day.rb +98 -0
  63. data/lib/zakuro/calculation/summary/japan/specifier/specified_range.rb +34 -0
  64. data/lib/zakuro/calculation/summary/western/range.rb +127 -0
  65. data/lib/zakuro/calculation/summary/western/single.rb +115 -0
  66. data/lib/zakuro/calculation/summary/western/specifier/multiple_day.rb +173 -0
  67. data/lib/zakuro/calculation/summary/western/specifier/single_day.rb +100 -0
  68. data/lib/zakuro/calculation/type/old_float.rb +2 -2
  69. data/lib/zakuro/calculation/type/optional.rb +46 -0
  70. data/lib/zakuro/condition.rb +143 -94
  71. data/lib/zakuro/context/context.rb +44 -0
  72. data/lib/zakuro/context/option.rb +119 -0
  73. data/lib/zakuro/context/version_class_resolver.rb +112 -0
  74. data/lib/zakuro/era/japan/calendar.rb +35 -33
  75. data/lib/zakuro/era/japan/gengou/alignment/aligner.rb +93 -74
  76. data/lib/zakuro/era/japan/gengou/alignment/division.rb +135 -133
  77. data/lib/zakuro/era/japan/gengou/alignment/line.rb +143 -122
  78. data/lib/zakuro/era/japan/gengou/alignment/linear_gengou.rb +177 -168
  79. data/lib/zakuro/era/japan/gengou/alignment.rb +40 -25
  80. data/lib/zakuro/era/japan/gengou/resource/parser.rb +199 -181
  81. data/lib/zakuro/era/japan/gengou/resource/type.rb +243 -234
  82. data/lib/zakuro/era/japan/gengou/resource/validator.rb +257 -254
  83. data/lib/zakuro/era/japan/gengou/resource.rb +23 -22
  84. data/lib/zakuro/era/japan/gengou.rb +28 -41
  85. data/lib/zakuro/era/japan/version.rb +18 -16
  86. data/lib/zakuro/era/western/calendar.rb +104 -88
  87. data/lib/zakuro/exception/case/pattern.rb +71 -0
  88. data/lib/zakuro/exception/case/preset.rb +51 -0
  89. data/lib/zakuro/exception/case/template.rb +48 -0
  90. data/lib/zakuro/exception/cause.rb +28 -0
  91. data/lib/zakuro/exception/exception.rb +39 -0
  92. data/lib/zakuro/exception/zakuro_error.rb +46 -0
  93. data/lib/zakuro/gateway/locale/date.rb +127 -0
  94. data/lib/zakuro/gateway/locale/range.rb +67 -0
  95. data/lib/zakuro/gateway/range.rb +99 -0
  96. data/lib/zakuro/gateway/single.rb +81 -0
  97. data/lib/zakuro/merchant.rb +67 -33
  98. data/lib/zakuro/operation/month/parser.rb +252 -249
  99. data/lib/zakuro/operation/month/type.rb +44 -35
  100. data/lib/zakuro/operation/month/validator.rb +182 -178
  101. data/lib/zakuro/operation/operation.rb +38 -36
  102. data/lib/zakuro/operation/yaml/month.yaml +3736 -3736
  103. data/lib/zakuro/output/logger.rb +17 -3
  104. data/lib/zakuro/output/response.rb +107 -141
  105. data/lib/zakuro/result/data/day.rb +43 -0
  106. data/lib/zakuro/result/data/gengou.rb +35 -0
  107. data/lib/zakuro/result/data/month.rb +63 -0
  108. data/lib/zakuro/result/data/option/abstract_option.rb +37 -0
  109. data/lib/zakuro/result/data/option/dropped_date/calculation.rb +47 -0
  110. data/lib/zakuro/result/data/option/dropped_date/option.rb +47 -0
  111. data/lib/zakuro/result/data/option/dropped_date/solar_term.rb +48 -0
  112. data/lib/zakuro/result/data/single_day.rb +56 -0
  113. data/lib/zakuro/result/data/solar_term.rb +35 -0
  114. data/lib/zakuro/result/data/year.rb +45 -0
  115. data/lib/zakuro/result/operation/month/annotation.rb +40 -0
  116. data/lib/zakuro/result/operation/month/history.rb +54 -0
  117. data/lib/zakuro/result/operation/month.rb +31 -0
  118. data/lib/zakuro/result/operation.rb +22 -97
  119. data/lib/zakuro/result/result.rb +6 -4
  120. data/lib/zakuro/tools/stringifier.rb +67 -42
  121. data/lib/zakuro/tools/typeconv.rb +17 -15
  122. data/lib/zakuro/tools/typeof.rb +15 -13
  123. data/lib/zakuro/version/daien/cycle/remainder.rb +2 -2
  124. data/lib/zakuro/version/daien/monthly/lunar_phase.rb +10 -12
  125. data/lib/zakuro/version/daien/option/dropped_date/parameter.rb +31 -0
  126. data/lib/zakuro/version/daien/range/annual_range.rb +19 -17
  127. data/lib/zakuro/version/daien/stella/lunar/adjustment.rb +42 -37
  128. data/lib/zakuro/version/daien/stella/lunar/localization.rb +16 -14
  129. data/lib/zakuro/version/daien/stella/lunar/value.rb +52 -47
  130. data/lib/zakuro/version/daien/stella/origin/average_november.rb +15 -13
  131. data/lib/zakuro/version/daien/stella/origin/lunar_age.rb +23 -21
  132. data/lib/zakuro/version/daien/stella/origin/winter_solstice.rb +22 -20
  133. data/lib/zakuro/version/daien/stella/solar/adjustment.rb +12 -10
  134. data/lib/zakuro/version/daien/stella/solar/average.rb +53 -51
  135. data/lib/zakuro/version/daien/stella/solar/interval.rb +19 -17
  136. data/lib/zakuro/version/daien/stella/solar/value.rb +13 -11
  137. data/lib/zakuro/version/genka/cycle/remainder.rb +4 -4
  138. data/lib/zakuro/version/genka/monthly/lunar_phase.rb +7 -5
  139. data/lib/zakuro/version/genka/option/dropped_date/parameter.rb +31 -0
  140. data/lib/zakuro/version/genka/range/annual_range.rb +48 -44
  141. data/lib/zakuro/version/genka/stella/origin/first_term.rb +30 -28
  142. data/lib/zakuro/version/genka/stella/origin/january.rb +22 -20
  143. data/lib/zakuro/version/gihou/cycle/remainder.rb +2 -2
  144. data/lib/zakuro/version/gihou/monthly/lunar_phase.rb +9 -9
  145. data/lib/zakuro/version/gihou/option/dropped_date/parameter.rb +31 -0
  146. data/lib/zakuro/version/gihou/range/annual_range.rb +19 -17
  147. data/lib/zakuro/version/gihou/stella/lunar/adjustment.rb +42 -37
  148. data/lib/zakuro/version/gihou/stella/lunar/localization.rb +16 -14
  149. data/lib/zakuro/version/gihou/stella/lunar/value.rb +52 -47
  150. data/lib/zakuro/version/gihou/stella/origin/average_november.rb +15 -13
  151. data/lib/zakuro/version/gihou/stella/origin/lunar_age.rb +23 -21
  152. data/lib/zakuro/version/gihou/stella/origin/winter_solstice.rb +24 -22
  153. data/lib/zakuro/version/gihou/stella/solar/adjustment.rb +12 -10
  154. data/lib/zakuro/version/gihou/stella/solar/average.rb +52 -50
  155. data/lib/zakuro/version/gihou/stella/solar/interval.rb +19 -17
  156. data/lib/zakuro/version/gihou/stella/solar/value.rb +13 -11
  157. data/lib/zakuro/version/senmyou/const/remainder.rb +5 -0
  158. data/lib/zakuro/version/senmyou/cycle/remainder.rb +37 -2
  159. data/lib/zakuro/version/senmyou/monthly/lunar_phase.rb +10 -10
  160. data/lib/zakuro/version/senmyou/option/dropped_date/parameter.rb +33 -0
  161. data/lib/zakuro/version/senmyou/range/annual_range.rb +19 -17
  162. data/lib/zakuro/version/senmyou/stella/lunar/adjustment.rb +42 -36
  163. data/lib/zakuro/version/senmyou/stella/lunar/localization.rb +16 -14
  164. data/lib/zakuro/version/senmyou/stella/lunar/value.rb +39 -34
  165. data/lib/zakuro/version/senmyou/stella/origin/average_november.rb +15 -13
  166. data/lib/zakuro/version/senmyou/stella/origin/lunar_age.rb +23 -21
  167. data/lib/zakuro/version/senmyou/stella/origin/winter_solstice.rb +22 -20
  168. data/lib/zakuro/version/senmyou/stella/solar/adjustment.rb +12 -10
  169. data/lib/zakuro/version/senmyou/stella/solar/average.rb +52 -50
  170. data/lib/zakuro/version/senmyou/stella/solar/interval.rb +19 -17
  171. data/lib/zakuro/version/senmyou/stella/solar/value.rb +13 -11
  172. metadata +68 -16
  173. data/lib/zakuro/calculation/range/operated_range.rb +0 -193
  174. data/lib/zakuro/calculation/range/operated_solar_terms.rb +0 -220
  175. data/lib/zakuro/calculation/specifier/multiple_day.rb +0 -162
  176. data/lib/zakuro/calculation/specifier/single_day.rb +0 -87
  177. data/lib/zakuro/calculation/summary/range.rb +0 -120
  178. data/lib/zakuro/calculation/summary/single.rb +0 -110
  179. data/lib/zakuro/result/data.rb +0 -187
  180. data/lib/zakuro/version/context.rb +0 -44
  181. data/lib/zakuro/version/version_class_resolver.rb +0 -74
@@ -2,7 +2,7 @@
2
2
 
3
3
  require_relative '../../../../../era/western/calendar'
4
4
 
5
- require_relative './list'
5
+ require_relative './dated_list'
6
6
 
7
7
  # :nodoc:
8
8
  module Zakuro
@@ -13,9 +13,9 @@ module Zakuro
13
13
  # :nodoc:
14
14
  module Reserve
15
15
  #
16
- # Range 予約済み計算範囲
16
+ # AbstractRange 予約済み計算範囲
17
17
  #
18
- class Range
18
+ class AbstractRange
19
19
  # @return [List] 1行目元号
20
20
  attr_reader :first_list
21
21
  # @return [List] 2行目元号
@@ -30,8 +30,8 @@ module Zakuro
30
30
  def initialize(start_date: Western::Calendar.new, last_date: Western::Calendar.new)
31
31
  last_date = start_date.clone if last_date.invalid?
32
32
 
33
- @first_list = List.new(first: true, start_date: start_date, last_date: last_date)
34
- @second_list = List.new(first: false, start_date: start_date, last_date: last_date)
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
35
 
36
36
  renew(last_date: last_date)
37
37
  end
@@ -43,26 +43,14 @@ module Zakuro
43
43
  # @param [Western::Calendar] last_date 西暦終了日
44
44
  #
45
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
46
+ start_date = native_start_date
59
47
 
60
- return if native_start_date.invalid?
48
+ return if start_date.invalid?
61
49
 
62
- @first_list = List.new(first: true,
63
- start_date: native_start_date, last_date: last_date)
64
- @second_list = List.new(first: false,
65
- start_date: native_start_date, last_date: last_date)
50
+ @first_list = DatedList.new(first: true, start_date: start_date,
51
+ last_date: last_date)
52
+ @second_list = DatedList.new(first: false, start_date: start_date,
53
+ last_date: last_date)
66
54
  end
67
55
 
68
56
  #
@@ -72,7 +60,7 @@ module Zakuro
72
60
  # @return [False] 不正なし
73
61
  #
74
62
  def invalid?
75
- @first_list.invalid?
63
+ first_list.invalid?
76
64
  end
77
65
 
78
66
  #
@@ -85,7 +73,7 @@ module Zakuro
85
73
  #
86
74
  def collect_first(start_date: Western::Calendar.new,
87
75
  last_date: Western::Calendar.new)
88
- @first_list.collect(start_date: start_date, last_date: last_date)
76
+ first_list.collect(start_date: start_date, last_date: last_date)
89
77
  end
90
78
 
91
79
  #
@@ -98,7 +86,7 @@ module Zakuro
98
86
  #
99
87
  def collect_second(start_date: Western::Calendar.new,
100
88
  last_date: Western::Calendar.new)
101
- @second_list.collect(start_date: start_date, last_date: last_date)
89
+ second_list.collect(start_date: start_date, last_date: last_date)
102
90
  end
103
91
 
104
92
  #
@@ -107,11 +95,11 @@ module Zakuro
107
95
  # @return [Japan::Calendar] 和暦開始日
108
96
  #
109
97
  def japan_start_date
110
- gengou = oldest_gengou
98
+ list = oldest_list
111
99
 
112
- return Japan::Calendar.new if gengou.invalid?
100
+ return Japan::Calendar.new if list.invalid?
113
101
 
114
- gengou.japan_start_date
102
+ list.japan_start_date
115
103
  end
116
104
 
117
105
  #
@@ -120,11 +108,11 @@ module Zakuro
120
108
  # @return [Western::Calendar] 西暦開始日
121
109
  #
122
110
  def western_start_date
123
- gengou = oldest_gengou
111
+ list = oldest_list
124
112
 
125
- return Western::Calendar.new if gengou.invalid?
113
+ return Western::Calendar.new if list.invalid?
126
114
 
127
- gengou.western_start_date
115
+ list.western_start_date
128
116
  end
129
117
 
130
118
  #
@@ -133,12 +121,12 @@ module Zakuro
133
121
  # @return [Integer] 開始西暦年
134
122
  #
135
123
  def western_start_year
136
- first_start_year = @first_list.western_start_year
137
- second_start_year = @second_list.western_start_year
124
+ first_start_year = first_list.western_start_year
125
+ second_start_year = second_list.western_start_year
138
126
 
139
- return first_start_year if @first_list.invalid?
127
+ return first_start_year if first_list.invalid?
140
128
 
141
- return first_start_year if @second_list.invalid?
129
+ return first_start_year if second_list.invalid?
142
130
 
143
131
  return first_start_year if first_start_year < second_start_year
144
132
 
@@ -151,12 +139,12 @@ module Zakuro
151
139
  # @return [Integer] 終了西暦年
152
140
  #
153
141
  def western_last_year
154
- first_last_year = @first_list.western_last_year
155
- second_last_year = @second_list.western_last_year
142
+ first_last_year = first_list.western_last_year
143
+ second_last_year = second_list.western_last_year
156
144
 
157
- return first_last_year if @first_list.invalid?
145
+ return first_last_year if first_list.invalid?
158
146
 
159
- return first_last_year if @second_list.invalid?
147
+ return first_last_year if second_list.invalid?
160
148
 
161
149
  return first_last_year if first_last_year > second_last_year
162
150
 
@@ -165,26 +153,61 @@ module Zakuro
165
153
 
166
154
  private
167
155
 
156
+ #
157
+ # 設定された元号の開始日を取得する
158
+ #
159
+ # @return [Western::Calendar] 設定された元号の開始日
160
+ #
161
+ def native_start_date
162
+ result = Western::Calendar.new
163
+ [first_list, second_list].each do |list|
164
+ next unless list.change_start_date?
165
+
166
+ if result.invalid?
167
+ result = list.native_start_date.clone
168
+ next
169
+ end
170
+
171
+ next if result <= list.native_start_date
172
+
173
+ result = list.native_start_date.clone
174
+ end
175
+
176
+ result
177
+ end
178
+
168
179
  #
169
180
  # 最古の元号を取得する
170
181
  #
171
- # @return [Japan::Gengou] 最古の元号
182
+ # @return [List] 最古の元号
172
183
  #
173
- def oldest_gengou
174
- return @first_list if @first_list.invalid?
184
+ def oldest_list
185
+ return first_list if invalid_list?
186
+
187
+ first_western_date = first_list.western_start_date
188
+ second_western_date = second_list.western_start_date
175
189
 
176
- return @first_list if @second_list.invalid?
190
+ return first_list if first_western_date.invalid?
177
191
 
178
- first_western_date = @first_list.western_start_date
179
- second_western_date = @second_list.western_start_date
192
+ return first_list if second_western_date.invalid?
180
193
 
181
- return @first_list if first_western_date.invalid?
194
+ return first_list if first_western_date < second_western_date
182
195
 
183
- return @first_list if second_western_date.invalid?
196
+ second_list
197
+ end
198
+
199
+ #
200
+ # 元号リストが不正か
201
+ #
202
+ # @return [True] 不正
203
+ # @return [False] 不正なし
204
+ #
205
+ def invalid_list?
206
+ return true if first_list.invalid?
184
207
 
185
- return @first_list if first_western_date < second_western_date
208
+ return true if second_list.invalid?
186
209
 
187
- @second_list
210
+ false
188
211
  end
189
212
  end
190
213
  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 = start_date.clone - (MAX_MONTH_DAYS + 1)
39
+ # 開始日の30日後に次の元号がある場合は、次の元号を設定する
40
+ last = last_date.clone + (MAX_MONTH_DAYS + 1)
41
+
42
+ @list |= line(start_date: start, last_date: last)
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,156 @@
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
+ class << self
27
+ #
28
+ # 空元号で満たす
29
+ #
30
+ # @param [Array<Gengou::Counter>] counters 加算元号リスト
31
+ # @param [Western::Calendar] start_date 開始日
32
+ # @param [Western::Calendar] last_date 終了日
33
+ #
34
+ def fill(counters:, start_date: Western::Calendar.new,
35
+ last_date: Western::Calendar.new)
36
+ if counters.size.zero?
37
+ counters.push(
38
+ create_empty_counter(
39
+ start_date: start_date.clone,
40
+ last_date: last_date.clone
41
+ )
42
+ )
43
+ return counters
44
+ end
45
+
46
+ fill_both_ends(counters: counters, start_date: start_date, last_date: last_date)
47
+
48
+ fill_middle(counters: counters)
49
+ end
50
+
51
+ private
52
+
53
+ #
54
+ # リストの両端を空元号で満たす
55
+ #
56
+ # @param [Array<Gengou::Counter>] counters 加算元号リスト
57
+ # @param [Western::Calendar] start_date 開始日
58
+ # @param [Western::Calendar] last_date 終了日
59
+ #
60
+ def fill_both_ends(counters:, start_date: Western::Calendar.new,
61
+ last_date: Western::Calendar.new)
62
+ fill_by_start(counters: counters, start_date: start_date)
63
+
64
+ fill_by_last(counters: counters, last_date: last_date)
65
+ end
66
+
67
+ #
68
+ # 先頭要素の手前を空元号で満たす
69
+ #
70
+ # @param [Array<Gengou::Counter>] counters 加算元号リスト
71
+ # @param [Western::Calendar] start_date 開始日
72
+ #
73
+ def fill_by_start(counters:, start_date:)
74
+ return unless start_date < counters[0].start_date
75
+
76
+ counters.unshift(
77
+ create_empty_counter(
78
+ start_date: start_date.clone,
79
+ last_date: counters[0].start_date.clone - 1
80
+ )
81
+ )
82
+ end
83
+
84
+ #
85
+ # 最終要素の手前を空元号で満たす
86
+ #
87
+ # @param [Array<Gengou::Counter>] counters 加算元号リスト
88
+ # @param [Western::Calendar] last_date 終了日
89
+ #
90
+ def fill_by_last(counters:, last_date:)
91
+ return unless last_date > counters[-1].last_date
92
+
93
+ counters.push(
94
+ create_empty_counter(
95
+ start_date: counters[-1].last_date.clone + 1,
96
+ last_date: last_date.clone
97
+ )
98
+ )
99
+ end
100
+
101
+ #
102
+ # 元号間を空元号で満たす
103
+ #
104
+ # @param [Array<Gengou::Counter>] counters 加算元号リスト
105
+ #
106
+ def 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
+
117
+ #
118
+ # 対象要素に空元号を追加する
119
+ #
120
+ # @param [Array<Gengou::Counter>] counters 加算元号リスト
121
+ # @param [Integer] index 要素番号
122
+ #
123
+ def insert(counters:, index:)
124
+ before_last_date = counters[index - 1].last_date.clone
125
+ current_start_date = counters[index].start_date.clone
126
+
127
+ return if (before_last_date.clone + 1) == current_start_date
128
+
129
+ counters.insert(
130
+ index,
131
+ create_empty_counter(
132
+ start_date: before_last_date + 1, last_date: current_start_date - 1
133
+ )
134
+ )
135
+ end
136
+
137
+ #
138
+ # 空元号を生成する
139
+ #
140
+ # @param [Western::Calendar] start_date 西暦開始日
141
+ # @param [Western::Calendar] last_date 西暦終了日
142
+ #
143
+ # @return [Gengou::Counter] 加算元号(空元号)
144
+ #
145
+ def create_empty_counter(start_date:, last_date:)
146
+ Gengou::Counter.new(
147
+ gengou: Japan::Gengou::Resource::Gengou.new,
148
+ start_date: start_date, last_date: last_date
149
+ )
150
+ end
151
+ end
152
+ end
153
+ end
154
+ end
155
+ end
156
+ end
@@ -0,0 +1,125 @@
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
+ # NamedList
14
+ #
15
+ # 予約元号一覧(元号名)
16
+ #
17
+ class NamedList < AbstractList
18
+ # @return [String] 不正元号名
19
+ INVALID_NAME = Japan::Calendar::EMPTY
20
+
21
+ #
22
+ # 初期化
23
+ #
24
+ # @param [True, False] first true:1行目元号, false:2行目元号
25
+ # @param [String] start_name 開始元号名
26
+ # @param [String] last_name 終了元号名
27
+ #
28
+ def initialize(first: true, start_name: INVALID_NAME, last_name: INVALID_NAME)
29
+ @index = parse_index(first: first)
30
+ @start_date = Western::Calendar.new
31
+ @last_date = Western::Calendar.new
32
+
33
+ locate(start_name: start_name, last_name: last_name)
34
+ super(index: index, start_date: start_date, last_date: last_date)
35
+ end
36
+
37
+ private
38
+
39
+ #
40
+ # 開始日・終了日を配置する
41
+ #
42
+ # @param [String] start_name 開始元号名
43
+ # @param [String] last_name 終了元号名
44
+ #
45
+ def locate(start_name:, last_name:)
46
+ start = start_name
47
+ last = last_name
48
+ last = start if last == INVALID_NAME
49
+
50
+ return if start == INVALID_NAME
51
+
52
+ [start, last].each do |name|
53
+ gengou_list = line_by_name(name: name)
54
+
55
+ next if gengou_list.size.zero?
56
+
57
+ choise_start_date(
58
+ date: gengou_list[0].start_date.clone
59
+ )
60
+ choise_last_date(
61
+ date: gengou_list[-1].last_date.clone
62
+ )
63
+ end
64
+ end
65
+
66
+ #
67
+ # 最古の開始日を選択する
68
+ #
69
+ # @param [Western::Calendar] date 西暦日
70
+ #
71
+ def choise_start_date(date:)
72
+ return if date.invalid?
73
+
74
+ if start_date.invalid?
75
+ @start_date = date
76
+ return
77
+ end
78
+
79
+ return if date > start_date
80
+
81
+ @start_date = date
82
+ end
83
+
84
+ #
85
+ # 最新の終了日を選択する
86
+ #
87
+ # @param [Western::Calendar] date 西暦日
88
+ #
89
+ def choise_last_date(date:)
90
+ return if date.invalid?
91
+
92
+ if last_date.invalid?
93
+ @last_date = date
94
+ return
95
+ end
96
+
97
+ return if date < last_date
98
+
99
+ @last_date = date
100
+ end
101
+
102
+ #
103
+ # 元号を取得する(元号名)
104
+ #
105
+ # @param [String] name 元号名
106
+ #
107
+ # @return [Array<Japan::Alignment::LinearGengou>] 元号
108
+ #
109
+ def line_by_name(name:)
110
+ Japan::Gengou.line_by_name(line: index, name: name)
111
+ end
112
+
113
+ #
114
+ # 予約元号一覧を更新する
115
+ #
116
+ # すでに解決済みの開始日・終了日をそのまま使用する(前後の元号を引き当てない)
117
+ #
118
+ def update
119
+ @list |= line(start_date: start_date, last_date: last_date)
120
+ end
121
+ end
122
+ end
123
+ end
124
+ end
125
+ end
@@ -0,0 +1,73 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative './abstract_range'
4
+ require_relative './named_list'
5
+
6
+ # :nodoc:
7
+ module Zakuro
8
+ # :nodoc:
9
+ module Calculation
10
+ # :nodoc:
11
+ module Gengou
12
+ # :nodoc:
13
+ module Reserve
14
+ #
15
+ # NamedRange 予約済み計算範囲(元号名)
16
+ #
17
+ class NamedRange < AbstractRange
18
+ # @return [String] 不正元号名
19
+ INVALID_NAME = Japan::Calendar::EMPTY
20
+
21
+ #
22
+ # 初期化
23
+ #
24
+ # @param [String] start_name 開始元号名
25
+ # @param [String] last_name 終了元号名
26
+ #
27
+ def initialize(start_name: INVALID_NAME, last_name: INVALID_NAME)
28
+ @first_list = NamedList.new(first: true, start_name: start_name, last_name: last_name)
29
+ @second_list = NamedList.new(first: false, start_name: start_name, last_name: last_name)
30
+
31
+ start_date = western_start_date
32
+ last_date = western_last_date
33
+
34
+ super(start_date: start_date, last_date: last_date)
35
+ end
36
+
37
+ #
38
+ # 最新の元号から西暦終了日を取得する
39
+ #
40
+ # @return [Western::Calendar] 西暦終了日
41
+ #
42
+ def western_last_date
43
+ list = newest_list
44
+
45
+ return Western::Calendar.new if list.invalid?
46
+
47
+ list.western_last_date
48
+ end
49
+
50
+ #
51
+ # 最新の元号を取得する
52
+ #
53
+ # @return [List] 最新の元号
54
+ #
55
+ def newest_list
56
+ return first_list if invalid_list?
57
+
58
+ first_western_date = first_list.western_last_date
59
+ second_western_date = second_list.western_last_date
60
+
61
+ return first_list if first_western_date.invalid?
62
+
63
+ return first_list if second_western_date.invalid?
64
+
65
+ return first_list if first_western_date > second_western_date
66
+
67
+ second_list
68
+ end
69
+ end
70
+ end
71
+ end
72
+ end
73
+ end