zakuro 0.6.0 → 0.6.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (50) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +16 -3
  3. data/VERSION +1 -1
  4. data/lib/zakuro/calculation/base/gengou.rb +4 -4
  5. data/lib/zakuro/calculation/base/linear_gengou.rb +6 -6
  6. data/lib/zakuro/calculation/era/gengou/internal/connector.rb +107 -0
  7. data/lib/zakuro/calculation/era/gengou/internal/counter.rb +214 -0
  8. data/lib/zakuro/calculation/era/gengou/internal/publisher.rb +104 -0
  9. data/lib/zakuro/calculation/era/gengou/internal/reserve/list.rb +244 -0
  10. data/lib/zakuro/calculation/era/gengou/internal/reserve/range.rb +193 -0
  11. data/lib/zakuro/calculation/{gengou → era/gengou}/internal/reserve.rb +6 -6
  12. data/lib/zakuro/calculation/{gengou → era/gengou}/scroll.rb +44 -95
  13. data/lib/zakuro/calculation/{version → era/version}/internal/crawler.rb +7 -7
  14. data/lib/zakuro/calculation/{version → era/version}/internal/range.rb +4 -4
  15. data/lib/zakuro/calculation/{version → era/version}/version.rb +3 -3
  16. data/lib/zakuro/calculation/monthly/month.rb +2 -2
  17. data/lib/zakuro/calculation/range/full_range.rb +18 -19
  18. data/lib/zakuro/calculation/range/operated_range.rb +3 -4
  19. data/lib/zakuro/calculation/specifier/internal/month.rb +84 -0
  20. data/lib/zakuro/calculation/specifier/multiple_day.rb +162 -0
  21. data/lib/zakuro/calculation/specifier/single_day.rb +2 -2
  22. data/lib/zakuro/calculation/summary/internal/operation.rb +98 -0
  23. data/lib/zakuro/calculation/summary/range.rb +120 -0
  24. data/lib/zakuro/calculation/summary/single.rb +45 -67
  25. data/lib/zakuro/condition.rb +14 -0
  26. data/lib/zakuro/era/japan/gengou/alignment/aligner.rb +106 -0
  27. data/lib/zakuro/era/japan/gengou/alignment/division.rb +165 -0
  28. data/lib/zakuro/era/japan/gengou/alignment/line.rb +156 -0
  29. data/lib/zakuro/era/japan/gengou/alignment/linear_gengou.rb +184 -0
  30. data/lib/zakuro/era/japan/gengou/alignment.rb +42 -0
  31. data/lib/zakuro/era/japan/gengou/resource/parser.rb +242 -0
  32. data/lib/zakuro/era/japan/gengou/resource/type.rb +292 -0
  33. data/lib/zakuro/era/japan/gengou/resource/validator.rb +346 -0
  34. data/lib/zakuro/era/japan/gengou/{yaml → resource/yaml}/set-001-until-south.yaml +2 -2
  35. data/lib/zakuro/era/japan/gengou/{yaml → resource/yaml}/set-002-from-north.yaml +2 -2
  36. data/lib/zakuro/era/japan/gengou/{yaml → resource/yaml}/set-003-modern.yaml +2 -2
  37. data/lib/zakuro/era/japan/gengou/resource.rb +32 -0
  38. data/lib/zakuro/era/japan/gengou.rb +34 -73
  39. data/lib/zakuro/era/japan/version.rb +18 -18
  40. data/lib/zakuro/merchant.rb +41 -3
  41. data/lib/zakuro/operation/month/validator.rb +1 -1
  42. data/lib/zakuro/result/result.rb +17 -0
  43. data/lib/zakuro/version/senmyou/README.md +11 -7
  44. metadata +32 -20
  45. data/lib/zakuro/calculation/gengou/internal/counter.rb +0 -129
  46. data/lib/zakuro/calculation/gengou/internal/reserve/interval.rb +0 -183
  47. data/lib/zakuro/calculation/gengou/internal/reserve/list.rb +0 -382
  48. data/lib/zakuro/era/japan/gengou/parser.rb +0 -237
  49. data/lib/zakuro/era/japan/gengou/type.rb +0 -285
  50. data/lib/zakuro/era/japan/gengou/validator.rb +0 -341
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 567799c25255c97ec707d421b465358c1209afeef0d8c2f1c84a9b3bd5e65148
4
- data.tar.gz: bff172488e9ea94a8da83d09fa1dc9bd62a28de871ad0cc41217d20ff5dc2b94
3
+ metadata.gz: b3e4e967b105b46ffc94cc792328a5020aa0535f797dbf5bc5bc1a8eb2786cd8
4
+ data.tar.gz: 71f0fce3e9592469062df9ef667b2e146754c79bde93647a020c942b21cf3fb1
5
5
  SHA512:
6
- metadata.gz: daf11f9f9319d23fbba463571cb8f76184aeb7b6133fcde661bd06773113f37bc9684e8680a848c4998a96ea287f371ca123f878a7bb5dbdfb2ed499c434f8a7
7
- data.tar.gz: 3624066b34b4271961793f58bbad9509b046a3df967df4513de195352237386ccd5848b9b5f72c8838f6efb944fac1129b494001c570aa2cb5a93440d089e173
6
+ metadata.gz: d4acc827cebcebad50f33b3c255a23b28ebab62b05fc322ec0e304fa7d738df9479f3aded5e776e6d741d31f2c8812a30bde7ee777f3841eb140be3f44479ce8
7
+ data.tar.gz: c3a8304e0afdd13b2bc31d7107594816d185114bd7b67701bc3d2f1a395cc7dfeb7c19ff653febe57fc654f8f0feaac0857a0fa2ba2db6aba5d48f2ae87231f8
data/README.md CHANGED
@@ -30,7 +30,9 @@ Or install it yourself as:
30
30
 
31
31
  開発途中です。
32
32
 
33
- 下記表で対応済とした暦の1日検索のみ対応しております。
33
+ 1日検索/期間検索に対応しております。
34
+
35
+ 下記表で対応済の暦は使用可能です。
34
36
 
35
37
  |開始日|暦 |計算方法|対応|
36
38
  |:----|:----|:----|:----|
@@ -71,6 +73,11 @@ western_date = Date.new(1685, 2, 3)
71
73
  merchant.offer(condition: { date: western_date })
72
74
  puts merchant.commit.to_json
73
75
  # => {"data":{"year":{"first_gengou":{"name":"貞享","number":1},"second_gengou":{"name":"","number":-1},"zodiac_name":"甲子","total_days":354},"month":{"number":12,"leaped":false,"days_name":"大","first_day":{"number":1,"zodiac_name":"壬辰","remainder":"28-4182","western_date":"1685-01-05"},"odd_solar_terms":[{"index":1,"remainder":"30-890"}],"even_solar_terms":[{"index":2,"remainder":"45-2726"}]},"day":{"number":30,"zodiac_name":"辛酉","remainder":"57-4182","western_date":"1685-02-03"}},"operation":{"operated":false,"month":{"current":{"id":"","western_date":"","page":-1,"number":-1,"annotations":[]},"parent":{"id":"","western_date":"","page":-1,"number":-1,"annotations":[]}},"original":{"year":{"first_gengou":{"name":"貞享","number":1},"second_gengou":{"name":"","number":-1},"zodiac_name":"甲子","total_days":354},"month":{"number":12,"leaped":false,"days_name":"大","first_day":{"number":1,"zodiac_name":"壬辰","remainder":"28-4182","western_date":"1685-01-05"},"odd_solar_terms":[{"index":1,"remainder":"30-890"}],"even_solar_terms":[{"index":2,"remainder":"45-2726"}]},"day":{"number":30,"zodiac_name":"辛酉","remainder":"57-4182","western_date":"1685-02-03"}}}}
