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
@@ -6,98 +6,117 @@ require_relative './line'
6
6
 
7
7
  # :nodoc:
8
8
  module Zakuro
9
- #
10
- # Japan 和暦
11
- #
9
+ # :nodoc:
12
10
  module Japan
13
- #
14
- # Alignment 整列
15
- #
16
- module Alignment
11
+ # :nodoc:
12
+ module Gengou
17
13
  #
18
- # Aligner 元号整列
14
+ # Alignment 整列
19
15
  #
20
- class Aligner
21
- # @return [Integer] 1行目元号
22
- FIRST_LINE = 0
16
+ module Alignment
17
+ #
18
+ # Aligner 元号整列
19
+ #
20
+ class Aligner
21
+ # @return [Integer] 1行目元号
22
+ FIRST_LINE = 0
23
23
 
24
- # @return [Integer] 2行目元号
25
- SECOND_LINE = 1
24
+ # @return [Integer] 2行目元号
25
+ SECOND_LINE = 1
26
26
 
27
- # @return [Array<Integer>] 元号リスト
28
- LINE_INDEXES = [FIRST_LINE, SECOND_LINE].freeze
27
+ # @return [Array<Integer>] 元号リスト
28
+ LINE_INDEXES = [FIRST_LINE, SECOND_LINE].freeze
29
29
 
30
- # @return [Integer] 行数
31
- LINE_SIZE = LINE_INDEXES.size
30
+ # @return [Integer] 行数
31
+ LINE_SIZE = LINE_INDEXES.size
32
32
 
33
- # @return [Array<Line>] 行元号
34
- attr_reader :lines
33
+ # @return [Array<Line>] 行元号
34
+ attr_reader :lines
35
35
 
36
- #
37
- # 初期化
38
- #
39
- # @param [Array<Set>] resources 元号解析結果
40
- #
41
- def initialize(resources: [])
42
- @lines = []
43
- (1..LINE_SIZE).each do |_index|
44
- @lines.push(Line.new)
36
+ #
37
+ # 初期化
38
+ #
39
+ # @param [Array<Set>] resources 元号解析結果
40
+ #
41
+ def initialize(resources: [])
42
+ @lines = []
43
+ (1..LINE_SIZE).each do |_index|
44
+ lines.push(Line.new)
45
+ end
46
+
47
+ save(resources: resources)
45
48
  end
46
49
 
47
- save(resources: resources)
48
- end
50
+ #
51
+ # 行元号に追加する
52
+ #
53
+ # @param [Set] set 元号セット
54
+ #
55
+ def push(set:)
56
+ list = set.list
57
+ list.each do |gengou|
58
+ push_gengou(gengou: gengou)
59
+ end
60
+ end
49
61
 
50
- #
51
- # 行元号に追加する
52
- #
53
- # @param [Set] set 元号セット
54
- #
55
- def push(set:)
56
- list = set.list
57
- list.each do |gengou|
58
- push_gengou(gengou: gengou)
62
+ #
63
+ # 指定した範囲内の元号を取得する
64
+ #
65
+ # @param [Integer] line
66
+ # @param [Western::Calendar] start_date 開始日
67
+ # @param [Western::Calendar] last_date 終了日
68
+ #
69
+ # @return [Array<LinearGengou>] 元号
70
+ #
71
+ # @raise [ArgumentError] 引数エラー
72
+ #
73
+ def get(line:, start_date:, last_date:)
74
+ raise ArgumentError.new, 'invalid line number' unless LINE_INDEXES.include?(line)
75
+
76
+ lines[line].get(start_date: start_date, last_date: last_date)
59
77
  end
60
- end
61
78
 
