zakuro 0.5.0 → 0.7.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (124) hide show
  1. checksums.yaml +4 -4
  2. data/Makefile +3 -0
  3. data/README.md +22 -7
  4. data/VERSION +1 -0
  5. data/doc/operation/transfer.rb +8 -8
  6. data/lib/zakuro/calculation/base/gengou.rb +128 -0
  7. data/lib/zakuro/calculation/base/linear_gengou.rb +76 -0
  8. data/lib/zakuro/calculation/base/operated_year.rb +5 -15
  9. data/lib/zakuro/calculation/base/year.rb +26 -14
  10. data/lib/zakuro/calculation/era/gengou/abstract_scroll.rb +209 -0
  11. data/lib/zakuro/calculation/era/gengou/dated_scroll.rb +30 -0
  12. data/lib/zakuro/calculation/era/gengou/internal/connector.rb +107 -0
  13. data/lib/zakuro/calculation/era/gengou/internal/counter.rb +214 -0
  14. data/lib/zakuro/calculation/era/gengou/internal/publisher.rb +104 -0
  15. data/lib/zakuro/calculation/era/gengou/internal/reserve/abstract_list.rb +230 -0
  16. data/lib/zakuro/calculation/era/gengou/internal/reserve/abstract_range.rb +193 -0
  17. data/lib/zakuro/calculation/era/gengou/internal/reserve/dated_list.rb +48 -0
  18. data/lib/zakuro/calculation/era/gengou/internal/reserve/dated_range.rb +30 -0
  19. data/lib/zakuro/calculation/era/gengou/internal/reserve/empty_link.rb +158 -0
  20. data/lib/zakuro/calculation/era/gengou/internal/reserve/named_list.rb +125 -0
  21. data/lib/zakuro/calculation/era/gengou/internal/reserve/named_range.rb +75 -0
  22. data/lib/zakuro/calculation/era/gengou/internal/reserve.rb +41 -0
  23. data/lib/zakuro/calculation/era/gengou/named_scroll.rb +30 -0
  24. data/lib/zakuro/calculation/era/version/internal/crawler.rb +51 -0
  25. data/lib/zakuro/calculation/era/version/internal/range.rb +39 -0
  26. data/lib/zakuro/calculation/era/version/version.rb +24 -0
  27. data/lib/zakuro/calculation/monthly/first_day.rb +3 -2
  28. data/lib/zakuro/calculation/monthly/month.rb +88 -2
  29. data/lib/zakuro/calculation/monthly/operated_month.rb +3 -3
  30. data/lib/zakuro/calculation/range/abstract_full_range.rb +172 -0
  31. data/lib/zakuro/calculation/range/{operated_range.rb → abstract_operation_range.rb} +17 -15
  32. data/lib/zakuro/calculation/range/dated_full_range.rb +32 -0
  33. data/lib/zakuro/calculation/range/dated_operation_range.rb +33 -0
  34. data/lib/zakuro/calculation/range/named_full_range.rb +38 -0
  35. data/lib/zakuro/calculation/range/named_operation_range.rb +36 -0
  36. data/lib/zakuro/calculation/range/operated_solar_terms.rb +36 -19
  37. data/lib/zakuro/calculation/range/transfer/gengou_scroller.rb +54 -0
  38. data/lib/zakuro/calculation/range/transfer/year_boundary.rb +25 -21
  39. data/lib/zakuro/calculation/summary/internal/month.rb +84 -0
  40. data/lib/zakuro/calculation/summary/{single.rb → internal/operation.rb} +6 -37
  41. data/lib/zakuro/calculation/summary/japan/range.rb +152 -0
  42. data/lib/zakuro/calculation/summary/japan/single.rb +118 -0
  43. data/lib/zakuro/calculation/summary/japan/specifier/single_day.rb +91 -0
  44. data/lib/zakuro/calculation/summary/western/range.rb +124 -0
  45. data/lib/zakuro/calculation/summary/western/single.rb +113 -0
  46. data/lib/zakuro/calculation/summary/western/specifier/multiple_day.rb +169 -0
  47. data/lib/zakuro/calculation/summary/western/specifier/single_day.rb +93 -0
  48. data/lib/zakuro/condition.rb +15 -2
  49. data/lib/zakuro/era/japan/calendar.rb +133 -0
  50. data/lib/zakuro/era/japan/gengou/alignment/aligner.rb +121 -0
  51. data/lib/zakuro/era/japan/gengou/alignment/division.rb +168 -0
  52. data/lib/zakuro/era/japan/gengou/alignment/line.rb +175 -0
  53. data/lib/zakuro/era/japan/gengou/alignment/linear_gengou.rb +187 -0
  54. data/lib/zakuro/era/japan/gengou/alignment.rb +55 -0
  55. data/lib/zakuro/era/japan/gengou/resource/parser.rb +243 -0
  56. data/lib/zakuro/era/japan/gengou/resource/type.rb +293 -0
  57. data/lib/zakuro/era/japan/gengou/resource/validator.rb +347 -0
  58. data/lib/zakuro/era/japan/gengou/resource/yaml/set-001-until-south.yaml +1870 -0
  59. data/lib/zakuro/era/japan/gengou/resource/yaml/set-002-from-north.yaml +810 -0
  60. data/lib/zakuro/era/japan/gengou/resource/yaml/set-003-modern.yaml +50 -0
  61. data/lib/zakuro/era/japan/gengou/resource.rb +33 -0
  62. data/lib/zakuro/era/japan/gengou.rb +26 -80
  63. data/lib/zakuro/era/japan/version.rb +151 -0
  64. data/lib/zakuro/era/{western.rb → western/calendar.rb} +9 -9
  65. data/lib/zakuro/exception/case/pattern.rb +35 -0
  66. data/lib/zakuro/exception/case/preset.rb +51 -0
  67. data/lib/zakuro/exception/case/template.rb +48 -0
  68. data/lib/zakuro/exception/cause.rb +28 -0
  69. data/lib/zakuro/exception/exception.rb +37 -0
  70. data/lib/zakuro/exception/zakuro_error.rb +28 -0
  71. data/lib/zakuro/gateway/locale/date.rb +127 -0
  72. data/lib/zakuro/gateway/locale/range.rb +67 -0
  73. data/lib/zakuro/gateway/range.rb +99 -0
  74. data/lib/zakuro/gateway/single.rb +70 -0
  75. data/lib/zakuro/merchant.rb +15 -4
  76. data/lib/zakuro/operation/month/parser.rb +10 -10
  77. data/lib/zakuro/operation/month/type.rb +32 -32
  78. data/lib/zakuro/operation/month/validator.rb +51 -51
  79. data/lib/zakuro/operation/yaml/month.yaml +3736 -3736
  80. data/lib/zakuro/output/logger.rb +17 -3
  81. data/lib/zakuro/output/response.rb +5 -5
  82. data/lib/zakuro/result/data/day.rb +43 -0
  83. data/lib/zakuro/result/data/gengou.rb +35 -0
  84. data/lib/zakuro/result/data/month.rb +63 -0
  85. data/lib/zakuro/result/data/single_day.rb +48 -0
  86. data/lib/zakuro/result/data/solar_term.rb +35 -0
  87. data/lib/zakuro/result/data/year.rb +45 -0
  88. data/lib/zakuro/result/operation/month/annotation.rb +40 -0
  89. data/lib/zakuro/result/operation/month/bundle.rb +36 -0
  90. data/lib/zakuro/result/operation/month/history.rb +54 -0
  91. data/lib/zakuro/result/operation/operation.rb +44 -0
  92. data/lib/zakuro/result/result.rb +21 -2
  93. data/lib/zakuro/tools/typeconv.rb +38 -0
  94. data/lib/zakuro/tools/typeof.rb +4 -1
  95. data/lib/zakuro/version/context.rb +24 -3
  96. data/lib/zakuro/version/daien/daien.rb +1 -26
  97. data/lib/zakuro/version/genka/genka.rb +1 -26
  98. data/lib/zakuro/version/gihou/gihou.rb +1 -30
  99. data/lib/zakuro/version/gregorio/gregorio.rb +1 -9
  100. data/lib/zakuro/version/houryaku/houryaku.rb +1 -9
  101. data/lib/zakuro/version/joukyou/joukyou.rb +1 -9
  102. data/lib/zakuro/version/kansei/kansei.rb +1 -9
  103. data/lib/zakuro/version/senmyou/README.md +11 -7
  104. data/lib/zakuro/version/senmyou/senmyou.rb +1 -30
  105. data/lib/zakuro/version/tenpou/tenpou.rb +1 -9
  106. data/zakuro.gemspec +1 -3
  107. metadata +74 -22
  108. data/lib/zakuro/calculation/base/multi_gengou.rb +0 -101
  109. data/lib/zakuro/calculation/base/multi_gengou_roller.rb +0 -218
  110. data/lib/zakuro/calculation/range/full_range.rb +0 -210
  111. data/lib/zakuro/calculation/range/transfer/western_date_allocation.rb +0 -82
  112. data/lib/zakuro/calculation/specifier/single_day.rb +0 -109
  113. data/lib/zakuro/era/japan/gengou/parser.rb +0 -167
  114. data/lib/zakuro/era/japan/gengou/type.rb +0 -178
  115. data/lib/zakuro/era/japan/gengou/validator.rb +0 -236
  116. data/lib/zakuro/era/japan/reki.rb +0 -91
  117. data/lib/zakuro/era/japan/yaml/set-001-until-south.yaml +0 -1121
  118. data/lib/zakuro/era/japan/yaml/set-002-from-north.yaml +0 -485
  119. data/lib/zakuro/era/japan/yaml/set-003-modern.yaml +0 -28
  120. data/lib/zakuro/result/data.rb +0 -187
  121. data/lib/zakuro/result/operation.rb +0 -114
  122. data/lib/zakuro/version/abstract_version.rb +0 -29
  123. data/lib/zakuro/version.rb +0 -7
  124. data/lib/zakuro/version_factory.rb +0 -59
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 47e40b8ff5fd4b60fffed41193862d39a4d0569facec9c12f0aef65391bb4103
4
- data.tar.gz: a411cb2f43e952c52892ba4b5505ada7c96706effd2f4dae4019e0f39f409c01
3
+ metadata.gz: 294b07e6f6f82cad6ed7d5332d96a0c147ee1cbcbb842faa745918f1d8fbac2e
4
+ data.tar.gz: 41341f57aab606cea9a86c75aa4dbef8a8d247421d7428ddf5f1e442afc1b8db
5
5
  SHA512:
