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
@@ -0,0 +1,169 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative '../../../../era/western/calendar'
4
+ require_relative '../../../../output/response'
5
+ require_relative '../../../../output/logger'
6
+
7
+ require_relative '../../../base/year'
8
+
9
+ require_relative '../../internal/month'
10
+
11
+ # :nodoc:
12
+ module Zakuro
13
+ # :nodoc:
14
+ module Calculation
15
+ # :nodoc:
16
+ module Summary
17
+ # :nodoc:
18
+ module Western
19
+ # :nodoc:
20
+ module Specifier
21
+ #
22
+ # MultipleDay 複数日検索
23
+ #
24
+ module MultipleDay
25
+ # @return [Output::Logger] ロガー
26
+ LOGGER = Output::Logger.new(location: 'specifier')
27
+
28
+ #
29
+ # 取得する
30
+ #
31
+ # @param [Array<Base::Year>] years 範囲
32
+ # @param [Western::Calendar] start_date 西暦開始日
33
+ # @param [Western::Calendar] last_date 西暦終了日
34
+ #
35
+ # @return [Array<Result::Data::SingleDay>] 期間検索結果(和暦日)
36
+ #
37
+ def self.get(years: [], start_date: Western::Calendar.new,
38
+ last_date: Western::Calendar.new)
39
+ months = specify(years: years, start_date: start_date, last_date: last_date)
40
+
41
+ result = []
42
+ months.each do |month|
43
+ result |= month.get
44
+ end
45
+
46
+ result
47
+ end
48
+
49
+ #
50
+ # 年を特定する
51
+ #
52
+ # @param [Array<Year>] years 範囲
53
+ # @param [Western::Calendar] start_date 西暦開始日
54
+ # @param [Western::Calendar] last_date 西暦終了日
55
+ #
56
+ # @return [Array<Month>] 特定月
57
+ #
58
+ def self.specify(years: [], start_date: Western::Calendar.new,
59
+ last_date: Western::Calendar.new)
60
+ result = []
61
+ years.each do |year|
62
+ result |= specify_month(
63
+ year: year, start_date: start_date, last_date: last_date
64
+ )
65
+ end
66
+
67
+ result
68
+ end
69
+ private_class_method :specify
70
+
71
+ # :reek:TooManyStatements { max_statements: 7 }
72
+
73
+ #
74
+ # 月を特定する
75
+ #
76
+ # @param [Year] year 年
77
+ # @param [Western::Calendar] start_date 西暦開始日
78
+ # @param [Western::Calendar] last_date 西暦終了日
79
+ #
80
+ # @return [Month] 対象月
81
+ #
82
+ def self.specify_month(year:, start_date: Western::Calendar.new,
83
+ last_date: Western::Calendar.new)
84
+ months = year.months
85
+
86
+ specify_months = []
87
+ months.each do |month|
88
+ next unless include?(month: month, start_date: start_date, last_date: last_date)
89
+
90
+ monthly_start_date = month.western_date.clone
91
+ monthly_last_date = month.last_date.clone
92
+
93
+ monthly_start_date = start_date.clone if start_date > monthly_start_date
94
+
95
+ monthly_last_date = last_date.clone if last_date < monthly_last_date
96
+
97
+ specify_months.push(
98
+ Month.new(year: year, month: month,
99
+ start_date: monthly_start_date, last_date: monthly_last_date)
100
+ )
101
+ end
102
+
103
+ specify_months
104
+ end
105
+ private_class_method :specify_month
106
+
107
+ #
108
+ # 月が範囲に含まれるか
109
+ #
110
+ # @param [Monthly::Month] month 月
111
+ # @param [Western::Calendar] start_date 西暦開始日
112
+ # @param [Western::Calendar] last_date 西暦終了日
113
+ #
114
+ # @return [True] 含む
115
+ # @return [False] 含まない
116
+ #
117
+ def self.include?(month:, start_date:, last_date:)
118
+ monthly_start_date = month.western_date.clone
119
+ monthly_last_date = month.last_date.clone
120
+
121
+ return false if monthly_start_date.invalid?
122
+
123
+ return false if monthly_last_date.invalid?
124
+
125
+ return false if under(monthly_start_date: monthly_start_date,
126
+ start_date: start_date, last_date: last_date)
127
+
128
+ return false if over(monthly_last_date: monthly_last_date,
129
+ start_date: start_date, last_date: last_date)
130
+
131
+ true
132
+ end
133
+ private_class_method :include?
134
+
135
+ #
136
+ # 範囲が月より前にあるか
137
+ #
138
+ # @param [Western::Calendar] monthly_start_date 月初日
139
+ # @param [Western::Calendar] start_date 西暦開始日
140
+ # @param [Western::Calendar] last_date 西暦終了日
141
+ #
142
+ # @return [True] 前にある
143
+ # @return [False] 前にない
144
+ #
145
+ def self.under(monthly_start_date:, start_date:, last_date:)
146
+ start_date < monthly_start_date && last_date < monthly_start_date
147
+ end
148
+ private_class_method :under
149
+
150
+ #
151
+ # 範囲が月より後にあるか
152
+ #
153
+ # @param [Western::Calendar] monthly_start_date 月末日
154
+ # @param [Western::Calendar] start_date 西暦開始日
155
+ # @param [Western::Calendar] last_date 西暦終了日
156
+ #
157
+ # @return [True] 後にある
158
+ # @return [False] 後にない
159
+ #
160
+ def self.over(monthly_last_date:, start_date:, last_date:)
161
+ start_date > monthly_last_date && last_date > monthly_last_date
162
+ end
163
+ private_class_method :over
164
+ end
165
+ end
166
+ end
167
+ end
168
+ end
169
+ end
@@ -0,0 +1,93 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative '../../../../era/western/calendar'
4
+ require_relative '../../../../output/response'
5
+ require_relative '../../../../output/logger'
6
+
7
+ require_relative '../../../base/year'
8
+
9
+ # :nodoc:
10
+ module Zakuro
11
+ # :nodoc:
12
+ module Calculation
13
+ # :nodoc:
14
+ module Summary
15
+ # :nodoc:
16
+ module Western
17
+ # :nodoc:
18
+ module Specifier
19
+ #
20
+ # SingleDay 一日検索
21
+ #
22
+ module SingleDay
23
+ # @return [Output::Logger] ロガー
24
+ LOGGER = Output::Logger.new(location: 'specifier')
25
+
26
+ #
27
+ # 取得する
28
+ #
29
+ # @param [Array<Year>] yeas 範囲
30
+ # @param [Western::Calendar] date 西暦日
31
+ #
32
+ # @return [Result::Data::SingleDay] 和暦日
33
+ #
34
+ def self.get(years: [], date: Western::Calendar.new)
35
+ year, month = specify(years: years, date: date)
36
+ first_date = month.western_date
37
+
38
+ Output::Response::SingleDay.save_single_day(
39
+ param: Output::Response::SingleDay::Param.new(
40
+ year: year, month: month,
41
+ date: date, days: date - first_date
42
+ )
43
+ )
44
+ end
45
+
46
+ #
47
+ # 年を特定する
48
+ #
49
+ # @param [Array<Year>] years 範囲
50
+ # @param [Western::Calendar] date 西暦日
51
+ #
52
+ # @return [Base::Year] 対象年
53
+ # @return [Monthly::Month] 対象月
54
+ #
55
+ def self.specify(years:, date:)
56
+ years.each do |year|
57
+ month = specify_month(year: year, date: date)
58
+ return year, month unless month.invalid?
59
+ end
60
+
61
+ raise ArgumentError, "invalid year range. date: #{date.format}"
62
+ end
63
+ private_class_method :specify
64
+
65
+ # :reek:TooManyStatements { max_statements: 7 }
66
+
67
+ #
68
+ # 月を特定する
69
+ #
70
+ # @param [Base::Year] year 年
71
+ # @param [Western::Calendar] date 西暦日
72
+ #
73
+ # @return [Monthly::Month] 対象月
74
+ #
75
+ def self.specify_month(year:, date:)
76
+ months = year.months
77
+
78
+ months.each do |month|
79
+ western_date = month.western_date
80
+ next if western_date.invalid?
81
+
82
+ return month if month.include?(date: date)
83
+ end
84
+
85
+ Monthly::Month.new
86
+ end
87
+ private_class_method :specify_month
88
+ end
89
+ end
90
+ end
91
+ end
92
+ end
93
+ end
@@ -18,10 +18,9 @@ module Zakuro
18
18
  #