62
- #
63
- # 指定した範囲内の元号を取得する
64
- #
65
- # @param [Integer] line 行
66
- # @param [Western::Calendar] start_date 開始日
67
- # @param [Western::Calendar] last_date 終了日
68
- #
69
- # @return [Array<LinearGengou>] 元号
70
- #
71
- def get(line:, start_date:, last_date:)
72
- raise ArgumentError.new, 'invalid line number' unless LINE_INDEXES.include?(line)
79
+ #
80
+ # 指定した範囲内の元号を取得する(元号名)
81
+ #
82
+ # @param [Integer] line 行
83
+ # @param [String] name 元号名
84
+ #
85
+ # @return [Array<LinearGengou>] 元号
86
+ #
87
+ # @raise [ArgumentError] 引数エラー
88
+ #
89
+ def get_by_name(line:, name:)
90
+ raise ArgumentError.new, 'invalid line number' unless LINE_INDEXES.include?(line)
73
91
 
74
- @lines[line].get(start_date: start_date, last_date: last_date)
75
- end
92
+ lines[line].get_by_name(name: name)
93
+ end
76
94
 
77
- private
95
+ private
78
96
 
79
- #
80
- # 保存する
81
- #
82
- # @param [Array<Set>] resources 元号解析結果
83
- #
84
- def save(resources: [])
85
- resources.each do |set|
86
- push(set: set)
97
+ #
98
+ # 保存する
99
+ #
100
+ # @param [Array<Set>] resources 元号解析結果
101
+ #
102
+ def save(resources: [])
103
+ resources.each do |set|
104
+ push(set: set)
105
+ end
87
106
  end
88
- end
89
107
 
90
- #
91
- # 元号を追加する
92
- #
93
- # @param [Gengou] gengou 元号
94
- #
95
- def push_gengou(gengou:)
96
- rest = [
97
- LinearGengou.new(gengou: gengou)
98
- ]
99
- @lines.each do |line|
100
- rest = line.push(list: rest)
108
+ #
109
+ # 元号を追加する
110
+ #
111
+ # @param [Gengou] gengou 元号
112
+ #
113
+ def push_gengou(gengou:)
114
+ rest = [
115
+ LinearGengou.new(gengou: gengou)
116
+ ]
117
+ lines.each do |line|
118
+ rest = line.push(list: rest)
119
+ end
101
120
  end
102
121
  end
103
122
  end
@@ -7,158 +7,160 @@ module Zakuro
7
7
  # :nodoc:
8
8
  module Japan
9
9
  # :nodoc:
10
- module Alignment
11
- #
12
- # Division 元号区分
13
- #
14
- module Division
10
+ module Gengou
11
+ # :nodoc:
12
+ module Alignment
15
13
  #
16
- # 重複した範囲を返す
14
+ # Division 元号区分
17
15
  #