6
- metadata.gz: 640e6519e03854a38da45e19dbbf589762a4f24b89c55b683383eb56e88471f4469ee41cde88fb3a1d3f91862091d323f820a9e46b9a9b3297826d62f40d5159
7
- data.tar.gz: 0c9d510a3ffac317b74286013f6fd0e476074ab1a8de2b7368d710d91e1a9f10f114340eb8a66f88b3e28795865bf5a483ce1a8aa49ae8382bee40104fb85a3b
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
@@ -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
  # 条件
@@ -80,13 +87,15 @@ puts merchant.commit.to_json
80
87
  | 項目名 | キー名 | hash内キー名 | データ型 | 備考 |
81
88
  |----------------|---------|--------------|----------|--------------------|
82
89
  | 基準日 | date | | Date | 西暦日 |
83
- | | | | String | 和暦日 |
84
- | 範囲(開始日) | range | start | Date | |
85
- | 範囲(終了日) | | last | Date | |
90
+ | | | | String | 和暦日/西暦日 |
91
+ | 範囲(開始日) | range | start | Date | 西暦日 |
92
+ | | | | String | 和暦日/西暦日 |
93
+ | 範囲(終了日) | | last | Date | 西暦日 |
94
+ | | | | String | 和暦日/西暦日 |
86
95
  | 列 | columns | | Array | 取得したい列の列名 |
