zakuro 0.6.1 → 0.8.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (181) hide show
  1. checksums.yaml +4 -4
  2. data/Makefile +3 -0
  3. data/README.md +46 -17
  4. data/VERSION +1 -1
  5. data/doc/dropped_date.md +93 -0
  6. data/doc/error.md +45 -0
  7. data/doc/operation/transfer.rb +8 -8
  8. data/lib/zakuro/calculation/base/day.rb +52 -0
  9. data/lib/zakuro/calculation/base/gengou.rb +49 -3
  10. data/lib/zakuro/calculation/base/linear_gengou.rb +5 -5
  11. data/lib/zakuro/calculation/base/operated_year.rb +26 -24
  12. data/lib/zakuro/calculation/base/year.rb +7 -7
  13. data/lib/zakuro/calculation/cycle/abstract_remainder.rb +55 -47
  14. data/lib/zakuro/calculation/cycle/abstract_solar_term.rb +28 -24
  15. data/lib/zakuro/calculation/cycle/zodiac.rb +22 -20
  16. data/lib/zakuro/calculation/era/gengou/{scroll.rb → abstract_scroll.rb} +44 -37
  17. data/lib/zakuro/calculation/era/gengou/dated_scroll.rb +30 -0
  18. data/lib/zakuro/calculation/era/gengou/internal/connector.rb +4 -4
  19. data/lib/zakuro/calculation/era/gengou/internal/counter.rb +16 -16
  20. data/lib/zakuro/calculation/era/gengou/internal/publisher.rb +74 -72
  21. data/lib/zakuro/calculation/era/gengou/internal/reserve/{list.rb → abstract_list.rb} +50 -64
  22. data/lib/zakuro/calculation/era/gengou/internal/reserve/{range.rb → abstract_range.rb} +73 -50
  23. data/lib/zakuro/calculation/era/gengou/internal/reserve/dated_list.rb +48 -0
  24. data/lib/zakuro/calculation/era/gengou/internal/reserve/dated_range.rb +30 -0
  25. data/lib/zakuro/calculation/era/gengou/internal/reserve/empty_link.rb +156 -0
  26. data/lib/zakuro/calculation/era/gengou/internal/reserve/named_list.rb +125 -0
  27. data/lib/zakuro/calculation/era/gengou/internal/reserve/named_range.rb +73 -0
  28. data/lib/zakuro/calculation/era/gengou/internal/reserve.rb +23 -22
  29. data/lib/zakuro/calculation/era/gengou/named_scroll.rb +30 -0
  30. data/lib/zakuro/calculation/era/version/internal/crawler.rb +39 -22
  31. data/lib/zakuro/calculation/era/version/version.rb +12 -10
  32. data/lib/zakuro/calculation/monthly/abstract_lunar_phase.rb +13 -9
  33. data/lib/zakuro/calculation/monthly/const.rb +18 -0
  34. data/lib/zakuro/calculation/monthly/initialized_month.rb +40 -38
  35. data/lib/zakuro/calculation/monthly/month.rb +55 -16
  36. data/lib/zakuro/calculation/monthly/month_label.rb +4 -4
  37. data/lib/zakuro/calculation/monthly/operated_month.rb +37 -86
  38. data/lib/zakuro/calculation/monthly/operated_solar_term.rb +80 -0
  39. data/lib/zakuro/calculation/option/dropped_date/abstract_parameter.rb +44 -0
  40. data/lib/zakuro/calculation/option/dropped_date/location.rb +135 -0
  41. data/lib/zakuro/calculation/range/{full_range.rb → abstract_full_range.rb} +45 -29
  42. data/lib/zakuro/calculation/range/abstract_operation_range.rb +193 -0
  43. data/lib/zakuro/calculation/range/dated_full_range.rb +32 -0
  44. data/lib/zakuro/calculation/range/dated_operation_range.rb +33 -0
  45. data/lib/zakuro/calculation/range/medieval_annual_range.rb +64 -63
  46. data/lib/zakuro/calculation/range/named_full_range.rb +38 -0
  47. data/lib/zakuro/calculation/range/named_operation_range.rb +36 -0
  48. data/lib/zakuro/calculation/range/operated_solar_term.rb +220 -0
  49. data/lib/zakuro/calculation/range/transfer/gengou_scroller.rb +32 -29
  50. data/lib/zakuro/calculation/range/transfer/year_boundary.rb +115 -117
  51. data/lib/zakuro/calculation/stella/lunar/abstract_location.rb +1 -1
  52. data/lib/zakuro/calculation/stella/lunar/choukei_value.rb +48 -46
  53. data/lib/zakuro/calculation/stella/solar/abstract_average.rb +31 -26
  54. data/lib/zakuro/calculation/stella/solar/abstract_location.rb +7 -5
  55. data/lib/zakuro/calculation/stella/solar/choukei_value.rb +114 -114
  56. data/lib/zakuro/calculation/summary/internal/day.rb +42 -0
  57. data/lib/zakuro/calculation/{specifier → summary}/internal/month.rb +33 -12
  58. data/lib/zakuro/calculation/summary/internal/operation.rb +68 -67
  59. data/lib/zakuro/calculation/summary/internal/option.rb +101 -0
  60. data/lib/zakuro/calculation/summary/japan/range.rb +179 -0
  61. data/lib/zakuro/calculation/summary/japan/single.rb +118 -0
  62. data/lib/zakuro/calculation/summary/japan/specifier/single_day.rb +98 -0
  63. data/lib/zakuro/calculation/summary/japan/specifier/specified_range.rb +34 -0
  64. data/lib/zakuro/calculation/summary/western/range.rb +127 -0
  65. data/lib/zakuro/calculation/summary/western/single.rb +115 -0
  66. data/lib/zakuro/calculation/summary/western/specifier/multiple_day.rb +173 -0
  67. data/lib/zakuro/calculation/summary/western/specifier/single_day.rb +100 -0
  68. data/lib/zakuro/calculation/type/old_float.rb +2 -2
  69. data/lib/zakuro/calculation/type/optional.rb +46 -0
  70. data/lib/zakuro/condition.rb +143 -94
  71. data/lib/zakuro/context/context.rb +44 -0
  72. data/lib/zakuro/context/option.rb +119 -0
  73. data/lib/zakuro/context/version_class_resolver.rb +112 -0
  74. data/lib/zakuro/era/japan/calendar.rb +35 -33
  75. data/lib/zakuro/era/japan/gengou/alignment/aligner.rb +93 -74
  76. data/lib/zakuro/era/japan/gengou/alignment/division.rb +135 -133
  77. data/lib/zakuro/era/japan/gengou/alignment/line.rb +143 -122
  78. data/lib/zakuro/era/japan/gengou/alignment/linear_gengou.rb +177 -168
  79. data/lib/zakuro/era/japan/gengou/alignment.rb +40 -25
  80. data/lib/zakuro/era/japan/gengou/resource/parser.rb +199 -181
  81. data/lib/zakuro/era/japan/gengou/resource/type.rb +243 -234
  82. data/lib/zakuro/era/japan/gengou/resource/validator.rb +257 -254
  83. data/lib/zakuro/era/japan/gengou/resource.rb +23 -22
  84. data/lib/zakuro/era/japan/gengou.rb +28 -41
  85. data/lib/zakuro/era/japan/version.rb +18 -16
  86. data/lib/zakuro/era/western/calendar.rb +104 -88
  87. data/lib/zakuro/exception/case/pattern.rb +71 -0
  88. data/lib/zakuro/exception/case/preset.rb +51 -0
  89. data/lib/zakuro/exception/case/template.rb +48 -0
  90. data/lib/zakuro/exception/cause.rb +28 -0
  91. data/lib/zakuro/exception/exception.rb +39 -0
  92. data/lib/zakuro/exception/zakuro_error.rb +46 -0
  93. data/lib/zakuro/gateway/locale/date.rb +127 -0
  94. data/lib/zakuro/gateway/locale/range.rb +67 -0
  95. data/lib/zakuro/gateway/range.rb +99 -0
  96. data/lib/zakuro/gateway/single.rb +81 -0
  97. data/lib/zakuro/merchant.rb +67 -33
  98. data/lib/zakuro/operation/month/parser.rb +252 -249
  99. data/lib/zakuro/operation/month/type.rb +44 -35
  100. data/lib/zakuro/operation/month/validator.rb +182 -178
  101. data/lib/zakuro/operation/operation.rb +38 -36
  102. data/lib/zakuro/operation/yaml/month.yaml +3736 -3736
  103. data/lib/zakuro/output/logger.rb +17 -3
  104. data/lib/zakuro/output/response.rb +107 -141
  105. data/lib/zakuro/result/data/day.rb +43 -0
  106. data/lib/zakuro/result/data/gengou.rb +35 -0
  107. data/lib/zakuro/result/data/month.rb +63 -0
  108. data/lib/zakuro/result/data/option/abstract_option.rb +37 -0
  109. data/lib/zakuro/result/data/option/dropped_date/calculation.rb +47 -0
  110. data/lib/zakuro/result/data/option/dropped_date/option.rb +47 -0
  111. data/lib/zakuro/result/data/option/dropped_date/solar_term.rb +48 -0
  112. data/lib/zakuro/result/data/single_day.rb +56 -0
  113. data/lib/zakuro/result/data/solar_term.rb +35 -0
  114. data/lib/zakuro/result/data/year.rb +45 -0
  115. data/lib/zakuro/result/operation/month/annotation.rb +40 -0
  116. data/lib/zakuro/result/operation/month/history.rb +54 -0
  117. data/lib/zakuro/result/operation/month.rb +31 -0
  118. data/lib/zakuro/result/operation.rb +22 -97
  119. data/lib/zakuro/result/result.rb +6 -4
  120. data/lib/zakuro/tools/stringifier.rb +67 -42
  121. data/lib/zakuro/tools/typeconv.rb +17 -15
  122. data/lib/zakuro/tools/typeof.rb +15 -13
  123. data/lib/zakuro/version/daien/cycle/remainder.rb +2 -2
  124. data/lib/zakuro/version/daien/monthly/lunar_phase.rb +10 -12
  125. data/lib/zakuro/version/daien/option/dropped_date/parameter.rb +31 -0
  126. data/lib/zakuro/version/daien/range/annual_range.rb +19 -17
  127. data/lib/zakuro/version/daien/stella/lunar/adjustment.rb +42 -37
  128. data/lib/zakuro/version/daien/stella/lunar/localization.rb +16 -14
  129. data/lib/zakuro/version/daien/stella/lunar/value.rb +52 -47
  130. data/lib/zakuro/version/daien/stella/origin/average_november.rb +15 -13
  131. data/lib/zakuro/version/daien/stella/origin/lunar_age.rb +23 -21
  132. data/lib/zakuro/version/daien/stella/origin/winter_solstice.rb +22 -20
  133. data/lib/zakuro/version/daien/stella/solar/adjustment.rb +12 -10
  134. data/lib/zakuro/version/daien/stella/solar/average.rb +53 -51
  135. data/lib/zakuro/version/daien/stella/solar/interval.rb +19 -17
  136. data/lib/zakuro/version/daien/stella/solar/value.rb +13 -11
  137. data/lib/zakuro/version/genka/cycle/remainder.rb +4 -4
  138. data/lib/zakuro/version/genka/monthly/lunar_phase.rb +7 -5
  139. data/lib/zakuro/version/genka/option/dropped_date/parameter.rb +31 -0
  140. data/lib/zakuro/version/genka/range/annual_range.rb +48 -44
  141. data/lib/zakuro/version/genka/stella/origin/first_term.rb +30 -28
  142. data/lib/zakuro/version/genka/stella/origin/january.rb +22 -20
  143. data/lib/zakuro/version/gihou/cycle/remainder.rb +2 -2
  144. data/lib/zakuro/version/gihou/monthly/lunar_phase.rb +9 -9
  145. data/lib/zakuro/version/gihou/option/dropped_date/parameter.rb +31 -0
  146. data/lib/zakuro/version/gihou/range/annual_range.rb +19 -17
  147. data/lib/zakuro/version/gihou/stella/lunar/adjustment.rb +42 -37
  148. data/lib/zakuro/version/gihou/stella/lunar/localization.rb +16 -14
  149. data/lib/zakuro/version/gihou/stella/lunar/value.rb +52 -47
  150. data/lib/zakuro/version/gihou/stella/origin/average_november.rb +15 -13
  151. data/lib/zakuro/version/gihou/stella/origin/lunar_age.rb +23 -21
  152. data/lib/zakuro/version/gihou/stella/origin/winter_solstice.rb +24 -22
  153. data/lib/zakuro/version/gihou/stella/solar/adjustment.rb +12 -10
  154. data/lib/zakuro/version/gihou/stella/solar/average.rb +52 -50
  155. data/lib/zakuro/version/gihou/stella/solar/interval.rb +19 -17
  156. data/lib/zakuro/version/gihou/stella/solar/value.rb +13 -11
  157. data/lib/zakuro/version/senmyou/const/remainder.rb +5 -0
  158. data/lib/zakuro/version/senmyou/cycle/remainder.rb +37 -2
  159. data/lib/zakuro/version/senmyou/monthly/lunar_phase.rb +10 -10
  160. data/lib/zakuro/version/senmyou/option/dropped_date/parameter.rb +33 -0
  161. data/lib/zakuro/version/senmyou/range/annual_range.rb +19 -17
  162. data/lib/zakuro/version/senmyou/stella/lunar/adjustment.rb +42 -36
  163. data/lib/zakuro/version/senmyou/stella/lunar/localization.rb +16 -14
  164. data/lib/zakuro/version/senmyou/stella/lunar/value.rb +39 -34
  165. data/lib/zakuro/version/senmyou/stella/origin/average_november.rb +15 -13
  166. data/lib/zakuro/version/senmyou/stella/origin/lunar_age.rb +23 -21
  167. data/lib/zakuro/version/senmyou/stella/origin/winter_solstice.rb +22 -20
  168. data/lib/zakuro/version/senmyou/stella/solar/adjustment.rb +12 -10
  169. data/lib/zakuro/version/senmyou/stella/solar/average.rb +52 -50
  170. data/lib/zakuro/version/senmyou/stella/solar/interval.rb +19 -17
  171. data/lib/zakuro/version/senmyou/stella/solar/value.rb +13 -11
  172. metadata +68 -16
  173. data/lib/zakuro/calculation/range/operated_range.rb +0 -193
  174. data/lib/zakuro/calculation/range/operated_solar_terms.rb +0 -220
  175. data/lib/zakuro/calculation/specifier/multiple_day.rb +0 -162
  176. data/lib/zakuro/calculation/specifier/single_day.rb +0 -87
  177. data/lib/zakuro/calculation/summary/range.rb +0 -120
  178. data/lib/zakuro/calculation/summary/single.rb +0 -110
  179. data/lib/zakuro/result/data.rb +0 -187
  180. data/lib/zakuro/version/context.rb +0 -44
  181. data/lib/zakuro/version/version_class_resolver.rb +0 -74