18
- # @param [LinearGengou] this 残り元号
19
- # @param [LinearGengou] other 比較元号
20
- #
21
- # @return [Array<LinearGengou>] 重複分
22
- #
23
- def self.match(this:, other:)
24
- result = []
25
- if other.in?(start_date: this.start_date, last_date: this.last_date)
26
- result.push(this)
27
- return result
28
- end
29
-
30
- if other.out?(start_date: this.start_date, last_date: this.last_date)
31
- # 該当なし
32
- return result
33
- end
34
-
35
- if other.covered?(start_date: this.start_date, last_date: this.last_date)
36
- # 範囲内のみ返す
37
- result.push(trim_gengou(this: this, other: other))
38
- return result
39
- end
40
-
41
- result.push(narrow_gengou(this: this, other: other))
42
-
43
- result
44
- end
45
-
46
- #
47
- # 範囲が重複しない差分を返す
48
- #
49
- # @param [LinearGengou] this 積元号
50
- # @param [LinearGengou] other 比較元号
51
- #
52
- # @return [Array<LinearGengou>] 差分
53
- #
54
- def self.mismatch(this:, other:)
55
- result = []
56
- if other.in?(start_date: this.start_date, last_date: this.last_date)
57
- # 該当なし
58
- return result
59
- end
60
-
61
- if other.out?(start_date: this.start_date, last_date: this.last_date)
62
- result.push(this)
63
- return result
64
- end
65
-
66
- if other.covered?(start_date: this.start_date, last_date: this.last_date)
67
- result |= split_gengou(this: this, other: other)
68
- return result
69
- end
70
-
71
- result.push(shave_gengou(this: this, other: other))
72
- result
73
- end
74
-
75
- #
76
- # 比較により分離した元号をつなげる
77
- #
78
- # @param [Array<LinearGengou>] list 元号
79
- #
80
- # @return [Array<LinearGengou>] 同一元号がつながった元号
81
- #
82
- def self.connect(list: []) # rubocop:disable Metrics/MethodLength
83
- result = []
84
- list.each do |linear_gengou|
85
- if result.size.zero?
86
- result.push(linear_gengou)
87
- next
16
+ module Division
17
+ class << self
18
+ #
19
+ # 重複した範囲を返す
20
+ #
21
+ # @param [LinearGengou] this 残り元号
22
+ # @param [LinearGengou] other 比較元号
23
+ #
24
+ # @return [Array<LinearGengou>] 重複分
25
+ #
26
+ def match(this:, other:)
27
+ result = []
28
+ if other.in?(start_date: this.start_date, last_date: this.last_date)
29
+ result.push(this)
30
+ return result
31
+ end
32
+
33
+ if other.out?(start_date: this.start_date, last_date: this.last_date)
34
+ # 該当なし
35
+ return result
36
+ end
37
+
38
+ if other.covered?(start_date: this.start_date, last_date: this.last_date)
39
+ # 範囲内のみ返す
40
+ result.push(trim_gengou(this: this, other: other))
41
+ return result
42
+ end
43
+
44
+ result.push(narrow_gengou(this: this, other: other))
45
+
46
+ result
88
47
  end
89
48
 
90
- before = result[-1]
49
+ #
50
+ # 範囲が重複しない差分を返す
51
+ #
52
+ # @param [LinearGengou] this 積元号
53
+ # @param [LinearGengou] other 比較元号
54
+ #
55
+ # @return [Array<LinearGengou>] 差分
56
+ #
57
+ def mismatch(this:, other:)
58
+ result = []
59
+ if other.in?(start_date: this.start_date, last_date: this.last_date)
60
+ # 該当なし
61
+ return result
62
+ end
63
+
64
+ if other.out?(start_date: this.start_date, last_date: this.last_date)
65
+ result.push(this)
66
+ return result
67
+ end
68
+
69
+ if other.covered?(start_date: this.start_date, last_date: this.last_date)
70
+ result |= split_gengou(this: this, other: other)
71
+ return result
72
+ end
73
+
74
+ result.push(shave_gengou(this: this, other: other))
75
+ result
76
+ end
91
77
 
92
- if unconnectable?(current: linear_gengou, before: before)
93
- result.push(linear_gengou)
94
- next
78
+ #
79
+ # 比較により分離した元号をつなげる
80
+ #
81
+ # @param [Array<LinearGengou>] list 元号
82
+ #
83
+ # @return [Array<LinearGengou>] 同一元号がつながった元号
84
+ #
85
+ def connect(list: []) # rubocop:disable Metrics/MethodLength
86
+ result = []
87
+ list.each do |linear_gengou|
88
+ if result.size.zero?
89
+ result.push(linear_gengou)
90
+ next
91
+ end
92
+
93
+ before = result[-1]
94
+
95
+ if unconnectable?(current: linear_gengou, before: before)
96
+ result.push(linear_gengou)
97
+ next
98
+ end
99
+
100
+ result[-1] = LinearGengou.new(
101
+ start_date: before.start_date, last_date: linear_gengou.last_date,
102
+ gengou: linear_gengou.gengou
103
+ )
104
+ end
105
+
106
+ result
95
107
  end
96
108
 
97
- result[-1] = LinearGengou.new(
98
- start_date: before.start_date, last_date: linear_gengou.last_date,
99
- gengou: linear_gengou.gengou
100
- )
101
- end
109
+ private
102
110
 