87
96
  | オプション | options | | Array | 取得オプション |
88
97
 
89
- 現時点では `date` の西暦日のみ対応中です。
98
+ 現時点では `date` と `range` のみ対応中です。
90
99
 
91
100
  ## オプション
92
101
 
@@ -119,6 +128,12 @@ puts merchant.commit.to_json
119
128
  | 日付情報 | data | | | Zakuro::Result::Data::SingleDay | - | 運用値(計算値は運用情報内を参照のこと)|
120
129
  | 運用情報 | operation | | | Zakuro::Result::Operation::Bundle | - | - |
121
130
 
131
+ ### Zakuro::Result::Range
132
+
133
+ | 項目名 | キー名 | - | - | データ型 | 参考値 | 備考   |
134
+ |----------|-----------|---|---|---------------------------------------|--------|---------------------------------------|
135
+ | 日リスト | list | | | Array\<Zakuro::Result::Data::Single\> | - | 範囲内の日付情報すべて |
136
+
122
137
  ### Zakuro::Result::SingleDay
123
138
 
124
139
  | 項目名 | キー名 | - | - | データ型 | 参考値 | 備考 |
@@ -176,8 +191,8 @@ puts merchant.commit.to_json
176
191
  | 月初日の西暦日 | | western_date |   | String | 0937-02-13 | 計算値 |
