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,117 @@
1
+ # frozen_string_literal: true
2
+
3
+ # :nodoc:
4
+ module Zakuro
5
+ # :nodoc:
6
+ module Context
7
+ #
8
+ # Option オプション値
9
+ #
10
+ class Option
11
+ # @return [String] 無効暦名
12
+ INVALID_VERSION_NAME = ''
13
+ # @return [Array<String>] 暦名
14
+ VERSION_NAMES = %w[Genka Gihou Daien Senmyou Joukyou Kansei Tenpou Gregorio].freeze
15
+
16
+ # @return [String] 没日オプションキー名
17
+ DROPPED_DATE_KEY = 'dropped_date'
18
+
19
+ # @return [Hash<String, Object>] オプション値
20
+ attr_reader :hash
21
+ # @return [String] デフォルト暦名
22
+ attr_reader :default_version
23
+
24
+ #
25
+ # 初期化
26
+ #
27
+ # @param [String] default_version デフォルト暦名
28
+ #
29
+ # @param [Hash<String, Object>] options オプション値
30
+ #
31
+ def initialize(default_version: INVALID_VERSION_NAME, hash: {})
32
+ @default_version = default_version
33
+ @hash = hash
34
+ end
35
+
36
+ #
37
+ # 暦名を返す
38
+ #
39
+ # @return [String] 暦名
40
+ #
41
+ def version
42
+ version = self.class.version(version: @default_version)
43
+
44
+ return version if self.class.version?(version: version)
45
+
46
+ version = hash['version']
47
+
48
+ self.class.version(version: version)
49
+ end
50
+
51
+ #
52
+ # 有効なデフォルト暦名か
53
+ #
54
+ # @return [True] 有効
55
+ # @return [False] 無効
56
+ #
57
+ def default_version?
58
+ self.class.version?(version: @default_version)
59
+ end
60
+
61
+ #
62
+ # 有効な暦名か
63
+ #
64
+ # @return [True] 有効
65
+ # @return [False] 無効
66
+ #
67
+ def version?
68
+ self.class.version?(version: version)
69
+ end
70
+
71
+ #
72
+ # 没日か
73
+ #
74
+ # @return [True] 没日あり
75
+ # @return [False] 没日なし
76
+ #
77
+ def dropped_date?
78
+ value = @hash[DROPPED_DATE_KEY]
79
+
80
+ return true if value.is_a?(TrueClass)
81
+
82
+ false
83
+ end
84
+
85
+ #
86
+ # 暦名を返す
87
+ #
88
+ # @param [String] version 暦名
89
+ #
90
+ # @return [String] 暦名
91
+ #
92
+ def self.version(version: INVALID_VERSION_NAME)
93
+ return INVALID_VERSION_NAME unless version
94
+
95
+ return INVALID_VERSION_NAME if version.empty?
96
+
97
+ return INVALID_VERSION_NAME unless VERSION_NAMES.include?(version)
98
+
99
+ version
100
+ end
101
+
102
+ #
103
+ # 有効な暦か
104
+ #
105
+ # @param [String] version 暦名
106
+ #
107
+ # @return [True] 有効
108
+ # @return [False] 無効
109
+ #
110
+ def self.version?(version: INVALID_VERSION_NAME)
111
+ return false if version == INVALID_VERSION_NAME
112
+
113
+ true
114
+ end
115
+ end
116
+ end
117
+ end
@@ -0,0 +1,110 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative '../version/genka/cycle/remainder'
4
+ require_relative '../version/genka/cycle/solar_term'
5
+ require_relative '../version/genka/range/annual_range'
6
+ require_relative '../version/genka/option/dropped_date/parameter'
7
+
8
+ require_relative '../version/gihou/cycle/remainder'
9
+ require_relative '../version/gihou/cycle/solar_term'
10
+ require_relative '../version/gihou/range/annual_range'
11
+ require_relative '../version/gihou/option/dropped_date/parameter'
12
+
13
+ require_relative '../version/daien/cycle/remainder'
14
+ require_relative '../version/daien/cycle/solar_term'
15
+ require_relative '../version/daien/range/annual_range'
16
+ require_relative '../version/daien/option/dropped_date/parameter'
17
+
18
+ require_relative '../version/senmyou/cycle/remainder'
19
+ require_relative '../version/senmyou/cycle/solar_term'
20
+ require_relative '../version/senmyou/range/annual_range'
21
+ require_relative '../version/senmyou/option/dropped_date/parameter'
22
+
23
+ # :nodoc:
24
+ module Zakuro
25
+ # :nodoc:
26
+ module Context
27
+ #
28
+ # VersionClassResolver 暦リゾルバー
29
+ #
30
+ class VersionClassResolver
31
+ # @return [String] 暦名
32
+ attr_reader :version_name
33
+
34
+ # @return [Hash<String, String>] 関連
35
+ CLASSES = {
36
+ 'remainder' => 'Zakuro::$VERSION::Cycle::Remainder',
37
+ 'solar_term' => 'Zakuro::$VERSION::Cycle::SolarTerm',
38
+ 'annual_range' => 'Zakuro::$VERSION::Range::AnnualRange',
39
+ 'dropped_date_parameter' => 'Zakuro::$VERSION::Option::DroppedDate::Parameter'
40
+ }.freeze
41
+
42
+ #
43
+ # 初期化
44
+ #
45
+ # @param [String] version_name 暦名
46
+ #
47
+ def initialize(version_name:)
48
+ @version_name = version_name
49
+ end
50
+
51
+ #
52
+ # 該当の暦のクラスを取得する
53
+ #
54
+ # @param [String] version_name 暦名
55
+ # @param [String] class_name クラス名
56
+ #
57
+ # @return [Object] 該当クラス
58
+ #
59
+ # @raise [ArgumentError] 引数エラー
60
+ #
61
+ def self.get_class(version_name:, class_name:)
62
+ constant = CLASSES.fetch(class_name, '')
63
+
64
+ raise ArgumentError.new, 'invalid class name' if constant == ''
65
+
66
+ resolved_constant = constant.gsub('$VERSION', version_name)
67
+
68
+ Object.const_get(resolved_constant)
69
+ end
70
+
71
+ #
72
+ # 大余小余(暦別)クラスを返す
73
+ #
74
+ # @return [Class] 大余小余(暦別)クラス
75
+ #
76
+ def remainder
77
+ VersionClassResolver.get_class(version_name: @version_name, class_name: 'remainder')
78
+ end
79
+
80
+ #
81
+ # 二十四節気クラスを返す
82
+ #
83
+ # @return [Class] 二十四節気クラス
84
+ #
85
+ def solar_term
86
+ VersionClassResolver.get_class(version_name: @version_name, class_name: 'solar_term')
87
+ end
88
+
89
+ #
90
+ # 年間範囲クラスを返す
91
+ #
92
+ # @return [Class] 年間範囲クラス
93
+ #
94
+ def annual_range
95
+ VersionClassResolver.get_class(version_name: @version_name, class_name: 'annual_range')
96
+ end
97
+
98
+ #
99
+ # 没日引数を返す
100
+ #
101
+ # @return [Class] 没日引数クラス
102
+ #
103
+ def dropped_date_parameter
104
+ VersionClassResolver.get_class(
105
+ version_name: @version_name, class_name: 'dropped_date_parameter'
106
+ )
107
+ end
108
+ end
109
+ end
110
+ end
@@ -0,0 +1,125 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative '../../../western/calendar'
4
+
5
+ require_relative './line'
6
+
7
+ # :nodoc:
8
+ module Zakuro
9
+ # :nodoc:
10
+ module Japan
11
+ # :nodoc:
12
+ module Gengou
13
+ #
14
+ # Alignment 整列
15
+ #
16
+ module Alignment
17
+ #
18
+ # Aligner 元号整列
19
+ #
20
+ class Aligner
21
+ # @return [Integer] 1行目元号
22
+ FIRST_LINE = 0
23
+
24
+ # @return [Integer] 2行目元号
25
+ SECOND_LINE = 1
26
+
27
+ # @return [Array<Integer>] 元号リスト
28
+ LINE_INDEXES = [FIRST_LINE, SECOND_LINE].freeze
29
+
30
+ # @return [Integer] 行数
31
+ LINE_SIZE = LINE_INDEXES.size
32
+
33
+ # @return [Array<Line>] 行元号
34
+ attr_reader :lines
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)
45
+ end
46
+
47
+ save(resources: resources)
48
+ end
49
+
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
61
+
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)
77
+ end
78
+
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)
91
+
92
+ @lines[line].get_by_name(name: name)
93
+ end
94
+
95
+ private
96
+
97
+ #
98
+ # 保存する
99
+ #
100
+ # @param [Array<Set>] resources 元号解析結果
101
+ #
102
+ def save(resources: [])
103
+ resources.each do |set|
104
+ push(set: set)
105
+ end
106
+ end
107
+
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
120
+ end
121
+ end
122
+ end
123
+ end
124
+ end
125
+ end
@@ -0,0 +1,168 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative './linear_gengou'
4
+
5
+ # :nodoc:
6
+ module Zakuro
7
+ # :nodoc:
8
+ module Japan
9
+ # :nodoc:
10
+ module Gengou
11
+ # :nodoc:
12
+ module Alignment
13
+ #
14
+ # Division 元号区分
15
+ #
16
+ module Division
17
+ #
18
+ # 重複した範囲を返す
19
+ #
20
+ # @param [LinearGengou] this 残り元号
21
+ # @param [LinearGengou] other 比較元号
22
+ #
23
+ # @return [Array<LinearGengou>] 重複分
24
+ #
25
+ def self.match(this:, other:)
26
+ result = []
27
+ if other.in?(start_date: this.start_date, last_date: this.last_date)
28
+ result.push(this)
29
+ return result
30
+ end
31
+
32
+ if other.out?(start_date: this.start_date, last_date: this.last_date)
33
+ # 該当なし
34
+ return result
35
+ end
36
+
37
+ if other.covered?(start_date: this.start_date, last_date: this.last_date)
38
+ # 範囲内のみ返す
39
+ result.push(trim_gengou(this: this, other: other))
40
+ return result
41
+ end
42
+
43
+ result.push(narrow_gengou(this: this, other: other))
44
+
45
+ result
46
+ end
47
+
48
+ #
49
+ # 範囲が重複しない差分を返す
50
+ #
51
+ # @param [LinearGengou] this 積元号
52
+ # @param [LinearGengou] other 比較元号
53
+ #
54
+ # @return [Array<LinearGengou>] 差分
55
+ #
56
+ def self.mismatch(this:, other:)
57
+ result = []
58
+ if other.in?(start_date: this.start_date, last_date: this.last_date)
59
+ # 該当なし
60
+ return result
61
+ end
62
+
63
+ if other.out?(start_date: this.start_date, last_date: this.last_date)
64
+ result.push(this)
65
+ return result
66
+ end
67
+
68
+ if other.covered?(start_date: this.start_date, last_date: this.last_date)
69
+ result |= split_gengou(this: this, other: other)
70
+ return result
71
+ end
72
+
73
+ result.push(shave_gengou(this: this, other: other))
74
+ result
75
+ end
76
+
77
+ #
78
+ # 比較により分離した元号をつなげる
79
+ #
80
+ # @param [Array<LinearGengou>] list 元号
81
+ #
82
+ # @return [Array<LinearGengou>] 同一元号がつながった元号
83
+ #
84
+ def self.connect(list: []) # rubocop:disable Metrics/MethodLength
85
+ result = []
86
+ list.each do |linear_gengou|
87
+ if result.size.zero?
88
+ result.push(linear_gengou)
89
+ next
90
+ end
91
+
92
+ before = result[-1]
93
+
94
+ if unconnectable?(current: linear_gengou, before: before)
95
+ result.push(linear_gengou)
96
+ next
97
+ end
98
+
99
+ result[-1] = LinearGengou.new(
100
+ start_date: before.start_date, last_date: linear_gengou.last_date,
101
+ gengou: linear_gengou.gengou
102
+ )
103
+ end
104
+
105
+ result
106
+ end
107
+
108
+ def self.unconnectable?(current:, before:)
109
+ return true unless current.gengou.name == before.gengou.name
110
+
111
+ return true unless (before.last_date.clone + 1) == current.start_date
112
+
113
+ false
114
+ end
115
+ private_class_method :unconnectable?
116
+
117
+ def self.split_gengou(this:, other:)
118
+ [
119
+ LinearGengou.new(
120
+ start_date: this.start_date.clone, last_date: other.start_date.clone - 1,
121
+ gengou: this.gengou
122
+ ),
123
+ LinearGengou.new(
124
+ start_date: other.last_date.clone + 1, last_date: this.last_date.clone,
125
+ gengou: this.gengou
126
+ )
127
+ ]
128
+ end
129
+ private_class_method :split_gengou
130
+
131
+ def self.trim_gengou(this:, other:)
132
+ LinearGengou.new(
133
+ start_date: other.start_date, last_date: other.last_date,
134
+ gengou: this.gengou
135
+ )
136
+ end
137
+ private_class_method :trim_gengou
138
+
139
+ def self.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
142
+
143
+ LinearGengou.new(
144
+ start_date: start.clone, last_date: last.clone, gengou: this.gengou
145
+ )
146
+ end
147
+ private_class_method :narrow_gengou
148
+
149
+ def self.shave_gengou(this:, other:) # rubocop:disable Metrics/AbcSize
150
+ start = this.start_date
151
+ last = this.last_date
152
+
153
+ # 開始日が比較元号の開始日より前の範囲
154
+ last = other.start_date.clone - 1 if this.start_date < other.start_date
155
+
156
+ # 終了日が比較元号の開始日より後の範囲
157
+ start = other.last_date.clone + 1 if this.last_date > other.last_date
158
+
159
+ LinearGengou.new(
160
+ start_date: start.clone, last_date: last.clone, gengou: this.gengou
161
+ )
162
+ end
163
+ private_class_method :shave_gengou
164
+ end
165
+ end
166
+ end
167
+ end
168
+ end
@@ -0,0 +1,175 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative './division'
4
+
5
+ require_relative './linear_gengou'
6
+
7
+ # :nodoc:
8
+ module Zakuro
9
+ # :nodoc:
10
+ module Japan
11
+ # :nodoc:
12
+ module Gengou
13
+ # :nodoc:
14
+ module Alignment
15
+ #
16
+ # Line 行
17
+ #
18
+ class Line
19
+ # @return [Array<LinearGengou>] 元号
20
+ attr_reader :list
21
+
22
+ #
23
+ # 初期化
24
+ #
25
+ def initialize
26
+ @list = []
27
+ end
28
+
29
+ #
30
+ # 追加する
31
+ #
32
+ # 下記のパターンが存在し、戻り値は重複分となる
33
+ #
34
+ # 1. 完全に範囲外(開始日より前)
35
+ # [@list]: |-------|-------|
36
+ # [list]: |---|
37
+ # 2. 前半のみ範囲外
38
+ # [@list]: |-------|-------|
39
+ # [list]: |------|
40
+ # 3. 範囲内
41
+ # [@list]: |-------|-------|
42
+ # [list]: |------|
43
+ # 4. 後半のみ範囲外
44
+ # [@list]: |-------|-------|
45
+ # [list]: |------|
46
+ # 5. 完全に範囲外(開始日より後)
47
+ # [@list]: |-------|-------|
48
+ # [list]: |----|
49
+ # 6. 両端が範囲外
50
+ # [@list]: |-------|-------|
51
+ # [list]: |--------------------|
52
+ #
53
+ # @param [Array<LinearGengou>] list 元号
54
+ #
55
+ # @return [Array<LinearGengou>] 未登録元号
56
+ #
57
+ def push(list: [])
58
+ rest = rest(list: list)
59
+
60
+ insert(list: list)
61
+
62
+ rest
63
+ end
64
+
65
+ #
66
+ # 範囲内の元号を取得する
67
+ #
68
+ # @param [Western::Calendar] start_date 開始日
69
+ # @param [Western::Calendar] last_date 終了日
70
+ #
71
+ # @return [Array<LinearGengou>] 元号
72
+ #
73
+ def get(start_date:, last_date:)
74
+ result = []
75
+ @list.each do |gengou|
76
+ next if gengou.out?(start_date: start_date, last_date: last_date)
77
+
78
+ # 1日でも範囲内であれば対象とみなす
79
+ result.push(gengou)
80
+ end
81
+
82
+ result
83
+ end
84
+
85
+ #
86
+ # 範囲内の元号を取得する(元号名)
87
+ #
88
+ # @param [String] name 元号名
89
+ #
90
+ # @return [Array<LinearGengou>] 元号
91
+ #
92
+ def get_by_name(name:)
93
+ result = []
94
+ @list.each do |gengou|
95
+ result.push(gengou) if gengou.name == name
96
+ end
97
+
98
+ result
99
+ end
100
+
101
+ private
102
+
103
+ #
104
+ # 重複分(空きがないため追加できない範囲の元号)を返す
105
+ #
106
+ # @param [Array<LinearGengou>] list 元号
107
+ #
108
+ # @return [Array<LinearGengou>] 重複分元号
109
+ #
110
+ def rest(list: [])
111
+ result = []
112
+
113
+ @list.each do |gengou|
114
+ result |= and!(rest: list, other: gengou)
115
+ end
116
+
117
+ Division.connect(list: result)
118
+ end
119
+
120
+ #
121
+ # 空き範囲に元号を登録する
122
+ #
123
+ # @param [Array<LinearGengou>] list 元号
124
+ #
125
+ def insert(list: [])
126
+ surplus_result = list.clone
127
+ @list.each do |gengou|
128
+ surplus_result = not!(surplus: surplus_result, other: gengou)
129
+ end
130
+
131
+ surplus_result = Division.connect(list: surplus_result)
132
+
133
+ surplus_result.each do |gengou|
134
+ @list.push(gengou)
135
+ end
136
+ end
137
+
138
+ #
139
+ # 重複した範囲を返す
140
+ #
141
+ # @param [Array<LinearGengou>] rest 残り元号
142
+ # @param [LinearGengou] other 比較元号
143
+ #
144
+ # @return [Array<LinearGengou>] 重複分
145
+ #
146
+ def and!(rest:, other:)
147
+ result = []
148
+ rest.each do |gengou|
149
+ result |= Division.match(this: gengou, other: other)
150
+ end
151
+
152
+ result
153
+ end
154
+
155
+ #
156
+ # 範囲が重複しない差分を返す
157
+ #
158
+ # @param [Array<LinearGengou>] surplus 積元号
159
+ # @param [LinearGengou] other 比較元号
160
+ #
161
+ # @return [Array<LinearGengou>] 差分
162
+ #
163
+ def not!(surplus:, other:)
164
+ result = []
165
+ surplus.each do |gengou|
166
+ result |= Division.mismatch(this: gengou, other: other)
167
+ end
168
+
169
+ result
170
+ end
171
+ end
172
+ end
173
+ end
174
+ end
175
+ end