@@ -0,0 +1,46 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'json'
4
+ require_relative './cause'
5
+
6
+ # :nodoc:
7
+ module Zakuro
8
+ # :nodoc:
9
+ module Exception
10
+ #
11
+ # ZakuroError ライブラリ内エラー
12
+ #
13
+ class ZakuroError < StandardError
14
+ # @return [Array<ErrorMessage>] エラーメッセージ
15
+ attr_reader :causes
16
+
17
+ #
18
+ # 初期化
19
+ #
20
+ # @param [String] msg メッセージ
21
+ # @param [Array<Cause>] causes 原因
22
+ #
23
+ def initialize(msg: '', causes: [])
24
+ @causes = causes
25
+ out = "#{msg}:#{text}"
26
+ super(out)
27
+ end
28
+
29
+ private
30
+
31
+ #
32
+ # 原因をJSON文字列にする
33
+ #
34
+ # @return [String] JSON文字列
35
+ #
36
+ def text
37
+ list = []
38
+ causes.each do |cause|
39
+ list.push(Tools::Stringifier.to_h(obj: cause, class_prefix: 'Zakuro'))
40
+ end
41
+
42
+ JSON.generate(list)
43
+ end
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,127 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative '../../era/japan/calendar'
4
+ require_relative '../../era/western/calendar'
5
+
6
+ require 'date'
7
+
8
+ # :nodoc:
9
+ module Zakuro
10
+ # :nodoc:
11
+ module Gateway
12
+ # :nodoc:
13
+ class Locale
14
+ #
15
+ # Date 日付
16
+ #
17
+ class Date
18
+ INDALID = Object::Date.new
19
+
20
+ # @return [Western::Calendar] 西暦日
21
+ attr_reader :western_date
22
+ # @return [Japan::Calendar] 和暦日
23
+ attr_reader :japan_date
24
+
25
+ #
26
+ # 初期化
27
+ #
28
+ # @param [Date, String] date 日付
29
+ #
30
+ def initialize(date: INDALID)
31
+ @western_date = Western::Calendar.new
32
+ @japan_date = Japan::Calendar.new
33
+
34
+ parse(date: date)
35
+ end
36
+
37
+ #
38
+ # 和暦日が有効か
39
+ #
40
+ # @return [True] 有効
41
+ # @return [False] 無効
42
+ #
43
+ def valid_japan?
44
+ !invalid_japan?
45
+ end
46
+
47
+ #
48
+ # 和暦日が不正か
49
+ #
50
+ # @return [True] 不正
51
+ # @return [False] 不正なし
52
+ #
53
+ def invalid_japan?
54
+ japan_date.invalid?
55
+ end
56
+
57
+ #
58
+ # 西暦日が有効か
59
+ #
60
+ # @return [True] 有効
61
+ # @return [False] 無効
62
+ #
63
+ def valid_western?
64
+ !invalid_western?
65
+ end
66
+
67
+ #
68
+ # 西暦日が不正か
69
+ #
70
+ # @return [True] 不正
71
+ # @return [False] 不正なし
72
+ #
73
+ def invalid_western?
74
+ western_date.invalid?
75
+ end
76
+
77
+ #
78
+ # 不正か
79
+ #
80
+ # @return [True] 不正
81
+ # @return [False] 不正なし
82
+ #
83
+ def invalid?
84
+ invalid_japan? && invalid_western?
85
+ end
86
+
87
+ private
88
+
89
+ #
90
+ # 解析する
91
+ #
92
+ # @param [Date, String] date 日付
93
+ #
94
+ def parse(date:)
95
+ return unless date
96
+
97
+ return if date == INDALID
98
+
99
+ if date.is_a?(Object::Date)
100
+ @western_date = Western::Calendar.create(date: date)
101
+ return
102
+ end
103
+
104
+ return unless date.is_a?(String)
105
+
106
+ parse_text(text: date)
107
+ end
108
+
109
+ #
110
+ # 文字解析する
111
+ #
112
+ # @param [String] text 日付文字列
113
+ #
114
+ def parse_text(text:)
115
+ if Western::Calendar.valid_date_string(text: text)
116
+ @western_date = Western::Calendar.parse(text: text)
117
+ return
118
+ end
119
+
120
+ return unless Japan::Calendar.valid_date_string(text: text)
121
+
122
+ @japan_date = Japan::Calendar.parse(text: text)
123
+ end
124
+ end
125
+ end
126
+ end
127
+ end
@@ -0,0 +1,67 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative './date'
4
+
5
+ # :nodoc:
6
+ module Zakuro
7
+ # :nodoc:
8
+ module Gateway
9
+ # :nodoc:
10
+ class Locale
11
+ #
12
+ # Range 範囲
13
+ #
14
+ class Range
15
+ # @return [LocaleDate] 開始日
16
+ attr_reader :start_date
17
+ # @return [LocaleDate] 終了日
18
+ attr_reader :last_date
19
+
20
+ #
21
+ # 初期化
22
+ #
23
+ # @param [Hash<Symbol, Object>] range 範囲
24
+ #
25
+ def initialize(range:)
26
+ @start_date = Date.new
27
+ @last_date = Date.new
28
+
29
+ return unless range
30
+
31
+ @start_date = Date.new(date: range[:start])
32
+ @last_date = Date.new(date: range[:last])
33
+ end
34
+
35
+ #
36
+ # 西暦日は有効か
37
+ #
38
+ # @return [True] 有効
39
+ # @return [False] 無効
40
+ #
41
+ def valid_western?
42
+ start_date.valid_western? && last_date.valid_western?
43
+ end
44
+
45
+ #
46
+ # 和暦日は有効か
47
+ #
48
+ # @return [True] 有効
49
+ # @return [False] 無効
50
+ #
51
+ def valid_japan?
52
+ start_date.valid_japan? && last_date.valid_japan?
53
+ end
54
+
55
+ #
56
+ # 不正か
57
+ #
58
+ # @return [True] 不正
59
+ # @return [False] 不正なし
60
+ #
61
+ def invalid?
62
+ start_date.invalid? && last_date.invalid?
63
+ end
64
+ end
65
+ end
66
+ end
67
+ end
@@ -0,0 +1,99 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative '../calculation/summary/japan/range'
4
+ require_relative '../calculation/summary/western/range'
5
+ require_relative '../exception/case/preset'
6
+ require_relative '../exception/exception'
7
+
8
+ require_relative './locale/range'
9
+
10
+ # :nodoc:
11
+ module Zakuro
12
+ # :nodoc:
13
+ module Gateway
14
+ #
15
+ # Range 範囲
16
+ #
17
+ class Range
18
+ # @return [Context::Context] 暦コンテキスト
19
+ attr_reader :context
20
+ # @return [Locale::Range] 範囲
21
+ attr_reader :range
22
+
23
+ #
24
+ # 初期化
25
+ #
26
+ # @param [Context::Context] context 暦コンテキスト
27
+ # @param [Hash<Symbol, Object>] range 範囲
28
+ #
29
+ def initialize(context:, range:)
30
+ @context = context
31
+ @range = Locale::Range.new(range: range)
32
+ end
33
+
34
+ #
35
+ # 不正か
36
+ #
37
+ # @return [True] 不正
38
+ # @return [False] 不正なし
39
+ #
40
+ def invalid?
41
+ range.invalid?
42
+ end
43
+
44
+ #
45
+ # 検索結果を取得する
46
+ #
47
+ # @return [Result::Range] 範囲検索結果(和暦日)
48
+ #
49
+ def get
50
+ start_date = range.start_date
51
+ last_date = range.last_date
52
+
53
+ return western(start_date: start_date, last_date: last_date) if range.valid_western?
54
+
55
+ return japan(start_date: start_date, last_date: last_date) if range.valid_japan?
56
+
57
+ raise Exception.get(
58
+ presets: [
59
+ Exception::Case::Preset.new(
60
+ template: Exception::Case::Pattern::INVALID_RANGE
61
+ )
62
+ ]
63
+ )
64
+ end
65
+
66
+ private
67
+
68
+ #
69
+ # 西暦日による結果を取得する
70
+ #
71
+ # @param [Locale::Date] start_date 西暦開始日
72
+ # @param [Locale::Date] last_date 西暦終了日
73
+ #
74
+ # @return [Result::Range] 範囲検索結果(和暦日)
75
+ #
76
+ def western(start_date: Locale::Date.new, last_date: Locale::Date.new)
77
+ Calculation::Summary::Western::Range.get(
78
+ context: context, start_date: start_date.western_date,
79
+ last_date: last_date.western_date
80
+ )
81
+ end
82
+
83
+ #
84
+ # 和暦日による結果を取得する
85
+ #
86
+ # @param [Locale::Date] start_date 和暦開始日
87
+ # @param [Locale::Date] last_date 和暦終了日
88
+ #
89
+ # @return [Result::Range] 範囲検索結果(和暦日)
90
+ #
91
+ def japan(start_date: Locale::Date.new, last_date: Locale::Date.new)
92
+ Calculation::Summary::Japan::Range.get(
93
+ context: context, start_date: start_date.japan_date,
94
+ last_date: last_date.japan_date
95
+ )
96
+ end
97
+ end
98
+ end
99
+ end
@@ -0,0 +1,81 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative '../calculation/summary/japan/single'
4
+ require_relative '../calculation/summary/western/single'
5
+ require_relative '../exception/case/preset'
6
+ require_relative '../exception/exception'
7
+
8
+ require_relative './locale/date'
9
+
10
+ # :nodoc:
11
+ module Zakuro
12
+ # :nodoc:
13
+ module Gateway
14
+ #
15
+ # Single 1日
16
+ #
17
+ class Single
18
+ # @return [Locale::Date] 日付
19
+ attr_reader :date
20
+
21
+ #
22
+ # 初期化
23
+ #
24
+ # @param [Context::Context] context 暦コンテキスト
25
+ # @param [Date, String] date 日付
26
+ #
27
+ def initialize(context:, date:)
28
+ @context = context
29
+ @date = Locale::Date.new(date: date)
30
+ end
31
+
32
+ #
33
+ # 不正か
34
+ #
35
+ # @return [True] 不正
36
+ # @return [False] 不正なし
37
+ #
38
+ def invalid?
39
+ date.invalid?
40
+ end
41
+
42
+ #
43
+ # 検索結果を取得する
44
+ #
45
+ # @return [Result::Single] 一日検索結果(和暦日)
46
+ #
47
+ def get
48
+ if date.valid_western?
49
+ return Calculation::Summary::Western::Single.get(
50
+ context: @context, date: date.western_date
51
+ )
52
+ end
53
+
54
+ if date.valid_japan?
55
+ return Calculation::Summary::Japan::Single.get(
56
+ context: @context, date: date.japan_date
57
+ )
58
+ end
59
+
60
+ raise invalid_date_error
61
+ end
62
+
63
+ private
64
+
65
+ #
66
+ # 日付不正エラーを取得する
67
+ #
68
+ # @return [ZakuroError] ライブラリエラー
69
+ #
70
+ def invalid_date_error
71
+ Exception.get(
72
+ presets: [
73
+ Exception::Case::Preset.new(
74
+ template: Exception::Case::Pattern::INVALID_DATE
75
+ )
76
+ ]
77
+ )
78
+ end
79
+ end
80
+ end
81
+ end
@@ -1,14 +1,14 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require_relative './era/western/calendar'
3
+ require_relative './gateway/single'
4
4
 