177
192
  | 原文頁数 | | page |   | Integer | 178 |   |
178
193
  | 原文注釈番号 | | number |   | Integer | 1 |   |
179
- | 注釈 | | annotations |   | Array<Zakuro::Result::Operation::::MonthAnnotation> |   |   |
180
- | 注釈内容 |   | | description | String | 計算は51乙卯であるが, 日本紀略に甲寅朔とある。正月甲寅朔のユリウス暦日は2月13日となる。(元旦日食 をさけるための変更か) |   |
194
+ | 注釈 | | annotations |   | Array\<Zakuro::Result::Operation::MonthAnnotation\> |   |   |
195
+ | 注釈内容 |   | | description | String | 計算は51乙卯であるが, 日本紀略に甲寅朔とある。<br>正月甲寅朔のユリウス暦日は2月13日となる。(元旦日食 をさけるための変更か) |   |
181
196
  | 注釈補記 |   | | note | String | - | 原文訂正 |
182
197
 
183
198
 
data/VERSION ADDED
@@ -0,0 +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['中気(実)']
@@ -0,0 +1,128 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative '../../era/western/calendar'
4
+
5
+ # :nodoc:
6
+ module Zakuro
7
+ # :nodoc:
8
+ module Calculation
9
+ # :nodoc:
10
+ module Base
11
+ #
12
+ # Gengou 元号
13
+ #
14
+ class Gengou
15
+ # @return [Western::Calendar] 開始日
16
+ attr_reader :start_date
17
+ # @return [Western::Calendar] 終了日
18
+ attr_reader :last_date
19
+ # @return [Array<LinearGengou>] 1行目元号
20
+ attr_reader :first_line
21
+ # @return [Array<LinearGengou>] 2行目元号
22
+ attr_reader :second_line
23
+
24
+ #
25
+ # 初期化
26
+ #
27
+ # @param [Western::Calendar] start_date 開始日
28
+ # @param [Western::Calendar] last_date 終了日
29
+ # @param [Array<LinearGengou>] first_line 1行目元号
30
+ # @param [Array<LinearGengou>] second_line 2行目元号
31
+ #
32
+ def initialize(start_date: Western::Calendar.new, last_date: Western::Calendar.new,
33
+ first_line: [], second_line: [])
34
+ @start_date = start_date
35
+ @last_date = last_date
36
+ @first_line = first_line
37
+ @second_line = second_line
38
+ end
39
+
40
+ #
41
+ # 1行目元号を取得する
42
+ #
43
+ # @param [Western::Calendar] date 西暦日
44
+ #
45
+ # @return [LinearGengou] 1行目元号
46
+ #
47
+ def match_first_line(date: Western::Calendar)
48
+ @first_line.each do |line|
49
+ return line.clone if line.include?(date: date)
50
+ end
51
+
52
+ LinearGengou.new
53
+ end
54
+
55
+ #
56
+ # 2行目元号を取得する
57
+ #
58
+ # @param [Western::Calendar] date 西暦日
59
+ #
60
+ # @return [LinearGengou] 2行目元号
61
+ #
62
+ def match_second_line(date: Western::Calendar)
63
+ @second_line.each do |line|
64
+ return line.clone if line.include?(date: date)
65
+ end
66
+
67
+ LinearGengou.new
68
+ end
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
+
116
+ #
117
+ # 不正か
118
+ #
119
+ # @return [True] 不正
120
+ # @return [False] 不正なし
121
+ #
122
+ def invalid?
123
+ @first_line.size.zero? && @second_line.size.zero?
124
+ end
125
+ end
126
+ end
127
+ end
128
+ end
@@ -0,0 +1,76 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative '../../era/western/calendar'
4
+
5
+ # :nodoc:
6
+ module Zakuro
7
+ # :nodoc:
8
+ module Calculation
9
+ # :nodoc:
10
+ module Base
11
+ #
12
+ # LinearGengou 直列元号
13
+ #
14
+ class LinearGengou
15
+ INVALID_YEAR = -1
16
+
17
+ # @return [Western::Calendar] 開始日
18
+ attr_reader :start_date
19
+ # @return [Western::Calendar] 終了日
20
+ attr_reader :last_date
21
+ # @return [String] 元号名
22
+ attr_reader :name
23
+ # @return [Integer] 年
24
+ attr_reader :year
25
+
26
+ #
27
+ # 初期化
28
+ #
29
+ # @param [Western::Calendar] start_date 開始日
30
+ # @param [Western::Calendar] last_date 終了日
31
+ # @param [String] name 元号名
32
+ # @param [Integer] 元号年
33
+ #
34
+ def initialize(start_date: Western::Calendar.new, last_date: Western::Calendar.new,
35
+ name: '', year: INVALID_YEAR)
36
+ @start_date = start_date
37
+ @last_date = last_date
38
+ @name = name
39
+ @year = year
40
+ end
41
+
42
+ #
43
+ # 不正か
44
+ #
45
+ # @return [True] 不正
46
+ # @return [False] 不正なし
47
+ #
48
+ def invalid?
49
+ @name == '' || @year == INVALID_YEAR
50
+ end
51
+
52
+ #
53
+ # 範囲内か
54
+ #
55
+ # @param [Western::Calendar] date 西暦日
56
+ #
57
+ # @return [True] 範囲内
58
+ # @return [False] 範囲外
59
+ #
60
+ def include?(date: Western::Calendar.new)
61
+ return false if invalid?
62
+
63
+ return false if @start_date.invalid?
64
+
65
+ return false if @last_date.invalid?
66
+
67
+ return false if date < @start_date
68
+
69
+ return false if date > @last_date
70
+
71
+ true
72
+ end
73
+ end
74
+ end
75
+ end
76
+ end
@@ -1,7 +1,8 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require_relative './year'
4
- require_relative './multi_gengou'
4
+
5
+ require_relative '../../version/context'
5
6
 
