zakuro 0.6.1 → 0.7.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (80) hide show
  1. checksums.yaml +4 -4
  2. data/Makefile +3 -0
  3. data/README.md +6 -4
  4. data/VERSION +1 -1
  5. data/doc/operation/transfer.rb +8 -8
  6. data/lib/zakuro/calculation/base/gengou.rb +46 -0
  7. data/lib/zakuro/calculation/era/gengou/{scroll.rb → abstract_scroll.rb} +6 -8
  8. data/lib/zakuro/calculation/era/gengou/dated_scroll.rb +30 -0
  9. data/lib/zakuro/calculation/era/gengou/internal/reserve/{list.rb → abstract_list.rb} +37 -51
  10. data/lib/zakuro/calculation/era/gengou/internal/reserve/{range.rb → abstract_range.rb} +17 -17
  11. data/lib/zakuro/calculation/era/gengou/internal/reserve/dated_list.rb +48 -0
  12. data/lib/zakuro/calculation/era/gengou/internal/reserve/dated_range.rb +30 -0
  13. data/lib/zakuro/calculation/era/gengou/internal/reserve/empty_link.rb +158 -0
  14. data/lib/zakuro/calculation/era/gengou/internal/reserve/named_list.rb +125 -0
  15. data/lib/zakuro/calculation/era/gengou/internal/reserve/named_range.rb +75 -0
  16. data/lib/zakuro/calculation/era/gengou/internal/reserve.rb +2 -3
  17. data/lib/zakuro/calculation/era/gengou/named_scroll.rb +30 -0
  18. data/lib/zakuro/calculation/monthly/month.rb +39 -0
  19. data/lib/zakuro/calculation/monthly/operated_month.rb +1 -1
  20. data/lib/zakuro/calculation/range/{full_range.rb → abstract_full_range.rb} +5 -4
  21. data/lib/zakuro/calculation/range/{operated_range.rb → abstract_operation_range.rb} +9 -11
  22. data/lib/zakuro/calculation/range/dated_full_range.rb +32 -0
  23. data/lib/zakuro/calculation/range/dated_operation_range.rb +33 -0
  24. data/lib/zakuro/calculation/range/named_full_range.rb +38 -0
  25. data/lib/zakuro/calculation/range/named_operation_range.rb +36 -0
  26. data/lib/zakuro/calculation/range/operated_solar_terms.rb +0 -2
  27. data/lib/zakuro/calculation/{specifier → summary}/internal/month.rb +1 -1
  28. data/lib/zakuro/calculation/summary/japan/range.rb +152 -0
  29. data/lib/zakuro/calculation/summary/japan/single.rb +118 -0
  30. data/lib/zakuro/calculation/summary/japan/specifier/single_day.rb +91 -0
  31. data/lib/zakuro/calculation/summary/western/range.rb +124 -0
  32. data/lib/zakuro/calculation/summary/western/single.rb +113 -0
  33. data/lib/zakuro/calculation/summary/western/specifier/multiple_day.rb +169 -0
  34. data/lib/zakuro/calculation/summary/western/specifier/single_day.rb +93 -0
  35. data/lib/zakuro/condition.rb +1 -2
  36. data/lib/zakuro/era/japan/gengou/alignment/aligner.rb +89 -74
  37. data/lib/zakuro/era/japan/gengou/alignment/division.rb +125 -122
  38. data/lib/zakuro/era/japan/gengou/alignment/line.rb +141 -122
  39. data/lib/zakuro/era/japan/gengou/alignment/linear_gengou.rb +171 -168
  40. data/lib/zakuro/era/japan/gengou/alignment.rb +38 -25
  41. data/lib/zakuro/era/japan/gengou/resource/parser.rb +183 -182
  42. data/lib/zakuro/era/japan/gengou/resource/type.rb +233 -232
  43. data/lib/zakuro/era/japan/gengou/resource/validator.rb +255 -254
  44. data/lib/zakuro/era/japan/gengou/resource.rb +23 -22
  45. data/lib/zakuro/era/japan/gengou.rb +5 -20
  46. data/lib/zakuro/era/western/calendar.rb +9 -9
  47. data/lib/zakuro/exception/case/pattern.rb +35 -0
  48. data/lib/zakuro/exception/case/preset.rb +51 -0
  49. data/lib/zakuro/exception/case/template.rb +48 -0
  50. data/lib/zakuro/exception/cause.rb +28 -0
  51. data/lib/zakuro/exception/exception.rb +37 -0
  52. data/lib/zakuro/exception/zakuro_error.rb +28 -0
  53. data/lib/zakuro/gateway/locale/date.rb +127 -0
  54. data/lib/zakuro/gateway/locale/range.rb +67 -0
  55. data/lib/zakuro/gateway/range.rb +99 -0
  56. data/lib/zakuro/gateway/single.rb +70 -0
  57. data/lib/zakuro/merchant.rb +8 -44
  58. data/lib/zakuro/operation/month/parser.rb +9 -9
  59. data/lib/zakuro/operation/month/type.rb +31 -31
  60. data/lib/zakuro/operation/month/validator.rb +49 -49
  61. data/lib/zakuro/operation/yaml/month.yaml +3736 -3736
  62. data/lib/zakuro/output/logger.rb +17 -3
  63. data/lib/zakuro/result/data/day.rb +43 -0
  64. data/lib/zakuro/result/data/gengou.rb +35 -0
  65. data/lib/zakuro/result/data/month.rb +63 -0
  66. data/lib/zakuro/result/data/single_day.rb +48 -0
  67. data/lib/zakuro/result/data/solar_term.rb +35 -0
  68. data/lib/zakuro/result/data/year.rb +45 -0
  69. data/lib/zakuro/result/operation/month/annotation.rb +40 -0
  70. data/lib/zakuro/result/operation/month/bundle.rb +36 -0
  71. data/lib/zakuro/result/operation/month/history.rb +54 -0
  72. data/lib/zakuro/result/operation/operation.rb +44 -0
  73. data/lib/zakuro/result/result.rb +4 -2
  74. metadata +46 -14
  75. data/lib/zakuro/calculation/specifier/multiple_day.rb +0 -162
  76. data/lib/zakuro/calculation/specifier/single_day.rb +0 -87
  77. data/lib/zakuro/calculation/summary/range.rb +0 -120
  78. data/lib/zakuro/calculation/summary/single.rb +0 -110
  79. data/lib/zakuro/result/data.rb +0 -187
  80. data/lib/zakuro/result/operation.rb +0 -114
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: b3e4e967b105b46ffc94cc792328a5020aa0535f797dbf5bc5bc1a8eb2786cd8
4
- data.tar.gz: 71f0fce3e9592469062df9ef667b2e146754c79bde93647a020c942b21cf3fb1
3
+ metadata.gz: 294b07e6f6f82cad6ed7d5332d96a0c147ee1cbcbb842faa745918f1d8fbac2e
4
+ data.tar.gz: 41341f57aab606cea9a86c75aa4dbef8a8d247421d7428ddf5f1e442afc1b8db
5
5
  SHA512:
6
- metadata.gz: d4acc827cebcebad50f33b3c255a23b28ebab62b05fc322ec0e304fa7d738df9479f3aded5e776e6d741d31f2c8812a30bde7ee777f3841eb140be3f44479ce8
7
- data.tar.gz: c3a8304e0afdd13b2bc31d7107594816d185114bd7b67701bc3d2f1a395cc7dfeb7c19ff653febe57fc654f8f0feaac0857a0fa2ba2db6aba5d48f2ae87231f8
6
+ metadata.gz: 9f3aee8d972a13e41ae98cb6b600f2032e833380a845513b2a738bf045e7d36a719a2e31161fd90f23194bdee4e0a690dc931934ad30ab0940742f20ea0f7bc7
7
+ data.tar.gz: 8c8ea9c23b11c28910cbfcdad25dec4720f919a1bbb0551737f8cf82cc62cd59621dcdd4770580d81c7a4a1c8d8859765e3ae7cd39592369a25dd89e1da9932d
data/Makefile CHANGED
@@ -1,2 +1,5 @@
1
1
  binstub:
2
2
  bundle binstubs rspec-core --path exe
3
+
4
+ count:
5
+ find . -type f -name "*.rb" | xargs cat | sed "/^\s*\(#\|\$\)/d" | wc -l
data/README.md CHANGED
@@ -87,13 +87,15 @@ puts merchant.commit.to_json
87
87
  | 項目名 | キー名 | hash内キー名 | データ型 | 備考 |