5
- require_relative './calculation/summary/single'
6
-
7
- require_relative './calculation/summary/range'
5
+ require_relative './gateway/range'
8
6
 
9
7
  require_relative './condition'
10
8
 
11
- require_relative './output/error'
9
+ require_relative './exception/exception'
10
+
11
+ require_relative './calculation/type/optional'
12
12
 
13
13
  # :nodoc:
14
14
  module Zakuro
@@ -17,6 +17,8 @@ module Zakuro
17
17
  # 東西の暦を取引する、素敵な笑顔の持ち主
18
18
  #
19
19
  class Merchant
20
+ # @return [Output::Logger] ロガー
21
+ LOGGER = Output::Logger.new(location: Merchant)
20
22
  # @return [Hash<Symbol, Object>] 条件
21
23
  attr_reader :condition
22
24
 
@@ -25,11 +27,17 @@ module Zakuro
25
27
  #
26
28
  # @param [Hash<Symbol, Object>] condition 条件
27
29
  #
30
+ # @raise [Exception::ZakuroError] ライブラリ内エラー
31
+ #
28
32
  def initialize(condition: {})
29
33
  failed = Condition.validate(hash: condition)
30
- raise Output::ZakuroError, failed.join('\n') unless failed.empty?
34
+ raise Exception.get(presets: failed) unless failed.empty?
31
35
 