19
19
  # 初期化
20
20
  #
21
- # @param [Date] date 西暦日
21
+ # @param [Date, String] date 西暦日/和暦日
22
22
  #
23
23
  def initialize(date:)
24
- # TODO: 和暦日(String)から西暦日(Date)への変換
25
24
  @date = date
26
25
  end
27
26
 
@@ -6,98 +6,113 @@ require_relative './line'
6
6
 
7
7
  # :nodoc:
8
8
  module Zakuro
9
- #
10
- # Japan 和暦
11
- #
9
+ # :nodoc:
12
10
  module Japan
13
- #
14
- # Alignment 整列
15
- #
16
- module Alignment
11
+ # :nodoc:
12
+ module Gengou
17
13
  #
18
- # Aligner 元号整列
14
+ # Alignment 整列
19
15
  #
20
- class Aligner
21
- # @return [Integer] 1行目元号
22
- FIRST_LINE = 0
16
+ module Alignment
17
+ #
18
+ # Aligner 元号整列
19
+ #
20
+ class Aligner
21
+ # @return [Integer] 1行目元号
22
+ FIRST_LINE = 0
23
23
 
24
- # @return [Integer] 2行目元号
25
- SECOND_LINE = 1
24
+ # @return [Integer] 2行目元号
25
+ SECOND_LINE = 1
26
26
 
