zakuro 0.5.0 → 0.7.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Makefile +3 -0
- data/README.md +22 -7
- data/VERSION +1 -0
- data/doc/operation/transfer.rb +8 -8
- data/lib/zakuro/calculation/base/gengou.rb +128 -0
- data/lib/zakuro/calculation/base/linear_gengou.rb +76 -0
- data/lib/zakuro/calculation/base/operated_year.rb +5 -15
- data/lib/zakuro/calculation/base/year.rb +26 -14
- data/lib/zakuro/calculation/era/gengou/abstract_scroll.rb +209 -0
- data/lib/zakuro/calculation/era/gengou/dated_scroll.rb +30 -0
- data/lib/zakuro/calculation/era/gengou/internal/connector.rb +107 -0
- data/lib/zakuro/calculation/era/gengou/internal/counter.rb +214 -0
- data/lib/zakuro/calculation/era/gengou/internal/publisher.rb +104 -0
- data/lib/zakuro/calculation/era/gengou/internal/reserve/abstract_list.rb +230 -0
- data/lib/zakuro/calculation/era/gengou/internal/reserve/abstract_range.rb +193 -0
- data/lib/zakuro/calculation/era/gengou/internal/reserve/dated_list.rb +48 -0
- data/lib/zakuro/calculation/era/gengou/internal/reserve/dated_range.rb +30 -0
- data/lib/zakuro/calculation/era/gengou/internal/reserve/empty_link.rb +158 -0
- data/lib/zakuro/calculation/era/gengou/internal/reserve/named_list.rb +125 -0
- data/lib/zakuro/calculation/era/gengou/internal/reserve/named_range.rb +75 -0
- data/lib/zakuro/calculation/era/gengou/internal/reserve.rb +41 -0
- data/lib/zakuro/calculation/era/gengou/named_scroll.rb +30 -0
- data/lib/zakuro/calculation/era/version/internal/crawler.rb +51 -0
- data/lib/zakuro/calculation/era/version/internal/range.rb +39 -0
- data/lib/zakuro/calculation/era/version/version.rb +24 -0
- data/lib/zakuro/calculation/monthly/first_day.rb +3 -2
- data/lib/zakuro/calculation/monthly/month.rb +88 -2
- data/lib/zakuro/calculation/monthly/operated_month.rb +3 -3
- data/lib/zakuro/calculation/range/abstract_full_range.rb +172 -0
- data/lib/zakuro/calculation/range/{operated_range.rb → abstract_operation_range.rb} +17 -15
- data/lib/zakuro/calculation/range/dated_full_range.rb +32 -0
- data/lib/zakuro/calculation/range/dated_operation_range.rb +33 -0
- data/lib/zakuro/calculation/range/named_full_range.rb +38 -0
- data/lib/zakuro/calculation/range/named_operation_range.rb +36 -0
- data/lib/zakuro/calculation/range/operated_solar_terms.rb +36 -19
- data/lib/zakuro/calculation/range/transfer/gengou_scroller.rb +54 -0
- data/lib/zakuro/calculation/range/transfer/year_boundary.rb +25 -21
- data/lib/zakuro/calculation/summary/internal/month.rb +84 -0
- data/lib/zakuro/calculation/summary/{single.rb → internal/operation.rb} +6 -37
- data/lib/zakuro/calculation/summary/japan/range.rb +152 -0
- data/lib/zakuro/calculation/summary/japan/single.rb +118 -0
- data/lib/zakuro/calculation/summary/japan/specifier/single_day.rb +91 -0
- data/lib/zakuro/calculation/summary/western/range.rb +124 -0
- data/lib/zakuro/calculation/summary/western/single.rb +113 -0
- data/lib/zakuro/calculation/summary/western/specifier/multiple_day.rb +169 -0
- data/lib/zakuro/calculation/summary/western/specifier/single_day.rb +93 -0
- data/lib/zakuro/condition.rb +15 -2
- data/lib/zakuro/era/japan/calendar.rb +133 -0
- data/lib/zakuro/era/japan/gengou/alignment/aligner.rb +121 -0
- data/lib/zakuro/era/japan/gengou/alignment/division.rb +168 -0
- data/lib/zakuro/era/japan/gengou/alignment/line.rb +175 -0
- data/lib/zakuro/era/japan/gengou/alignment/linear_gengou.rb +187 -0
- data/lib/zakuro/era/japan/gengou/alignment.rb +55 -0
- data/lib/zakuro/era/japan/gengou/resource/parser.rb +243 -0
- data/lib/zakuro/era/japan/gengou/resource/type.rb +293 -0
- data/lib/zakuro/era/japan/gengou/resource/validator.rb +347 -0
- data/lib/zakuro/era/japan/gengou/resource/yaml/set-001-until-south.yaml +1870 -0
- data/lib/zakuro/era/japan/gengou/resource/yaml/set-002-from-north.yaml +810 -0
- data/lib/zakuro/era/japan/gengou/resource/yaml/set-003-modern.yaml +50 -0
- data/lib/zakuro/era/japan/gengou/resource.rb +33 -0
- data/lib/zakuro/era/japan/gengou.rb +26 -80
- data/lib/zakuro/era/japan/version.rb +151 -0
- data/lib/zakuro/era/{western.rb → western/calendar.rb} +9 -9
- data/lib/zakuro/exception/case/pattern.rb +35 -0
- data/lib/zakuro/exception/case/preset.rb +51 -0
- data/lib/zakuro/exception/case/template.rb +48 -0
- data/lib/zakuro/exception/cause.rb +28 -0
- data/lib/zakuro/exception/exception.rb +37 -0
- data/lib/zakuro/exception/zakuro_error.rb +28 -0
- data/lib/zakuro/gateway/locale/date.rb +127 -0
- data/lib/zakuro/gateway/locale/range.rb +67 -0
- data/lib/zakuro/gateway/range.rb +99 -0
- data/lib/zakuro/gateway/single.rb +70 -0
- data/lib/zakuro/merchant.rb +15 -4
- data/lib/zakuro/operation/month/parser.rb +10 -10
- data/lib/zakuro/operation/month/type.rb +32 -32
- data/lib/zakuro/operation/month/validator.rb +51 -51
- data/lib/zakuro/operation/yaml/month.yaml +3736 -3736
- data/lib/zakuro/output/logger.rb +17 -3
- data/lib/zakuro/output/response.rb +5 -5
- data/lib/zakuro/result/data/day.rb +43 -0
- data/lib/zakuro/result/data/gengou.rb +35 -0
- data/lib/zakuro/result/data/month.rb +63 -0
- data/lib/zakuro/result/data/single_day.rb +48 -0
- data/lib/zakuro/result/data/solar_term.rb +35 -0
- data/lib/zakuro/result/data/year.rb +45 -0
- data/lib/zakuro/result/operation/month/annotation.rb +40 -0
- data/lib/zakuro/result/operation/month/bundle.rb +36 -0
- data/lib/zakuro/result/operation/month/history.rb +54 -0
- data/lib/zakuro/result/operation/operation.rb +44 -0
- data/lib/zakuro/result/result.rb +21 -2
- data/lib/zakuro/tools/typeconv.rb +38 -0
- data/lib/zakuro/tools/typeof.rb +4 -1
- data/lib/zakuro/version/context.rb +24 -3
- data/lib/zakuro/version/daien/daien.rb +1 -26
- data/lib/zakuro/version/genka/genka.rb +1 -26
- data/lib/zakuro/version/gihou/gihou.rb +1 -30
- data/lib/zakuro/version/gregorio/gregorio.rb +1 -9
- data/lib/zakuro/version/houryaku/houryaku.rb +1 -9
- data/lib/zakuro/version/joukyou/joukyou.rb +1 -9
- data/lib/zakuro/version/kansei/kansei.rb +1 -9
- data/lib/zakuro/version/senmyou/README.md +11 -7
- data/lib/zakuro/version/senmyou/senmyou.rb +1 -30
- data/lib/zakuro/version/tenpou/tenpou.rb +1 -9
- data/zakuro.gemspec +1 -3
- metadata +74 -22
- data/lib/zakuro/calculation/base/multi_gengou.rb +0 -101
- data/lib/zakuro/calculation/base/multi_gengou_roller.rb +0 -218
- data/lib/zakuro/calculation/range/full_range.rb +0 -210
- data/lib/zakuro/calculation/range/transfer/western_date_allocation.rb +0 -82
- data/lib/zakuro/calculation/specifier/single_day.rb +0 -109
- data/lib/zakuro/era/japan/gengou/parser.rb +0 -167
- data/lib/zakuro/era/japan/gengou/type.rb +0 -178
- data/lib/zakuro/era/japan/gengou/validator.rb +0 -236
- data/lib/zakuro/era/japan/reki.rb +0 -91
- data/lib/zakuro/era/japan/yaml/set-001-until-south.yaml +0 -1121
- data/lib/zakuro/era/japan/yaml/set-002-from-north.yaml +0 -485
- data/lib/zakuro/era/japan/yaml/set-003-modern.yaml +0 -28
- data/lib/zakuro/result/data.rb +0 -187
- data/lib/zakuro/result/operation.rb +0 -114
- data/lib/zakuro/version/abstract_version.rb +0 -29
- data/lib/zakuro/version.rb +0 -7
- data/lib/zakuro/version_factory.rb +0 -59
@@ -0,0 +1,133 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative '../../tools/typeconv'
|
4
|
+
|
5
|
+
# :nodoc:
|
6
|
+
module Zakuro
|
7
|
+
#
|
8
|
+
# Japan 和暦
|
9
|
+
#
|
10
|
+
module Japan
|
11
|
+
#
|
12
|
+
# Calendar 年月日情報(和暦)
|
13
|
+
#
|
14
|
+
class Calendar
|
15
|
+
# @return [Integer] 無効値
|
16
|
+
INVALID = -1
|
17
|
+
# @return [String] 空文字列
|
18
|
+
EMPTY = ''
|
19
|
+
# @return [String] 閏を示す文字列
|
20
|
+
LEAPED_TEXT = '閏'
|
21
|
+
# @return [Regexp] 和暦日フォーマット
|
22
|
+
FORMAT = /^([一-龥]{2,4})([0-9]+)年(#{LEAPED_TEXT})?([0-9]+)月([0-9]+)日$/.freeze
|
23
|
+
# @return [String] 出力用デフォルトフォーマット
|
24
|
+
DEFAULT_OUTPUT_FORM = '%s%02d年%s%02d月%02d日'
|
25
|
+
|
26
|
+
# @return [String] 元号
|
27
|
+
attr_reader :gengou
|
28
|
+
# @return [Integer] 元号年
|
29
|
+
attr_reader :year
|
30
|
+
# @return [True] 閏あり
|
31
|
+
# @return [False] 閏なし
|
32
|
+
attr_reader :leaped
|
33
|
+
# @return [Integer] 月
|
34
|
+
attr_reader :month
|
35
|
+
# @return [Integer] 日
|
36
|
+
attr_reader :day
|
37
|
+
|
38
|
+
#
|
39
|
+
# 初期化
|
40
|
+
#
|
41
|
+
# @param [String] gengou 元号
|
42
|
+
# @param [Integer] year 元号年
|
43
|
+
# @param [True, False] leaped 閏
|
44
|
+
# @param [Integer] month 月
|
45
|
+
# @param [Integer] day 日
|
46
|
+
#
|
47
|
+
def initialize(gengou: EMPTY, year: INVALID, leaped: false, month: INVALID, day: INVALID)
|
48
|
+
@gengou = gengou
|
49
|
+
@year = year
|
50
|
+
@leaped = leaped
|
51
|
+
@month = month
|
52
|
+
@day = day
|
53
|
+
end
|
54
|
+
|
55
|
+
#
|
56
|
+
# 無効か
|
57
|
+
#
|
58
|
+
# @return [True] 無効
|
59
|
+
# @return [False] 有効
|
60
|
+
#
|
61
|
+
def invalid?
|
62
|
+
@gengou == EMPTY || @year == INVALID || @month == INVALID || @day == INVALID
|
63
|
+
end
|
64
|
+
|
65
|
+
#
|
66
|
+
# 文字列にする
|
67
|
+
#
|
68
|
+
# @return [String] 和暦日フォーマット文字列
|
69
|
+
#
|
70
|
+
def format(form: DEFAULT_OUTPUT_FORM)
|
71
|
+
leaped_text = @leaped ? LEAPED_TEXT : ''
|
72
|
+
super(form, @gengou, @year, leaped_text, @month, @day)
|
73
|
+
end
|
74
|
+
|
75
|
+
#
|
76
|
+
# 同月か
|
77
|
+
# 年と日は無視する
|
78
|
+
#
|
79
|
+
# @param [True, False] leaped 閏
|
80
|
+
# @param [Integer] month 月
|
81
|
+
#
|
82
|
+
# @return [True] 同月
|
83
|
+
# @return [True] 同月ではない
|
84
|
+
#
|
85
|
+
def same_month?(leaped:, month:)
|
86
|
+
return false unless @leaped == leaped
|
87
|
+
|
88
|
+
@month == month
|
89
|
+
end
|
90
|
+
|
91
|
+
#
|
92
|
+
# 年月日情報(和暦)を生成する
|
93
|
+
#
|
94
|
+
# @param [Regexp] regex 正規表現
|
95
|
+
# @param [String] text 和暦日文字列
|
96
|
+
#
|
97
|
+
# @return [Calendar] 年月日情報(和暦)
|
98
|
+
#
|
99
|
+
def self.parse(regex: FORMAT, text: '')
|
100
|
+
return Calendar.new unless valid_date_string(regex: regex, text: text)
|
101
|
+
|
102
|
+
matched = text.match(regex)
|
103
|
+
|
104
|
+
Calendar.new(
|
105
|
+
gengou: matched[1],
|
106
|
+
year: Tools::Typeconv.to_i(text: matched[2], default: INVALID),
|
107
|
+
leaped: matched[3] ? true : false,
|
108
|
+
month: Tools::Typeconv.to_i(text: matched[4], default: INVALID),
|
109
|
+
day: Tools::Typeconv.to_i(text: matched[5], default: INVALID)
|
110
|
+
)
|
111
|
+
end
|
112
|
+
|
113
|
+
#
|
114
|
+
# 日付文字列を検証する
|
115
|
+
#
|
116
|
+
# @param [Regexp] regex 正規表現
|
117
|
+
# @param [String] text 和暦日文字列
|
118
|
+
#
|
119
|
+
# @return [True] 正しい
|
120
|
+
# @return [True] 正しくない
|
121
|
+
#
|
122
|
+
def self.valid_date_string(regex: FORMAT, text: '')
|
123
|
+
return false unless text
|
124
|
+
|
125
|
+
matched = text.match(regex)
|
126
|
+
|
127
|
+
return false unless matched
|
128
|
+
|
129
|
+
matched.size == 6
|
130
|
+
end
|
131
|
+
end
|
132
|
+
end
|
133
|
+
end
|
@@ -0,0 +1,121 @@
|
|
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
|
+
def get(line:, start_date:, last_date:)
|
72
|
+
raise ArgumentError.new, 'invalid line number' unless LINE_INDEXES.include?(line)
|
73
|
+
|
74
|
+
@lines[line].get(start_date: start_date, last_date: last_date)
|
75
|
+
end
|
76
|
+
|
77
|
+
#
|
78
|
+
# 指定した範囲内の元号を取得する(元号名)
|
79
|
+
#
|
80
|
+
# @param [Integer] line 行
|
81
|
+
# @param [String] name 元号名
|
82
|
+
#
|
83
|
+
# @return [Array<LinearGengou>] 元号
|
84
|
+
#
|
85
|
+
def get_by_name(line:, name:)
|
86
|
+
raise ArgumentError.new, 'invalid line number' unless LINE_INDEXES.include?(line)
|
87
|
+
|
88
|
+
@lines[line].get_by_name(name: name)
|
89
|
+
end
|
90
|
+
|
91
|
+
private
|
92
|
+
|
93
|
+
#
|
94
|
+
# 保存する
|
95
|
+
#
|
96
|
+
# @param [Array<Set>] resources 元号解析結果
|
97
|
+
#
|
98
|
+
def save(resources: [])
|
99
|
+
resources.each do |set|
|
100
|
+
push(set: set)
|
101
|
+
end
|
102
|
+
end
|
103
|
+
|
104
|
+
#
|
105
|
+
# 元号を追加する
|
106
|
+
#
|
107
|
+
# @param [Gengou] gengou 元号
|
108
|
+
#
|
109
|
+
def push_gengou(gengou:)
|
110
|
+
rest = [
|
111
|
+
LinearGengou.new(gengou: gengou)
|
112
|
+
]
|
113
|
+
@lines.each do |line|
|
114
|
+
rest = line.push(list: rest)
|
115
|
+
end
|
116
|
+
end
|
117
|
+
end
|
118
|
+
end
|
119
|
+
end
|
120
|
+
end
|
121
|
+
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
|