32
36
  @condition = Condition.new(hash: condition)
37
+ rescue Exception::ZakuroError => e
38
+ raise e
39
+ rescue StandardError => e
40
+ make_internal_error(error: e)
33
41
  end
34
42
 
35
43
  #
@@ -39,13 +47,19 @@ module Zakuro
39
47
  #
40
48
  # @return [Merchant] 自インスタンス
41
49
  #
50
+ # @raise [Exception::ZakuroError] ライブラリ内エラー
51
+ #
42
52
  def offer(condition: {})
43
53
  failed = Condition.validate(hash: condition)
44
- raise Output::ZakuroError, failed.join('\n') unless failed.empty?
54
+ raise Exception.get(presets: failed) unless failed.empty?
45
55
 
46
- @condition.rewrite(hash: condition)
56
+ condition.rewrite(hash: condition)
47
57
 
48
58
  self
59
+ rescue Exception::ZakuroError => e
60
+ raise e
61
+ rescue StandardError => e
62
+ make_internal_error(error: e)
49
63
  end
50
64
 
51
65
  #
@@ -54,52 +68,72 @@ module Zakuro
54
68
  # @return [Result::SingleDay] 和暦日
55
69
  # @return [Result::Range] 和暦日範囲
56
70
  #
71
+ # @raise [Exception::ZakuroError] ライブラリ内エラー
72
+ #
57
73
  def commit