6
7
  # :nodoc:
7
8
  module Zakuro
@@ -16,23 +17,12 @@ module Zakuro
16
17
  #
17
18
  # 初期化
18
19
  #
19
- # @param [Gengou] multi_gengou 元号
20
+ # @param [Context] context 暦コンテキスト
20
21
  # @param [Array<OperatedMonth>] months 年内の全ての月
21
22
  # @param [Integer] total_days 年の日数
22
- # @param [Western::Calendar] new_year_date 元旦
23
23
  #
24
- def initialize(multi_gengou: MultiGengou.new, new_year_date: Western::Calendar.new,
25
- months: [], total_days: 0)
26
- super(multi_gengou: multi_gengou, new_year_date: new_year_date,
27
- months: months, total_days: total_days)
28
- end
29
-
30
- def commit
31
- super
32
-
33
- return if months.empty?
34
-
35
- @new_year_date = months[0].first_day.western_date
24
+ def initialize(context: Context.new, months: [], total_days: 0)
25
+ super(context: context, months: months, total_days: total_days)
36
26
  end
37
27
 
38
28
  #
@@ -2,6 +2,10 @@
2
2
 
3
3
  require_relative '../cycle/zodiac'
4
4
 
5
+ require_relative '../../era/western/calendar'
6
+
7
+ require_relative '../../version/context'
8
+
5
9
  # :nodoc:
6
10
  module Zakuro
7
11
  # :nodoc:
@@ -12,28 +16,23 @@ module Zakuro
12
16
  # Year 年
13
17
  #
14
18
  class Year
15
- # @return [Gengou] 元号
16
- attr_reader :multi_gengou
19
+ # @return [Context] 暦コンテキスト
20
+ attr_reader :context
17
21
  # @return [Array<Month>] 年内の全ての月
18
22
  attr_reader :months
19
23
  # @return [Integer] 年の日数