76
+
77
+ # 期間検索
78
+ merchant = Zakuro::Merchant.new(condition: { range: {start: Date.new(862, 2, 3), last: Date.new(862, 2, 4)}})
79
+ puts merchant.commit.to_json
80
+ {"list":[{"data":{"year":{"first_gengou":{"name":"貞観","number":4},"second_gengou":{"name":"","number":-1},"zodiac_name":"壬午","total_days":354},"month":{"number":1,"leaped":false,"days_name":"大","first_day":{"number":1,"zodiac_name":"庚午","remainder":"6-1282","western_date":"0862-02-03"},"odd_solar_terms":[{"index":5,"remainder":"34-5368"}],"even_solar_terms":[{"index":4,"remainder":"19-3532"}]},"day":{"number":1,"zodiac_name":"庚午","remainder":"6-1282","western_date":"0862-02-03"}},"operation":{"operated":false,"month":{"current":{"id":"","western_date":"","page":-1,"number":-1,"annotations":[]},"parent":{"id":"","western_date":"","page":-1,"number":-1,"annotations":[]}},"original":{"year":{"first_gengou":{"name":"貞観","number":4},"second_gengou":{"name":"","number":-1},"zodiac_name":"壬午","total_days":354},"month":{"number":1,"leaped":false,"days_name":"大","first_day":{"number":1,"zodiac_name":"庚午","remainder":"6-1282","western_date":"0862-02-03"},"odd_solar_terms":[{"index":5,"remainder":"34-5368"}],"even_solar_terms":[{"index":4,"remainder":"19-3532"}]},"day":{"number":1,"zodiac_name":"庚午","remainder":"6-1282","western_date":"0862-02-03"}}}},{"data":{"year":{"first_gengou":{"name":"貞観","number":4},"second_gengou":{"name":"","number":-1},"zodiac_name":"壬午","total_days":354},"month":{"number":1,"leaped":false,"days_name":"大","first_day":{"number":1,"zodiac_name":"庚午","remainder":"6-1282","western_date":"0862-02-03"},"odd_solar_terms":[{"index":5,"remainder":"34-5368"}],"even_solar_terms":[{"index":4,"remainder":"19-3532"}]},"day":{"number":2,"zodiac_name":"辛未","remainder":"7-1282","western_date":"0862-02-04"}},"operation":{"operated":false,"month":{"current":{"id":"","western_date":"","page":-1,"number":-1,"annotations":[]},"parent":{"id":"","western_date":"","page":-1,"number":-1,"annotations":[]}},"original":{"year":{"first_gengou":{"name":"貞観","number":4},"second_gengou":{"name":"","number":-1},"zodiac_name":"壬午","total_days":354},"month":{"number":1,"leaped":false,"days_name":"大","first_day":{"number":1,"zodiac_name":"庚午","remainder":"6-1282","western_date":"0862-02-03"},"odd_solar_terms":[{"index":5,"remainder":"34-5368"}],"even_solar_terms":[{"index":4,"remainder":"19-3532"}]},"day":{"number":2,"zodiac_name":"辛未","remainder":"7-1282","western_date":"0862-02-04"}}}}]}
74
81
  ```
75
82
 
76
83
  # 条件
@@ -119,6 +126,12 @@ puts merchant.commit.to_json
119
126
  | 日付情報 | data | | | Zakuro::Result::Data::SingleDay | - | 運用値(計算値は運用情報内を参照のこと)|
120
127
  | 運用情報 | operation | | | Zakuro::Result::Operation::Bundle | - | - |
121
128
 
129
+ ### Zakuro::Result::Range
130
+
131
+ | 項目名 | キー名 | - | - | データ型 | 参考値 | 備考   |
132
+ |----------|-----------|---|---|---------------------------------------|--------|---------------------------------------|
133
+ | 日リスト | list | | | Array\<Zakuro::Result::Data::Single\> | - | 範囲内の日付情報すべて |
134
+
122
135
  ### Zakuro::Result::SingleDay
123
136
 
124
137
  | 項目名 | キー名 | - | - | データ型 | 参考値 | 備考 |
@@ -176,8 +189,8 @@ puts merchant.commit.to_json
176
189
  | 月初日の西暦日 | | western_date |   | String | 0937-02-13 | 計算値 |
177
190
  | 原文頁数 | | page |   | Integer | 178 |   |
178
191
  | 原文注釈番号 | | number |   | Integer | 1 |   |
179
- | 注釈 | | annotations |   | Array<Zakuro::Result::Operation::::MonthAnnotation> |   |   |
180
- | 注釈内容 |   | | description | String | 計算は51乙卯であるが, 日本紀略に甲寅朔とある。正月甲寅朔のユリウス暦日は2月13日となる。(元旦日食 をさけるための変更か) |   |
192
+ | 注釈 | | annotations |   | Array\<Zakuro::Result::Operation::MonthAnnotation\> |   |   |
193
+ | 注釈内容 |   | | description | String | 計算は51乙卯であるが, 日本紀略に甲寅朔とある。<br>正月甲寅朔のユリウス暦日は2月13日となる。(元旦日食 をさけるための変更か) |   |
181
194
  | 注釈補記 |   | | note | String | - | 原文訂正 |
182
195
 
183
196
 
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.6.0
1
+ 0.6.1
@@ -15,7 +15,7 @@ module Zakuro
15
15
  # @return [Western::Calendar] 開始日
16
16
  attr_reader :start_date
17
17
  # @return [Western::Calendar] 終了日
18
- attr_reader :end_date
18
+ attr_reader :last_date
19
19
  # @return [Array<LinearGengou>] 1行目元号
20
20
  attr_reader :first_line
21
21
  # @return [Array<LinearGengou>] 2行目元号
@@ -25,14 +25,14 @@ module Zakuro
25
25
  # 初期化
26
26
  #
27
27
  # @param [Western::Calendar] start_date 開始日
28
- # @param [Western::Calendar] end_date 終了日
28
+ # @param [Western::Calendar] last_date 終了日
29
29
  # @param [Array<LinearGengou>] first_line 1行目元号
30
30
  # @param [Array<LinearGengou>] second_line 2行目元号
31
31
  #
32
- def initialize(start_date: Western::Calendar.new, end_date: Western::Calendar.new,
32
+ def initialize(start_date: Western::Calendar.new, last_date: Western::Calendar.new,
33
33
  first_line: [], second_line: [])
34
34
  @start_date = start_date
35
- @end_date = end_date
35
+ @last_date = last_date
36
36
  @first_line = first_line
37
37
  @second_line = second_line
38
38
  end
@@ -17,7 +17,7 @@ module Zakuro
17
17
  # @return [Western::Calendar] 開始日
18
18
  attr_reader :start_date
19
19
  # @return [Western::Calendar] 終了日
20
- attr_reader :end_date
20
+ attr_reader :last_date
21
21
  # @return [String] 元号名
22
22
  attr_reader :name
23
23
  # @return [Integer] 年
@@ -27,14 +27,14 @@ module Zakuro
27
27
  # 初期化
28
28
  #
29
29
  # @param [Western::Calendar] start_date 開始日
30
- # @param [Western::Calendar] end_date 終了日
30
+ # @param [Western::Calendar] last_date 終了日
31
31
  # @param [String] name 元号名
32
32
  # @param [Integer] 元号年
33
33
  #
34
- def initialize(start_date: Western::Calendar.new, end_date: Western::Calendar.new,
34
+ def initialize(start_date: Western::Calendar.new, last_date: Western::Calendar.new,
35
35
  name: '', year: INVALID_YEAR)
36
36
  @start_date = start_date
37
- @end_date = end_date
37
+ @last_date = last_date
38
38
  @name = name
39
39
  @year = year
40
40
  end
@@ -62,11 +62,11 @@ module Zakuro
62
62
 
63
63
  return false if @start_date.invalid?
64
64
 
65
- return false if @end_date.invalid?
65
+ return false if @last_date.invalid?
66
66
 
67
67
  return false if date < @start_date
68
68
 
69
- return false if date > @end_date
69
+ return false if date > @last_date
70
70
 
71
71
  true
72
72
  end
@@ -0,0 +1,107 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative '../../../../era/japan/gengou/resource'
4
+ require_relative '../../../../era/japan/calendar'
5
+ require_relative '../../../../era/western/calendar'
6
+
7
+ # :nodoc:
8
+ module Zakuro
9
+ # :nodoc:
10
+ module Calculation
11
+ # :nodoc:
12
+ module Gengou
13
+ #
14
+ # Connector 元号接続器
15
+ #
16
+ # 現在は"明徳"のみを解決している
17
+ # 元中1年4月28日 元中9年閏10月4日 明徳5年7月4日
18
+ # 1384-05-18 1392-11-18 1394-8-1
19
+ # [1] |-------------元中---------------|---------明徳------------|
20
+ # [2] |---------明徳--------|
21
+ # 明徳1年3月26日
22
+ # 1390-04-12
23
+ #
24
+ # * 明徳は2行目元号から始まる
25
+ # * 1392-11-19より1行目元号に移行する
26
+ # * 移行のタイミングで元号年を引き継げるようにする
27
+ #
28
+ class Connector
29
+ # @return [Array<Counter>] 未解決元号
30
+ attr_reader :unsolved_list
31
+
32
+ #
33
+ # 初期化
34
+ #
35
+ def initialize
36
+ @unsolved_list = []
37
+ end
38
+
39
+ #
40
+ # 元号を更新する
41
+ #
42
+ # @param [Array<Array<Counter>>] lines 全行元号
43
+ #
44
+ def update(lines: [])
45
+ lines.each do |line|
46
+ line.each_with_index do |gengou, index|
47
+ next unless gengou.changed?
48
+
49
+ line[index] = replace(gengou: gengou)
50
+ end
51
+ end
52
+ end
53
+
54
+ #
55
+ # 元号を年を更新した元号に入れ替える
56
+ #
57
+ # @param [Counter] gengou 元号
58
+ #
59
+ # @return [Counter] 元号/更新済元号
60
+ #
61
+ def replace(gengou:)
62
+ matched_index = -1
63
+ @unsolved_list.each_with_index do |unsolved, index|
64
+ next unless unsolved.name == gengou.name
65
+
66
+ matched_index = index
67
+ break
68
+ end
69
+
70
+ if matched_index == -1
71
+ @unsolved_list.push(gengou)
72
+ return gengou
73
+ end
74
+
75
+ matched = @unsolved_list[matched_index]
76
+
77
+ result = recreate(gengou: gengou, unsolved: matched)
78
+
79
+ @unsolved_list[matched_index] = result
80
+
81
+ # 分離した元号の末尾まで到達した
82
+ @unsolved_list.delete_at(matched_index) unless result.change_last_date?
83
+
84
+ result
85
+ end
86
+
87
+ #
88
+ # 年を更新した元号を生成する
89
+ #
90
+ # @param [Counter] gengou 元号
91
+ # @param [Counter] unsolved 未解決元号
92
+ #
93
+ # @return [Counter] 更新済元号
94
+ #
95
+ def recreate(gengou:, unsolved:)
96
+ japan_year = gengou.japan_year
97
+ japan_year = unsolved.japan_year if unsolved.japan_year > gengou.japan_year
98
+
99
+ Counter.new(
100
+ gengou: gengou.gengou, start_date: gengou.start_date,
101
+ last_date: gengou.last_date, japan_year: japan_year
102
+ )
103
+ end
104
+ end
105
+ end
106
+ end
107
+ end
@@ -0,0 +1,214 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative '../../../../era/japan/gengou/resource'
4
+ require_relative '../../../../era/japan/calendar'
5
+ require_relative '../../../../era/western/calendar'
6
+
7
+ # :nodoc:
8
+ module Zakuro
9
+ # :nodoc:
10
+ module Calculation
11
+ # :nodoc:
12
+ module Gengou
13
+ #
14
+ # Counter 加算元号
15
+ #
16
+ class Counter
17
+ # @return [Integer] 不正値
18
+ INVALID_YEAR = -1
19
+
20
+ # @return [Japan::Resource::Gengou] 元号
21
+ attr_reader :gengou
22
+ # @return [Integer] 元号年
23
+ attr_reader :japan_year
24
+ # @return [Integer] 西暦年
25
+ attr_reader :western_year
26
+ # @return [Western::Calendar] 西暦開始年
27
+ attr_reader :start_date
28
+ # @return [Western::Calendar] 西暦終了年
29
+ attr_reader :last_date
30
+
31
+ #
32
+ # 初期化
33
+ #
34
+ # @param [Japan::Resource::Gengou] gengou 元号
35
+ # @param [Western::Calendar] start_date 西暦開始年
36
+ # @param [Western::Calendar] last_date 西暦終了年
37
+ # @param [Integer] japan_year 和暦年
38
+ #
39
+ def initialize(gengou: Japan::Resource::Gengou.new,
40
+ start_date: Western::Calendar.new, last_date: Western::Calendar.new,
41
+ japan_year: INVALID_YEAR)
42
+ @gengou = gengou
43
+ @japan_year = japan_year
44
+ @japan_year = gengou.both_start_year.japan if @japan_year == INVALID_YEAR
45
+ @western_year = gengou.both_start_year.western
46
+
47
+ @start_date = start_date.clone
48
+ @last_date = last_date.clone
49
+
50
+ select_valid_date
51
+ end
52
+
53
+ #
54
+ # 和暦開始日を取得する
55
+ #
56
+ # @return [Japan::Calendar] 和暦開始日
57
+ #
58
+ def japan_start_date
59
+ return Japan::Calendar.new if @gengou.invalid?
60
+
61
+ @gengou.both_start_date.japan
62
+ end
63
+
64
+ #
65
+ # 西暦開始日を取得する
66
+ #
67
+ # @return [Western::Calendar] 西暦開始日
68
+ #
69
+ def western_start_date
70
+ return Western::Calendar.new if @gengou.invalid?
71
+
72
+ @start_date
73
+ end
74
+
75
+ #
76
+ # 西暦終了日を取得する
77
+ #
78
+ # @return [Western::Calendar] 西暦終了日
79
+ #
80
+ def western_last_date
81
+ return Western::Calendar.new if @gengou.invalid?
82
+
83
+ @last_date
84
+ end
85
+
86
+ #
87
+ # 次年にする
88
+ #
89
+ # @return [MultiGengou] 自身
90
+ #
91
+ def next_year
92
+ @japan_year += 1
93
+ @western_year += 1
94
+
95
+ self
96
+ end
97
+
98
+ #
99
+ # 元号名を取得する
100
+ #
101
+ # @return [String] 元号名
102
+ #
103
+ def name
104
+ return '' unless @gengou
105
+
106
+ @gengou.name
107
+ end
108
+
109
+ #
110
+ # 不正か
111
+ #
112
+ # @return [True] 不正
113
+ # @return [False] 不正なし
114
+ #
115
+ def invalid?
116
+ @gengou.invalid? || @japan_year == INVALID_YEAR || @western_year == INVALID_YEAR
117
+ end
118
+
119
+ #
120
+ # 指定した日が元号に含まれるか
121
+ #
122
+ # @param [Western::Calendar] date 日
123
+ #
124
+ # @return [True] 含まれる
125
+ # @return [False] 含まれない
126
+ #
127
+ def include?
128
+ @gengou.include?
129
+ end
130
+
131
+ #
132
+ # 元は1繋ぎであった元号が別の行に存在するか(設定値から変更されているか)?
133
+ #
134
+ # @return [True] 存在する
135
+ # @return [False] 存在しない
136
+ #
137
+ def changed?
138
+ return true if change_start_date?
139
+
140
+ return true if change_last_date?
141
+
142
+ false
143
+ end
144
+
145
+ #
146
+ # 開始日が設定された開始日と異なるか(行が変更されているか)
147
+ #
148
+ # @return [True] 異なる
149
+ # @return [False] 同一
150
+ #
151
+ def change_start_date?
152
+ return false if invalid?
153
+
154
+ @start_date != native_start_date
155
+ end
156
+
157
+ #
158
+ # 終了日が設定された終了日と異なるか(行が変更されているか)
159
+ #
160
+ # @return [True] 異なる
161
+ # @return [False] 同一
162
+ #
163
+ def change_last_date?
164
+ return false if invalid?
165
+
166
+ @last_date != native_last_date
167
+ end
168
+
169
+ #
170
+ # ディープコピー
171
+ #
172
+ # @param [MultiGengou] obj 自身
173
+ #
174
+ def initialize_copy(obj)
175
+ @gengou = obj.gengou.clone
176
+ @japan_year = obj.japan_year
177
+ @western_year = obj.western_year
178
+ @start_date = obj.start_date.clone
179
+ @last_date = obj.last_date.clone
180
+ end
181
+
182
+ private
183
+
184
+ #
185
+ # 有効な日付範囲を選択する
186
+ #
187
+ def select_valid_date
188
+ return if @gengou.invalid?
189
+
190
+ @start_date = @gengou.both_start_date.western.clone if @start_date.invalid?
191
+ @last_date = @gengou.last_date.clone if @last_date.invalid?
192
+ end
193
+
194
+ #
195
+ # 設定された元号の開始日を取得する
196
+ #
197
+ # @return [Western::Calendar]設定された元号の開始日
198
+ #
199
+ def native_start_date
200
+ @gengou.both_start_date.western
201
+ end
202
+
203
+ #
204
+ # 設定された元号の終了日を取得する
205
+ #
206
+ # @return [Western::Calendar] 設定された元号の終了日
207
+ #
208
+ def native_last_date
209
+ @gengou.last_date
210
+ end
211
+ end
212
+ end
213
+ end
214
+ end
@@ -0,0 +1,104 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative '../../../../era/western/calendar'
4
+ require_relative '../../../base/linear_gengou'
5
+
6
+ # :nodoc:
7
+ module Zakuro
8
+ # :nodoc:
9
+ module Calculation
10
+ # :nodoc:
11
+ module Gengou
12
+ # Publisher
13
+ #
14
+ # 元号発行
15
+ #
16
+ module Publisher
17
+ #
18
+ # 発行する
19
+ #
20
+ # @param [Western::Calendar] start_date 西暦開始日
21
+ # @param [Western::Calendar] last_date 西暦終了日
22
+ # @param [Array<Counter>] first_gengou 1行目元号
23
+ # @param [Array<Counter>] second_gengou 2行目元号
24
+ #
25
+ # @return [Base::Gengou] 元号
26
+ #
27
+ def self.run(start_date: Western::Calendar.new, last_date: Western::Calendar.new,
28
+ first_gengou: [], second_gengou: [])
29
+
30
+ Base::Gengou.new(
31
+ start_date: start_date,
32
+ last_date: last_date,
33
+ first_line: to_linear_gengou(
34
+ start_date: start_date, last_date: last_date, gengou_list: first_gengou
35
+ ),
36
+ second_line: to_linear_gengou(
37
+ start_date: start_date, last_date: last_date, gengou_list: second_gengou
38
+ )
39
+ )
40
+ end
41
+
42
+ #
43
+ # 直列元号に変換する
44
+ #
45
+ # * 最初の元号:開始日~その元号の終了日
46
+ # * 中間の元号:その元号の開始日~その元号の終了日
47
+ # * 最後の元号:その元号の開始日~終了日
48
+ #
49
+ # @param [Western::Calendar] start_date 西暦開始日
50
+ # @param [Western::Calendar] last_date 西暦終了日
51
+ # @param [Array<Counter>] gengou_list 元号リスト
52
+ #
53
+ # @return [Array<Base::LinearGengou>] 元号リスト
54
+ #
55
+ def self.to_linear_gengou(start_date:, last_date:, gengou_list: [])
56
+ return [] if gengou_list.size.zero?
57
+
58
+ result = []
59
+
60
+ gengou_list.each do |gengou|
61
+ if gengou.invalid?
62
+ # 無効元号は無効のままにする
63
+ result.push(Base::LinearGengou.new)
64
+ next
65
+ end
66
+
67
+ linear_gengou = to_limited_linear_gengou(
68
+ start_date: start_date,
69
+ last_date: last_date,
70
+ gengou: gengou
71
+ )
72
+ result.push(linear_gengou)
73
+ end
74
+
75
+ result
76
+ end
77
+
78
+ #
79
+ # 範囲を限定した直列元号に変換する
80
+ #
81
+ # * 開始日・終了日により範囲を狭める
82
+ #
83
+ # @param [Western::Calendar] start_date 西暦開始日
84
+ # @param [Western::Calendar] last_date 西暦終了日
85
+ # @param [Counter] gengou 加算元号
86
+ #
87
+ # @return [Base::LinearGengou] 元号
88
+ #
89
+ def self.to_limited_linear_gengou(start_date:, last_date:, gengou:)
90
+ gengou_start_date = gengou.western_start_date.clone
91
+ gengou_last_date = gengou.western_last_date.clone
92
+
93
+ gengou_start_date = start_date.clone if start_date > gengou_start_date
94
+ gengou_last_date = last_date.clone if last_date < gengou_last_date
95
+
96
+ Base::LinearGengou.new(
97
+ start_date: gengou_start_date, last_date: gengou_last_date,
98
+ name: gengou.name, year: gengou.japan_year
99
+ )
100
+ end
101
+ end
102
+ end
103
+ end
104
+ end