58
- date = condition.date
59
-
60
- return single(date: date) if date
74
+ context = Context::Context.new(options: condition.options)
61
75
 
62
- range = condition.range
76
+ result = get(context: context)
63
77
 
64
- return range(range: range) if range
78
+ return result.get unless result.invalid?
65
79
 
66
- {}
80
+ make_uncommitable_error
81
+ rescue Exception::ZakuroError => e
82
+ raise e
83
+ rescue StandardError => e
84
+ make_internal_error(error: e)
67
85
  end
68
86
 
69
87
  private
70
88
 
71
89
  #
72
- # 1日検索
90
+ # 結果取得する
73
91
  #
74
- # @param [Date] date 西暦日
92
+ # @param [Context::Context] context 暦コンテキスト
75
93
  #
76
- # @return [Result::Single] 検索結果
94
+ # @return [Calculation::Type::Optional] 参照
77
95
  #
78
- def single(date:)
79
- western_date = Western::Calendar.create(date: date)
96
+ def get(context:)
97
+ single = Gateway::Single.new(context: context, date: condition.date)
98
+
99
+ return Calculation::Type::Optional.new(obj: single.get) unless single.invalid?
80
100
 
81
- # TODO: condition で設定する
82
- context = Context.new(version_name: '')
101
+ range = Gateway::Range.new(context: context, range: condition.range)
83
102
 