20
24
  attr_reader :total_days
21
- # @return [Western::Calendar] 元旦
22
- attr_reader :new_year_date
23
25
 
24
26
  #
25
27
  # 初期化
26
28
  #
27
- # @param [Gengou] multi_gengou 元号
29
+ # @param [Context] context 暦コンテキスト
28
30
  # @param [Array<Month>] months 年内の全ての月
29
31
  # @param [Integer] total_days 年の日数
30
- # @param [Western::Calendar] new_year_date 元旦
31
32
  #
32
- def initialize(multi_gengou: MultiGengou.new, new_year_date: Western::Calendar.new,
33
- months: [], total_days: 0)
34
- @multi_gengou = multi_gengou
33
+ def initialize(context: Context.new, months: [], total_days: 0)
34
+ @context = context
35
35
  @months = months
36
- @new_year_date = new_year_date
37
36
  @total_days = total_days
38
37
  end
39
38
 
@@ -58,21 +57,34 @@ module Zakuro
58
57
  # @return [MultiGengou] 自身
59
58
  #
60
59
  def next_year
61
- @multi_gengou.next_year
62
-
63
- @new_year_date += @total_days
64
60
  @total_days = 0
65
61
 
66
62
  self
67
63
  end
68
64
 
65
+ #
66
+ # 年初を取得する
67
+ #
68
+ # @return [Western::Calendar] 年初
69
+ #
70
+ def new_year_date
71
+ return Western::Calendar.new unless months
72
+
73
+ return Western::Calendar.new if months.size.zero?
74
+
75
+ @months[0].western_date
76
+ end
77
+
69
78
  #
70
79
  # 十干十二支を取得する
71
80
  #
72
81
  # @return [String] 十干十二支
73
82
  #
74
83
  def zodiac_name
75
- Cycle::Zodiac.year_name(western_year: @new_year_date.year)
84
+ date = new_year_date
85
+ return '' if date.invalid?
86
+
87
+ Cycle::Zodiac.year_name(western_year: date.year)
76
88
  end
77
89
 
78
90
  #