27
- # @return [Array<Integer>] 元号リスト
28
- LINE_INDEXES = [FIRST_LINE, SECOND_LINE].freeze
27
+ # @return [Array<Integer>] 元号リスト
28
+ LINE_INDEXES = [FIRST_LINE, SECOND_LINE].freeze
29
29
 
30
- # @return [Integer] 行数
31
- LINE_SIZE = LINE_INDEXES.size
30
+ # @return [Integer] 行数
31
+ LINE_SIZE = LINE_INDEXES.size
32
32
 
33
- # @return [Array<Line>] 行元号
34
- attr_reader :lines
33
+ # @return [Array<Line>] 行元号
34
+ attr_reader :lines
35
35
 
36
- #
37
- # 初期化
38
- #
39
- # @param [Array<Set>] resources 元号解析結果
40
- #
41
- def initialize(resources: [])
42
- @lines = []
43
- (1..LINE_SIZE).each do |_index|
44
- @lines.push(Line.new)
36
+ #
37
+ # 初期化
38
+ #
39
+ # @param [Array<Set>] resources 元号解析結果
40
+ #
41
+ def initialize(resources: [])
42
+ @lines = []
43
+ (1..LINE_SIZE).each do |_index|
44
+ @lines.push(Line.new)
45
+ end
46
+
47
+ save(resources: resources)
45
48
  end
46
49
 
47
- save(resources: resources)
48
- end
50
+ #
51
+ # 行元号に追加する
52
+ #
53
+ # @param [Set] set 元号セット
54
+ #
55
+ def push(set:)
56
+ list = set.list
57
+ list.each do |gengou|
58
+ push_gengou(gengou: gengou)
59
+ end
60
+ end
49
61
 
50
- #
51
- # 行元号に追加する
52
- #
53
- # @param [Set] set 元号セット
54
- #
55
- def push(set:)
56
- list = set.list
57
- list.each do |gengou|
58
- push_gengou(gengou: gengou)
62
+ #
63
+ # 指定した範囲内の元号を取得する
64
+ #
65
+ # @param [Integer] line
66
+ # @param [Western::Calendar] start_date 開始日
67
+ # @param [Western::Calendar] last_date 終了日
68
+ #
69
+ # @return [Array<LinearGengou>] 元号
70
+ #
71
+ def get(line:, start_date:, last_date:)
72
+ raise ArgumentError.new, 'invalid line number' unless LINE_INDEXES.include?(line)
73
+
74
+ @lines[line].get(start_date: start_date, last_date: last_date)
59
75
  end
60
- end
61
76
 
62
- #
63
- # 指定した範囲内の元号を取得する
64
- #
65
- # @param [Integer] line 行
66
- # @param [Western::Calendar] start_date 開始日
67
- # @param [Western::Calendar] last_date 終了日
68
- #
69
- # @return [Array<LinearGengou>] 元号
70
- #
71
- def get(line:, start_date:, last_date:)
72
- raise ArgumentError.new, 'invalid line number' unless LINE_INDEXES.include?(line)
77
+ #
78
+ # 指定した範囲内の元号を取得する(元号名)
79
+ #
80
+ # @param [Integer] line 行
81
+ # @param [String] name 元号名
82
+ #
83
+ # @return [Array<LinearGengou>] 元号
84
+ #
85
+ def get_by_name(line:, name:)
86
+ raise ArgumentError.new, 'invalid line number' unless LINE_INDEXES.include?(line)
73
87
 
74
- @lines[line].get(start_date: start_date, last_date: last_date)
75
- end
88
+ @lines[line].get_by_name(name: name)
89
+ end
76
90
 
77
- private
91
+ private
78
92
 
79
- #
80
- # 保存する
81
- #
82
- # @param [Array<Set>] resources 元号解析結果
83
- #
84
- def save(resources: [])
85
- resources.each do |set|
86
- push(set: set)
93
+ #
94
+ # 保存する
95
+ #
96
+ # @param [Array<Set>] resources 元号解析結果
97
+ #
98
+ def save(resources: [])
99
+ resources.each do |set|
100
+ push(set: set)
101
+ end
87
102
  end
88
- end
89
103
 
90
- #
91
- # 元号を追加する
92
- #
93
- # @param [Gengou] gengou 元号
94
- #
95
- def push_gengou(gengou:)
96
- rest = [
97
- LinearGengou.new(gengou: gengou)
98
- ]
99
- @lines.each do |line|
100
- rest = line.push(list: rest)
104
+ #
105
+ # 元号を追加する
106
+ #
107
+ # @param [Gengou] gengou 元号
108
+ #
109
+ def push_gengou(gengou:)
110
+ rest = [
111
+ LinearGengou.new(gengou: gengou)
112
+ ]
113
+ @lines.each do |line|
114
+ rest = line.push(list: rest)
115
+ end
101
116
  end
102
117
  end
103
118
  end