84
- Calculation::Summary::Single.get(context: context, date: western_date)
103
+ return Calculation::Type::Optional.new(obj: range.get) unless range.invalid?
104
+
105
+ Calculation::Type::Optional.new
85
106
  end
86
107
 
87
108
  #
88
- # 期間検索
109
+ # 内部エラーを生成する
89
110
  #
90
- # @param [Catalog::Range] range 期間
111
+ # @param [StandardError] error 内部エラー
91
112
  #
92
- # @return [Result::Range] 和暦日範囲
113
+ # @raise [Exception::ZakuroError] ライブラリ内エラー
93
114
  #
94
- def range(range:)
95
- start_date = Western::Calendar.create(date: range[:start])
96
- last_date = Western::Calendar.create(date: range[:last])
97
-
98
- # TODO: condition で設定する
99
- context = Context.new(version_name: '')
115
+ def make_internal_error(error:)
116
+ LOGGER.error(error)
117
+ presets = [
118
+ Exception::Case::Preset.new(
119
+ template: Exception::Case::Pattern::INTERNAL_ERROR
120
+ )
121
+ ]
122
+ raise Exception.get(presets: presets)
123
+ end
100
124
 
101
- Calculation::Summary::Range.get(
102
- context: context, start_date: start_date, last_date: last_date
125
+ #
126
+ # 内部エラーを生成する
127
+ #
128
+ # @raise [Exception::ZakuroError] ライブラリ内エラー
129
+ #
130
+ def make_uncommitable_error
131
+ raise Exception.get(
132
+ presets: [
133
+ Exception::Case::Preset.new(
134
+ template: Exception::Case::Pattern::UNCOMMITTABLE_DATE
135
+ )
136
+ ]
103
137
  )
104
138
  end
105
139
  end