@@ -0,0 +1,209 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative '../../../era/western/calendar'
4
+ require_relative '../../base/gengou'
5
+ require_relative '../../base/linear_gengou'
6
+ require_relative './internal/connector'
7
+ require_relative './internal/publisher'
8
+
9
+ # :nodoc:
10
+ module Zakuro
11
+ # :nodoc:
12
+ module Calculation
13
+ # :nodoc:
14
+ module Gengou
15
+ # AbstractScroll
16
+ #
17
+ # 元号スクロール
18
+ #
19
+ class AbstractScroll
20
+ # @return [Western::Calendar] 月初日
21
+ attr_reader :monthly_start_date
22
+ # @return [Western::Calendar] 月末日
23
+ attr_reader :monthly_last_date
24
+ # @return [Reserve::AbstractRange] 予約範囲
25
+ attr_reader :range
26
+ # @return [Array<Counte>] 1行目元号
27
+ attr_reader :first_gengou
28
+ # @return [Array<Counte>] 2行目元号
29
+ attr_reader :second_gengou
30
+ # @return [Connector] 行変更済元号
31
+ attr_reader :connector
32
+
33
+ #
34
+ # 初期化
35
+ #
36
+ # @param [Reserve::AbstractRange] range 予約範囲
37
+ #
38
+ def initialize(range:)
39
+ @monthly_start_date = Western::Calendar.new
40
+ @monthly_last_date = Western::Calendar.new
41
+ @range = range
42
+ @first_gengou = []
43
+ @second_gengou = []
44
+ @ignited = false
45
+ @connector = Connector.new
46
+ end
47
+
48
+ #
49
+ # 進める
50
+ #
51
+ # @param [Monthly::Month] month 月
52
+ #
53
+ def run(month:)
54
+ unless @ignited
55
+ # 開始日の検索を行う
56
+ @ignited = ignite(month: month)
57
+ return
58
+ end
59
+
60
+ # 時間を進める
61
+ advance(month: month)
62
+ end
63
+
64
+ #
65
+ # 元号開始を試みる
66
+ #
67
+ # @param [Monthly::Month] month 月
68
+ #
69
+ # @return [True] 開始あり
70
+ # @return [False] 開始なし
71
+ #
72
+ def ignite(month:)
73
+ return false unless ignitable?(month: month)
74
+
75
+ japan_start_date = @range.japan_start_date
76
+
77
+ western_start_date = @range.western_start_date
78
+
79
+ # 今月初日(和暦日が1月2日であれば、開始日の1日前が初日)
80
+ @monthly_start_date = western_start_date.clone - japan_start_date.day + 1
81
+
82
+ # 今月末
83
+ @monthly_last_date = @monthly_start_date.clone + month.days - 1
84
+
85
+ update_current_gengou
86
+
87
+ true
88
+ end
89
+
90
+ #
91
+ # 進める
92
+ #
93
+ # @param [Monthly::Month] month 月
94
+ #
95
+ def advance(month:)
96
+ @monthly_start_date = @monthly_last_date.clone + 1
97
+
98
+ @monthly_last_date = @monthly_start_date.clone + month.days - 1
99
+
100
+ next_year if month.number == 1 && !month.leaped?
101
+
102
+ update_current_gengou
103
+ end
104
+
105
+ #
106
+ # 共通の元号に変換する
107
+ #
108
+ # @return [Base::Gengou] 元号
109
+ #
110
+ def to_gengou
111
+ start_date = @monthly_start_date.clone
112
+ last_date = @monthly_last_date.clone
113
+
114
+ # 行を超えた元号切り替え処理
115
+ continue_year
116
+
117
+ Publisher.run(
118
+ start_date: start_date, last_date: last_date,
119
+ first_gengou: @first_gengou, second_gengou: @second_gengou
120
+ )
121
+ end
122
+
123
+ #
124
+ # 開始西暦年を取得する
125
+ #
126
+ # @return [Integer] 開始西暦年
127
+ #
128
+ def western_start_year
129
+ @range.western_start_year
130
+ end
131
+
132
+ #
133
+ # 終了西暦年を取得する
134
+ #
135
+ # @return [Integer] 終了西暦年
136
+ #
137
+ def western_last_year
138
+ @range.western_last_year
139
+ end
140
+
141
+ private
142
+
143
+ #
144
+ # 行を跨ぐ元号年を継続させる
145
+ #
146
+ def continue_year
147
+ @connector.update(lines: [@first_gengou, @second_gengou])
148
+ end
149
+
150
+ #
151
+ # 現在月に合わせて元号を更新する
152
+ #
153
+ def update_current_gengou
154
+ start_date = @monthly_start_date
155
+ last_date = @monthly_last_date
156
+ first_gengou = @range.collect_first(start_date: start_date, last_date: last_date)
157
+ second_gengou = @range.collect_second(start_date: start_date,
158
+ last_date: last_date)
159
+
160
+ @first_gengou = replace_gengou(source: @first_gengou, destination: first_gengou)
161
+ @second_gengou = replace_gengou(source: @second_gengou, destination: second_gengou)
162
+ end
163
+
164
+ #
165
+ # 元号を差し替える
166
+ #
167
+ # @param [Array<Counter>] source 元の元号
168
+ # @param [Array<Counter>] destination 次の元号
169
+ #
170
+ # @return [Array<Counter>] 差し替え結果
171
+ #
172
+ def replace_gengou(source: [], destination: [])
173
+ return destination if destination.size.zero?
174
+
175
+ return destination if source.size.zero?
176
+
177
+ last = source[-1]
178
+ destination[0] = last if destination[0].name == last.name
179
+
180
+ destination
181
+ end
182
+
183
+ #
184
+ # 開始可能か
185
+ #
186
+ # @param [Monthly::Month] month 月
187
+ #
188
+ # @return [True] 開始可
189
+ # @return [True] 開始不可
190
+ #
191
+ def ignitable?(month:)
192
+ return false unless @monthly_start_date.invalid?
193
+
194
+ japan_start_date = @range.japan_start_date
195
+
196
+ japan_start_date.same_month?(leaped: month.leaped?, month: month.number)
197
+ end
198
+
199
+ #
200
+ # 次年にする
201
+ #
202
+ def next_year
203
+ @first_gengou.each(&:next_year)
204
+ @second_gengou.each(&:next_year)
205
+ end
206
+ end
207
+ end
208
+ end
209
+ end