103
- result
104
- end
111
+ def unconnectable?(current:, before:)
112
+ return true unless current.gengou.name == before.gengou.name
105
113
 
106
- def self.unconnectable?(current:, before:)
107
- return true unless current.gengou.name == before.gengou.name
114
+ return true unless (before.last_date.clone + 1) == current.start_date
108
115
 
109
- return true unless (before.last_date.clone + 1) == current.start_date
116
+ false
117
+ end
110
118
 
111
- false
112
- end
113
- private_class_method :unconnectable?
114
-
115
- def self.split_gengou(this:, other:)
116
- [
117
- LinearGengou.new(
118
- start_date: this.start_date.clone, last_date: other.start_date.clone - 1,
119
- gengou: this.gengou
120
- ),
121
- LinearGengou.new(
122
- start_date: other.last_date.clone + 1, last_date: this.last_date.clone,
123
- gengou: this.gengou
124
- )
125
- ]
126
- end
127
- private_class_method :split_gengou
119
+ def split_gengou(this:, other:)
120
+ [
121
+ LinearGengou.new(
122
+ start_date: this.start_date.clone, last_date: other.start_date.clone - 1,
123
+ gengou: this.gengou
124
+ ),
125
+ LinearGengou.new(
126
+ start_date: other.last_date.clone + 1, last_date: this.last_date.clone,
127
+ gengou: this.gengou
128
+ )
129
+ ]
130
+ end
128
131
 
129
- def self.trim_gengou(this:, other:)
130
- LinearGengou.new(
131
- start_date: other.start_date, last_date: other.last_date,
132
- gengou: this.gengou
133
- )
134
- end
135
- private_class_method :trim_gengou
132
+ def trim_gengou(this:, other:)
133
+ LinearGengou.new(
134
+ start_date: other.start_date, last_date: other.last_date,
135
+ gengou: this.gengou
136
+ )
137
+ end
136
138
 
137
- def self.narrow_gengou(this:, other:)
138
- start = this.start_date > other.start_date ? this.start_date : other.start_date
139
- last = this.last_date < other.last_date ? this.last_date : other.last_date
139
+ def narrow_gengou(this:, other:)
140
+ start = this.start_date > other.start_date ? this.start_date : other.start_date
141
+ last = this.last_date < other.last_date ? this.last_date : other.last_date
140
142
 
141
- LinearGengou.new(
142
- start_date: start.clone, last_date: last.clone, gengou: this.gengou
143
- )
144
- end
145
- private_class_method :narrow_gengou
143
+ LinearGengou.new(
144
+ start_date: start.clone, last_date: last.clone, gengou: this.gengou
145
+ )
146
+ end
146
147
 
147
- def self.shave_gengou(this:, other:) # rubocop:disable Metrics/AbcSize
148
- start = this.start_date
149
- last = this.last_date
148
+ def shave_gengou(this:, other:) # rubocop:disable Metrics/AbcSize
149
+ start = this.start_date
150
+ last = this.last_date
150
151
 
151
- # 開始日が比較元号の開始日より前の範囲
152
- last = other.start_date.clone - 1 if this.start_date < other.start_date
152
+ # 開始日が比較元号の開始日より前の範囲
153
+ last = other.start_date.clone - 1 if this.start_date < other.start_date
153
154
 
154
- # 終了日が比較元号の開始日より後の範囲
155
- start = other.last_date.clone + 1 if this.last_date > other.last_date
155
+ # 終了日が比較元号の開始日より後の範囲
156
+ start = other.last_date.clone + 1 if this.last_date > other.last_date
156
157
 
157
- LinearGengou.new(
158
- start_date: start.clone, last_date: last.clone, gengou: this.gengou
159
- )
158
+ LinearGengou.new(
159
+ start_date: start.clone, last_date: last.clone, gengou: this.gengou
160
+ )
161
+ end
162
+ end
160
163
  end
161
- private_class_method :shave_gengou
162
164
  end
163
165
  end
164
166
  end