88
88
  |----------------|---------|--------------|----------|--------------------|
89
89
  | 基準日 | date | | Date | 西暦日 |
90
- | | | | String | 和暦日 |
91
- | 範囲(開始日) | range | start | Date | |
92
- | 範囲(終了日) | | last | Date | |
90
+ | | | | String | 和暦日/西暦日 |
91
+ | 範囲(開始日) | range | start | Date | 西暦日 |
92
+ | | | | String | 和暦日/西暦日 |
93
+ | 範囲(終了日) | | last | Date | 西暦日 |
94
+ | | | | String | 和暦日/西暦日 |
93
95
  | 列 | columns | | Array | 取得したい列の列名 |
94
96
  | オプション | options | | Array | 取得オプション |
95
97
 
96
- 現時点では `date` の西暦日のみ対応中です。
98
+ 現時点では `date` と `range` のみ対応中です。
97
99
 
98
100
  ## オプション
99
101
 
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.6.1
1
+ 0.7.0
@@ -39,25 +39,25 @@ table.each do |row|
39
39
  'diffs' => {
40
40
  'month' => {
41
41
  'number' => {
42
- 'calc' => month_str_to_number(row['月(計)']),
43
- 'actual' => month_str_to_number(row['月(実)'])
42
+ 'src' => month_str_to_number(row['月(計)']),
43
+ 'dest' => month_str_to_number(row['月(実)'])
44
44
  },
45
45
  'leaped' => {
46
- 'calc' => month_str_to_leaped(row['月(計)']),
47
- 'actual' => month_str_to_leaped(row['月(実)'])
46
+ 'src' => month_str_to_leaped(row['月(計)']),
47
+ 'dest' => month_str_to_leaped(row['月(実)'])
48
48
  },
49
49
  'days' => {
50
- 'calc' => row['月大小(計)'],
51
- 'actual' => row['月大小(実)']
50
+ 'src' => row['月大小(計)'],
51
+ 'dest' => row['月大小(実)']
52
52
  }
53
53
  },
54
54
  'solar_term' => {
55
- 'calc' => {
55
+ 'src' => {
56
56
  'index' => row['中気番号(計)'],
57
57
  'to' => row['中気移動先'],
58
58
  'zodiac_name' => row['中気(計)']
59
59
  },
60
- 'actual' => {
60
+ 'dest' => {
61
61
  'index' => row['中気番号(実)'],
62
62
  'from' => row['中気移動元'],
63
63
  'zodiac_name' => row['中気(実)']
@@ -67,6 +67,52 @@ module Zakuro
67
67
  LinearGengou.new
68
68
  end
69
69
 
70
+ #
71
+ # 元号を取得する
72
+ #
73
+ # @param [String] name 元号名
74
+ #
75
+ # @return [LinearGengou] 元号
76
+ #
77
+ def match_by_name(name:)
78
+ result = match_first_line_by_name(name: name)
79
+ return result unless result.invalid?
80
+
81
+ result = match_second_line_by_name(name: name)
82
+
83
+ result
84
+ end
85
+
86
+ #
87
+ # 1行目元号を取得する
88
+ #
89
+ # @param [String] name 元号名
90
+ #
91
+ # @return [LinearGengou] 1行目元号
92
+ #
93
+ def match_first_line_by_name(name:)
94
+ @first_line.each do |line|
95
+ return line.clone if line.name == name
96
+ end
97
+
98
+ LinearGengou.new
99
+ end
100
+
101
+ #
102
+ # 2行目元号を取得する
103
+ #
104
+ # @param [String] name 元号名
105
+ #
106
+ # @return [LinearGengou] 2行目元号
107
+ #
108
+ def match_second_line_by_name(name:)
109
+ @second_line.each do |line|
110
+ return line.clone if line.name == name
111
+ end
112
+
113
+ LinearGengou.new
114
+ end
115
+
70
116
  #
71
117
  # 不正か
72
118
  #
@@ -3,7 +3,6 @@
3
3
  require_relative '../../../era/western/calendar'
4
4
  require_relative '../../base/gengou'
5
5
  require_relative '../../base/linear_gengou'
6
- require_relative './internal/reserve/range'
7
6
  require_relative './internal/connector'
8
7
  require_relative './internal/publisher'
9
8
 
@@ -13,16 +12,16 @@ module Zakuro
13
12
  module Calculation
14
13
  # :nodoc:
15
14
  module Gengou
16
- # Scroll
15
+ # AbstractScroll
17
16
  #
18
17
  # 元号スクロール
19
18
  #
20
- class Scroll
19
+ class AbstractScroll
21
20
  # @return [Western::Calendar] 月初日
22
21
  attr_reader :monthly_start_date
23
22
  # @return [Western::Calendar] 月末日
24
23
  attr_reader :monthly_last_date
25
- # @return [Reserve::Range] 予約範囲
24
+ # @return [Reserve::AbstractRange] 予約範囲
26
25
  attr_reader :range
27
26
  # @return [Array<Counte>] 1行目元号
28
27
  attr_reader :first_gengou
@@ -34,13 +33,12 @@ module Zakuro
34
33
  #
35
34
  # 初期化
36
35
  #
37
- # @param [Western::Calendar] start_date 西暦開始日(最大範囲)
38
- # @param [Western::Calendar] last_date 西暦終了日(最大範囲)
36
+ # @param [Reserve::AbstractRange] range 予約範囲
39
37
  #
40
- def initialize(start_date: Western::Calendar.new, last_date: Western::Calendar.new)
38
+ def initialize(range:)
41
39
  @monthly_start_date = Western::Calendar.new
42
40
  @monthly_last_date = Western::Calendar.new
43
- @range = Reserve::Range.new(start_date: start_date, last_date: last_date)
41
+ @range = range
44
42
  @first_gengou = []
45
43
  @second_gengou = []
46
44
  @ignited = false
@@ -0,0 +1,30 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative './abstract_scroll'
4
+ require_relative './internal/reserve/dated_range'
5
+
6
+ # :nodoc:
7
+ module Zakuro
8
+ # :nodoc:
9
+ module Calculation
10
+ # :nodoc:
11
+ module Gengou
12
+ # DatedScroll
13
+ #
14
+ # 元号スクロール
15
+ #
16
+ class DatedScroll < AbstractScroll
17
+ #
18
+ # 初期化
19
+ #
20
+ # @param [Western::Calendar] start_date 西暦開始日(最大範囲)
21
+ # @param [Western::Calendar] last_date 西暦終了日(最大範囲)
22
+ #
23
+ def initialize(start_date: Western::Calendar.new, last_date: Western::Calendar.new)
24
+ range = Reserve::DatedRange.new(start_date: start_date, last_date: last_date)
25
+ super(range: range)
26
+ end
27
+ end
28
+ end
29
+ end
30
+ end
@@ -1,4 +1,4 @@
1
- # frozen_string_literal: true
1
+ # frozen_string_literal: true
2
2
 
3
3
  require_relative '../../../../../era/japan/gengou/resource'
4
4
  require_relative '../../../../../era/japan/gengou'
@@ -7,6 +7,8 @@ require_relative '../../../../../era/western/calendar'
7
7
 
8
8
  require_relative '../counter'
9
9
 
10
+ require_relative './empty_link'
11
+
10
12
  # :nodoc:
11
13
  module Zakuro
12
14
  # :nodoc:
@@ -15,11 +17,11 @@ module Zakuro
15
17
  module Gengou
16
18
  # :nodoc:
17
19
  module Reserve
18
- # List
20
+ # AbstractList
19
21
  #
20
- # 予約元号一覧
22
+ # 抽象予約元号一覧
21
23
  #
22
- class List
24
+ class AbstractList
23
25
  # @return [Integer] 不正年
24
26
  INVALID_YEAR = -1
25
27
  # @return [Integer] 最大月日数
@@ -37,15 +39,15 @@ module Zakuro
37
39
  #
38
40
  # 初期化
39
41
  #
40
- # @param [True, False] first true:1行目元号, false:2行目元号
42
+ # @param [Integer] index n行目元号
41
43
  # @param [Western::Calendar] start_date 開始日
42
44
  # @param [Western::Calendar] last_date 終了日
43
45
  #
44
- def initialize(first: true, start_date: Western::Calendar.new,
46
+ def initialize(index:, start_date: Western::Calendar.new,
45
47
  last_date: Western::Calendar)
46
- @index = first ? Japan::Gengou::FIRST_LINE : Japan::Gengou::SECOND_LINE
48
+ @index = index
47
49
  @start_date = start_date.clone
48
- @last_date = last_date.invalid? ? start_date.clone : last_date.clone
50
+ @last_date = last_date.clone
49
51
  @list = []
50
52
 
51
53
  update
@@ -91,41 +93,7 @@ module Zakuro
91
93
  )
92
94
  end
93
95
 
94
- # TODO: refactor
95
- if result.size.zero?
96
- result.push(
97
- Gengou::Counter.new(
98
- gengou: Japan::Resource::Gengou.new(
99
- both_start_date: Japan::Resource::Both::Date.new(
100
- western: start_date.clone
101
- ),
102
- last_date: last_date.clone
103
- )
104
- )
105
- )
106
- return result
107
- end
108
-
109
- # FIXME: 有効元号の前後しか見ていない
110
- if start_date < result[0].start_date
111
- result.unshift(
112
- Gengou::Counter.new(
113
- gengou: Japan::Resource::Gengou.new,
114
- start_date: start_date.clone,
115
- last_date: result[0].start_date.clone - 1
116
- )
117
- )
118
- end
119
-
120
- if last_date > result[-1].last_date
121
- result.push(
122
- Gengou::Counter.new(
123
- gengou: Japan::Resource::Gengou.new,
124
- start_date: result[0].last_date.clone + 1,
125
- last_date: last_date.clone
126
- )
127
- )
128
- end
96
+ EmptyLink.fill(counters: result, start_date: start_date, last_date: last_date)
129
97
 
130
98
  result
131
99
  end
@@ -152,6 +120,17 @@ module Zakuro
152
120
  @list[0].gengou.both_start_date.western.clone
153
121
  end
154
122
 
123
+ #
124
+ # 西暦終了日を取得する
125
+ #
126
+ # @return [Western::Calendar] 西暦開始日
127
+ #
128
+ def western_last_date
129
+ return Western::Calendar.new if invalid?
130
+
131
+ @list[-1].gengou.last_date.clone
132
+ end
133
+
155
134
  #
156
135
  # 西暦開始年を取得する
157
136
  #
@@ -219,24 +198,31 @@ module Zakuro
219
198
  # 予約元号一覧を更新する
220
199
  #
221
200
  def update
222
- # 開始日の30日前に前の元号がある場合は、前の元号を設定する
223
- start_date = @start_date.clone - (MAX_MONTH_DAYS + 1)
224
- # 開始日の30日後に次の元号がある場合は、次の元号を設定する
225
- last_date = @last_date.clone + (MAX_MONTH_DAYS + 1)
226
-
227
- @list |= line(start_date: start_date, last_date: last_date)
201
+ # override
228
202
  end
229
203
 
230
204
  #
231
- # 元号
205
+ # 元号を取得する
232
206
  #
233
- # @param [Western::Calendar] date 日付
207
+ # @param [Western::Calendar] start_date 開始日
208
+ # @param [Western::Calendar] last_date 終了日
234
209
  #
235
210
  # @return [Array<Japan::Alignment::LinearGengou>] 元号
236
211
  #
237
212
  def line(start_date:, last_date:)
238
213
  Japan::Gengou.line(line: @index, start_date: start_date, last_date: last_date)
239
214
  end
215
+
216
+ #
217
+ # 行番号に変換する
218
+ #
219
+ # @param [True, False] first 1行目/2行目
220
+ #
221
+ # @return [<Integer] 行番号
222
+ #
223
+ def parse_index(first: true)
224
+ first ? Japan::Gengou::FIRST_LINE : Japan::Gengou::SECOND_LINE
225
+ end
240
226
  end
241
227
  end
242
228
  end
@@ -2,7 +2,7 @@
2
2
 
3
3
  require_relative '../../../../../era/western/calendar'
4
4
 
5
- require_relative './list'
5
+ require_relative './dated_list'
6
6
 
7
7
  # :nodoc:
8
8
  module Zakuro
@@ -13,9 +13,9 @@ module Zakuro
13
13
  # :nodoc:
14
14
  module Reserve
15
15
  #
16
- # Range 予約済み計算範囲
16
+ # AbstractRange 予約済み計算範囲
17
17
  #
18
- class Range
18
+ class AbstractRange
19
19
  # @return [List] 1行目元号
20
20
  attr_reader :first_list
21
21
  # @return [List] 2行目元号
@@ -30,8 +30,8 @@ module Zakuro
30
30
  def initialize(start_date: Western::Calendar.new, last_date: Western::Calendar.new)
31
31
  last_date = start_date.clone if last_date.invalid?
32
32
 
33
- @first_list = List.new(first: true, start_date: start_date, last_date: last_date)
34
- @second_list = List.new(first: false, start_date: start_date, last_date: last_date)
33
+ @first_list = DatedList.new(first: true, start_date: start_date, last_date: last_date)
34
+ @second_list = DatedList.new(first: false, start_date: start_date, last_date: last_date)
35
35
 
36
36
  renew(last_date: last_date)
37
37
  end
@@ -59,10 +59,10 @@ module Zakuro
59
59
 
60
60
  return if native_start_date.invalid?
61
61
 
62
- @first_list = List.new(first: true,
63
- start_date: native_start_date, last_date: last_date)
64
- @second_list = List.new(first: false,
65
- start_date: native_start_date, last_date: last_date)
62
+ @first_list = DatedList.new(first: true, start_date: native_start_date,
63
+ last_date: last_date)
64
+ @second_list = DatedList.new(first: false, start_date: native_start_date,
65
+ last_date: last_date)
66
66
  end
67
67
 
68
68
  #
@@ -107,11 +107,11 @@ module Zakuro
107
107
  # @return [Japan::Calendar] 和暦開始日
108
108
  #
109
109
  def japan_start_date
110
- gengou = oldest_gengou
110
+ list = oldest_list
111
111
 
112
- return Japan::Calendar.new if gengou.invalid?
112
+ return Japan::Calendar.new if list.invalid?
113
113
 
114
- gengou.japan_start_date
114
+ list.japan_start_date
115
115
  end
116
116
 
117
117
  #
@@ -120,11 +120,11 @@ module Zakuro
120
120
  # @return [Western::Calendar] 西暦開始日
121
121
  #
122
122
  def western_start_date
123
- gengou = oldest_gengou
123
+ list = oldest_list
124
124
 
125
- return Western::Calendar.new if gengou.invalid?
125
+ return Western::Calendar.new if list.invalid?
126
126
 
127
- gengou.western_start_date
127
+ list.western_start_date
128
128
  end
129
129
 
130
130
  #
@@ -168,9 +168,9 @@ module Zakuro
168
168
  #
169
169
  # 最古の元号を取得する
170
170
  #
171
- # @return [Japan::Gengou] 最古の元号
171
+ # @return [List] 最古の元号
172
172
  #
173
- def oldest_gengou
173
+ def oldest_list
174
174
  return @first_list if @first_list.invalid?
175
175
 
176
176
  return @first_list if @second_list.invalid?
@@ -0,0 +1,48 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative './abstract_list'
4
+
5
+ # :nodoc:
6
+ module Zakuro
7
+ # :nodoc:
8
+ module Calculation
9
+ # :nodoc:
10
+ module Gengou
11
+ # :nodoc:
12
+ module Reserve
13
+ # List
14
+ #
15
+ # 予約元号一覧
16
+ #
17
+ class DatedList < AbstractList
18
+ #
19
+ # 初期化
20
+ #
21
+ # @param [True, False] first true:1行目元号, false:2行目元号
22
+ # @param [Western::Calendar] start_date 開始日
23
+ # @param [Western::Calendar] last_date 終了日
24
+ #
25
+ def initialize(first: true, start_date: Western::Calendar.new,
26
+ last_date: Western::Calendar)
27
+ @index = parse_index(first: first)
28
+ @start_date = start_date.clone
29
+ @last_date = last_date.invalid? ? start_date.clone : last_date.clone
30
+ super(index: @index, start_date: start_date, last_date: last_date)
31
+ end
32
+
33
+ #
34
+ # 予約元号一覧を更新する
35
+ #
36
+ def update
37
+ # 開始日の30日前に前の元号がある場合は、前の元号を設定する
38
+ start_date = @start_date.clone - (MAX_MONTH_DAYS + 1)
39
+ # 開始日の30日後に次の元号がある場合は、次の元号を設定する
40
+ last_date = @last_date.clone + (MAX_MONTH_DAYS + 1)
41
+
42
+ @list |= line(start_date: start_date, last_date: last_date)
43
+ end
44
+ end
45
+ end
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,30 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative './abstract_range'
4
+
5
+ # :nodoc:
6
+ module Zakuro
7
+ # :nodoc:
8
+ module Calculation
9
+ # :nodoc:
10
+ module Gengou
11
+ # :nodoc:
12
+ module Reserve
13
+ #
14
+ # DatedRange 予約済み計算範囲
15
+ #
16
+ class DatedRange < AbstractRange
17
+ #
18
+ # 初期化
19
+ #
20
+ # @param [Western::Calendar] start_date 西暦開始日
21
+ # @param [Western::Calendar] last_date 西暦終了日
22
+ #
23
+ def initialize(start_date: Western::Calendar.new, last_date: Western::Calendar.new)
24
+ super(start_date: start_date, last_date: last_date)
25
+ end
26
+ end
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,158 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative '../../../../../era/japan/gengou/resource'
4
+ require_relative '../../../../../era/japan/gengou'
5
+ require_relative '../../../../../era/japan/calendar'
6
+ require_relative '../../../../../era/western/calendar'
7
+
8
+ require_relative '../counter'
9
+
10
+ # :nodoc:
11
+ module Zakuro
12
+ # :nodoc:
13
+ module Calculation
14
+ # :nodoc:
15
+ module Gengou
16
+ # :nodoc:
17
+ module Reserve
18
+ # EmptyLink
19
+ #
20
+ # 空元号連結
21
+ #
22
+ # * 開始日と終了日の間は常に元号で満たす必要がある
23
+ # * もし元号が存在しない期間があれば、空の元号を生成する
24
+ #
25
+ module EmptyLink
26
+ #
27
+ # 空元号で満たす
28
+ #
29
+ # @param [Array<Gengou::Counter>] counters 加算元号リスト
30
+ # @param [Western::Calendar] start_date 開始日
31
+ # @param [Western::Calendar] last_date 終了日
32
+ #
33
+ def self.fill(counters:, start_date: Western::Calendar.new,
34
+ last_date: Western::Calendar.new)
35
+ if counters.size.zero?
36
+ counters.push(
37
+ create_empty_counter(
38
+ start_date: start_date.clone,
39
+ last_date: last_date.clone
40
+ )
41
+ )
42
+ return counters
43
+ end
44
+
45
+ fill_both_ends(counters: counters, start_date: start_date, last_date: last_date)
46
+
47
+ fill_middle(counters: counters)
48
+ end
49
+
50
+ #
51
+ # リストの両端を空元号で満たす
52
+ #
53
+ # @param [Array<Gengou::Counter>] counters 加算元号リスト
54
+ # @param [Western::Calendar] start_date 開始日
55
+ # @param [Western::Calendar] last_date 終了日
56
+ #
57
+ def self.fill_both_ends(counters:, start_date: Western::Calendar.new,
58
+ last_date: Western::Calendar.new)
59
+ fill_by_start(counters: counters, start_date: start_date)
60
+
61
+ fill_by_last(counters: counters, last_date: last_date)
62
+ end
63
+ private_class_method :fill_both_ends
64
+
65
+ #
66
+ # 先頭要素の手前を空元号で満たす
67
+ #
68
+ # @param [Array<Gengou::Counter>] counters 加算元号リスト
69
+ # @param [Western::Calendar] start_date 開始日
70
+ #
71
+ def self.fill_by_start(counters:, start_date:)
72
+ return unless start_date < counters[0].start_date
73
+
74
+ counters.unshift(
75
+ create_empty_counter(
76
+ start_date: start_date.clone,
77
+ last_date: counters[0].start_date.clone - 1
78
+ )
79
+ )
80
+ end
81
+ private_class_method :fill_by_start
82
+
83
+ #
84
+ # 最終要素の手前を空元号で満たす
85
+ #
86
+ # @param [Array<Gengou::Counter>] counters 加算元号リスト
87
+ # @param [Western::Calendar] last_date 終了日
88
+ #
89
+ def self.fill_by_last(counters:, last_date:)
90
+ return unless last_date > counters[-1].last_date
91
+
92
+ counters.push(
93
+ create_empty_counter(
94
+ start_date: counters[-1].last_date.clone + 1,
95
+ last_date: last_date.clone
96
+ )
97
+ )
98
+ end
99
+ private_class_method :fill_by_last
100
+
101
+ #
102
+ # 元号間を空元号で満たす
103
+ #
104
+ # @param [Array<Gengou::Counter>] counters 加算元号リスト
105
+ #
106
+ def self.fill_middle(counters:)
107
+ return if counters.size.zero?
108
+
109
+ size = counters.size - 1
110
+ (0..size).reverse_each do |index|
111
+ break if index <= 0
112
+
113
+ insert(counters: counters, index: index)
114
+ end
115
+ end
116
+ private_class_method :fill_middle
117
+
118
+ #
119
+ # 対象要素に空元号を追加する
120
+ #
121
+ # @param [Array<Gengou::Counter>] counters 加算元号リスト
122
+ # @param [Integer] index 要素番号
123
+ #
124
+ def self.insert(counters:, index:)
125
+ before_last_date = counters[index - 1].last_date.clone
126
+ current_start_date = counters[index].start_date.clone
127
+
128
+ return if (before_last_date.clone + 1) == current_start_date
129
+
130
+ counters.insert(
131
+ index,
132
+ create_empty_counter(
133
+ start_date: before_last_date + 1, last_date: current_start_date - 1
134
+ )
135
+ )
136
+ end
137
+ private_class_method :insert
138
+
139
+ #
140
+ # 空元号を生成する
141
+ #
142
+ # @param [Western::Calendar] start_date 西暦開始日
143
+ # @param [Western::Calendar] last_date 西暦終了日
144
+ #
145
+ # @return [Gengou::Counter] 加算元号(空元号)
146
+ #
147
+ def self.create_empty_counter(start_date:, last_date:)
148
+ Gengou::Counter.new(
149
+ gengou: Japan::Gengou::Resource::Gengou.new,
150
+ start_date: start_date, last_date: last_date
151
+ )
152
+ end
153
+ private_class_method :create_empty_counter
154
+ end
155
+ end
156
+ end
157
+ end
158
+ end