zakuro 0.0.3 → 0.1.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (79) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +102 -42
  3. data/doc/operation.md +25 -0
  4. data/doc/operation/csv/month.csv +202 -0
  5. data/doc/operation/operation.xlsx +0 -0
  6. data/doc/operation/transfer.rb +77 -0
  7. data/lib/zakuro/calculation/base/multi_gengou.rb +101 -0
  8. data/lib/zakuro/calculation/base/multi_gengou_roller.rb +218 -0
  9. data/lib/zakuro/calculation/base/year.rb +107 -0
  10. data/lib/zakuro/calculation/cycle/abstract_remainder.rb +459 -0
  11. data/lib/zakuro/calculation/cycle/abstract_solar_term.rb +173 -0
  12. data/lib/zakuro/calculation/cycle/zodiac.rb +106 -0
  13. data/lib/zakuro/calculation/monthly/first_day.rb +45 -0
  14. data/lib/zakuro/calculation/monthly/initialized_month.rb +125 -0
  15. data/lib/zakuro/calculation/monthly/month.rb +187 -0
  16. data/lib/zakuro/calculation/monthly/month_label.rb +88 -0
  17. data/lib/zakuro/calculation/monthly/operated_month.rb +209 -0
  18. data/lib/zakuro/calculation/range/full_range.rb +210 -0
  19. data/lib/zakuro/calculation/range/operated_range.rb +144 -0
  20. data/lib/zakuro/calculation/range/operated_solar_terms.rb +201 -0
  21. data/lib/zakuro/calculation/range/transfer/western_date_allocation.rb +82 -0
  22. data/lib/zakuro/calculation/range/transfer/year_boundary.rb +146 -0
  23. data/lib/zakuro/calculation/specifier/single_day.rb +109 -0
  24. data/lib/zakuro/calculation/summary/single.rb +129 -0
  25. data/lib/zakuro/condition.rb +16 -13
  26. data/lib/zakuro/era/japan/gengou.rb +106 -0
  27. data/lib/zakuro/era/japan/gengou/parser.rb +167 -0
  28. data/lib/zakuro/era/japan/gengou/type.rb +178 -0
  29. data/lib/zakuro/era/japan/gengou/validator.rb +236 -0
  30. data/lib/zakuro/era/japan/reki.rb +91 -0
  31. data/lib/zakuro/era/{gengou → japan/yaml}/set-001-until-south.yaml +0 -0
  32. data/lib/zakuro/era/{gengou → japan/yaml}/set-002-from-north.yaml +0 -0
  33. data/lib/zakuro/era/{gengou → japan/yaml}/set-003-modern.yaml +0 -0
  34. data/lib/zakuro/era/western.rb +1 -1
  35. data/lib/zakuro/merchant.rb +2 -2
  36. data/lib/zakuro/operation/month/parser.rb +373 -0
  37. data/lib/zakuro/operation/month/type.rb +453 -0
  38. data/lib/zakuro/operation/month/validator.rb +802 -0
  39. data/lib/zakuro/operation/operation.rb +66 -0
  40. data/lib/zakuro/operation/yaml/month.yaml +6452 -0
  41. data/lib/zakuro/output/error.rb +7 -4
  42. data/lib/zakuro/output/logger.rb +50 -47
  43. data/lib/zakuro/output/response.rb +146 -143
  44. data/lib/zakuro/result/core.rb +52 -0
  45. data/lib/zakuro/result/data.rb +187 -0
  46. data/lib/zakuro/result/operation.rb +114 -0
  47. data/lib/zakuro/result/result.rb +37 -0
  48. data/lib/zakuro/{output → tools}/stringifier.rb +16 -9
  49. data/lib/zakuro/tools/typeof.rb +33 -0
  50. data/lib/zakuro/version.rb +1 -1
  51. data/lib/zakuro/version/abstract_version.rb +1 -1
  52. data/lib/zakuro/version/context.rb +23 -0
  53. data/lib/zakuro/version/senmyou/cycle/remainder.rb +63 -0
  54. data/lib/zakuro/version/senmyou/cycle/solar_term.rb +31 -0
  55. data/lib/zakuro/version/senmyou/monthly/lunar_phase.rb +186 -182
  56. data/lib/zakuro/version/senmyou/range/annual_range.rb +167 -0
  57. data/lib/zakuro/version/senmyou/senmyou.rb +10 -4
  58. data/lib/zakuro/version/senmyou/stella/lunar_orbit.rb +6 -6
  59. data/lib/zakuro/version/senmyou/stella/solar_average.rb +103 -152
  60. data/lib/zakuro/version/senmyou/stella/solar_location.rb +213 -0
  61. data/lib/zakuro/version/senmyou/stella/solar_orbit.rb +3 -184
  62. data/lib/zakuro/version/senmyou/stella/winter_solstice.rb +4 -4
  63. data/lib/zakuro/version/version_class_resolver.rb +62 -0
  64. data/lib/zakuro/version_factory.rb +3 -3
  65. metadata +53 -24
  66. data/lib/zakuro/cycle/abstract_remainder.rb +0 -456
  67. data/lib/zakuro/cycle/zodiac.rb +0 -103
  68. data/lib/zakuro/era/japan.rb +0 -664
  69. data/lib/zakuro/output/result.rb +0 -225
  70. data/lib/zakuro/version/senmyou/base/era.rb +0 -83
  71. data/lib/zakuro/version/senmyou/base/multi_gengou.rb +0 -98
  72. data/lib/zakuro/version/senmyou/base/multi_gengou_roller.rb +0 -205
  73. data/lib/zakuro/version/senmyou/base/remainder.rb +0 -60
  74. data/lib/zakuro/version/senmyou/base/solar_term.rb +0 -66
  75. data/lib/zakuro/version/senmyou/base/year.rb +0 -104
  76. data/lib/zakuro/version/senmyou/monthly/month.rb +0 -122
  77. data/lib/zakuro/version/senmyou/summary/annual_range.rb +0 -187
  78. data/lib/zakuro/version/senmyou/summary/full_range.rb +0 -224
  79. data/lib/zakuro/version/senmyou/summary/specifier.rb +0 -100
@@ -0,0 +1,187 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative './first_day'
4
+ require_relative './month_label'
5
+
6
+ # :nodoc:
7
+ module Zakuro
8
+ # :nodoc:
9
+ module Calculation
10
+ # :nodoc:
11
+ module Monthly
12
+ #
13
+ # Month 月情報
14
+ #
15
+ class Month
16
+ # @return [Context] 暦コンテキスト
17
+ attr_reader :context
18
+ # @return [MonthLabel] 月表示名
19
+ attr_reader :month_label
20
+ # @return [FirstDay] 月初日(朔日)
21
+ attr_reader :first_day
22
+ # @return [Array<SolarTerm>] 二十四節気
23
+ attr_reader :solar_terms
24
+
25
+ #
26
+ # 初期化
27
+ #
28
+ # @param [Context] context 暦コンテキスト
29
+ # @param [MonthLabel] month_label 月表示名
30
+ # @param [FirstDay] first_day 月初日(朔日)
31
+ # @param [Array<SolarTerm>] solar_terms 二十四節気
32
+ #
33
+ def initialize(context:, month_label: MonthLabel.new, first_day: FirstDay.new,
34
+ solar_terms: [])
35
+ @context = context
36
+ @month_label = month_label
37
+ @first_day = first_day
38
+ @solar_terms = solar_terms
39
+ end
40
+
41
+ #
42
+ # 中気なしは閏月とする
43
+ #
44
+ def eval_leaped
45
+ leaped = even_term.invalid?
46
+
47
+ @month_label = MonthLabel.new(number: number, is_many_days: many_days?, leaped: leaped)
48
+ end
49
+
50
+ #
51
+ # 月初日の西暦日を返す
52
+ #
53
+ # @return [Western::Calendar] 西暦日
54
+ #
55
+ def western_date
56
+ @first_day.western_date
57
+ end
58
+
59
+ #
60
+ # 月初日の大余小余を返す
61
+ #
62
+ # @return [Remainder] 大余小余
63
+ #
64
+ def remainder
65
+ @first_day.remainder
66
+ end
67
+
68
+ #
69
+ # 月の大小を返す
70
+ #
71
+ # @return [True] 大の月(30日)
72
+ # @return [False] 小の月(29日)
73
+ #
74
+ def many_days?
75
+ @month_label.is_many_days
76
+ end
77
+
78
+ #
79
+ # 月の日数を返す
80
+ #
81
+ # @return [Integer] 日数
82
+ #
83
+ def days
84
+ @month_label.days
85
+ end
86
+
87
+ #
88
+ # 月の名前(大小)を返す
89
+ #
90
+ # @return [String] 月の名前(大小)
91
+ #
92
+ def days_name
93
+ @month_label.days_name
94
+ end
95
+
96
+ #
97
+ # 月を返す
98
+ #
99
+ # @return [Integer] 月(xx月のxx)
100
+ #
101
+ def number
102
+ @month_label.number
103
+ end
104
+
105
+ #
106
+ # 閏を返す
107
+ #
108
+ # @return [True] 閏月
109
+ # @return [False] 平月
110
+ #
111
+ def leaped?
112
+ @month_label.leaped
113
+ end
114
+
115
+ #
116
+ # 次月の大余から月の日数を定める
117
+ #
118
+ # @param [Integer] next_month_day 次月の大余
119
+ #
120
+ def eval_many_days(next_month_day:)
121
+ is_many_days = remainder.same_remainder_divided_by_ten?(other: next_month_day)
122
+
123
+ @month_label = MonthLabel.new(
124
+ number: number, is_many_days: is_many_days, leaped: leaped?
125
+ )
126
+ end
127
+
128
+ #
129
+ # 二十四節気が未設定かどうかを検証する
130
+ #
131
+ # @return [True] 設定なし
132
+ # @return [False] 設定あり
133
+ #
134
+ def empty_solar_term?
135
+ @solar_terms.empty?
136
+ end
137
+
138
+ #
139
+ # 中気を返す
140
+ #
141
+ # @return [SolarTerm] 中気
142
+ #
143
+ def even_term
144
+ @solar_terms.each do |term|
145
+ return term if term.index.even?
146
+ end
147
+
148
+ context.resolver.solar_term.new
149
+ end
150
+
151
+ #
152
+ # 節気を返す
153
+ #
154
+ # @return [SolarTerm] 節気
155
+ #
156
+ def odd_term
157
+ @solar_terms.each do |term|
158
+ return term if term.index.odd?
159
+ end
160
+
161
+ context.resolver.solar_term.new
162
+ end
163
+
164
+ #
165
+ # 二十四節気を追加する
166
+ #
167
+ # @param [SolarTerm] term 二十四節気
168
+ #
169
+ def add_term(term:)
170
+ @solar_terms.push(term)
171
+ end
172
+
173
+ #
174
+ # 同一の月情報かを検証する
175
+ #
176
+ # @param [Month] other 他の月情報
177
+ #
178
+ # @return [True] 同一の月
179
+ # @return [False] 異なる月
180
+ #
181
+ def same?(other:)
182
+ number == other.number && leaped? == other.leaped?
183
+ end
184
+ end
185
+ end
186
+ end
187
+ end
@@ -0,0 +1,88 @@
1
+ # frozen_string_literal: true
2
+
3
+ # :nodoc:
4
+ module Zakuro
5
+ # :nodoc:
6
+ module Calculation
7
+ # :nodoc:
8
+ module Monthly
9
+ #
10
+ # MonthLabel 月表示情報
11
+ #
12
+ class MonthLabel
13
+ # @return [True] 大の月(30日)
14
+ # @return [False] 小の月(29日)
15
+ attr_reader :is_many_days
16
+ # @return [Integer] 月(xx月のxx)
17
+ attr_reader :number
18
+ # @return [True] 閏月
19
+ # @return [False] 平月
20
+ attr_reader :leaped
21
+
22
+ # :reek:ControlParameter and :reek:BooleanParameter
23
+
24
+ #
25
+ # 初期化
26
+ #
27
+ # @param [Integer] number 月(xx月のxx)
28
+ # @param [True, False] is_many_days 月の大小
29
+ # @param [True, False] leaped 閏月/平月
30
+ #
31
+ def initialize(number: -1, is_many_days: false, leaped: false)
32
+ # 月の大小
33
+ @is_many_days = is_many_days
34
+ # 月
35
+ @number = number
36
+ # 閏
37
+ @leaped = leaped
38
+ end
39
+
40
+ #
41
+ # 月の日数を返す
42
+ #
43
+ # @return [Integer] 日数
44
+ #
45
+ def days
46
+ @is_many_days ? 30 : 29
47
+ end
48
+
49
+ #
50
+ # 月の名前(大小)を返す
51
+ #
52
+ # @return [String] 月の名前(大小)
53
+ #
54
+ def days_name
55
+ @is_many_days ? '大' : '小'
56
+ end
57
+
58
+ #
59
+ # 一ヶ月戻す
60
+ #
61
+ # @return [True] 昨年
62
+ # @return [False] 今年
63
+ #
64
+ def back_to_last_month
65
+ @number -= 1
66
+
67
+ return false if @number.positive?
68
+
69
+ @number = 12
70
+
71
+ true
72
+ end
73
+
74
+ #
75
+ # 同一の月情報かを検証する
76
+ #
77
+ # @param [Month] other 他の月情報
78
+ #
79
+ # @return [True] 同一の月
80
+ # @return [False] 異なる月
81
+ #
82
+ def same?(other:)
83
+ @number == other.number && @leaped == other.leaped
84
+ end
85
+ end
86
+ end
87
+ end
88
+ end
@@ -0,0 +1,209 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative './month'
4
+ require_relative '../../operation/operation'
5
+
6
+ # :nodoc:
7
+ module Zakuro
8
+ # :nodoc:
9
+ module Calculation
10
+ # :nodoc:
11
+ module Monthly
12
+ # :reek:TooManyInstanceVariables { max_instance_variables: 5 }
13
+
14
+ #
15
+ # OperatedMonth 月情報(運用)
16
+ #
17
+ class OperatedMonth < Month
18
+ # @return [Operation::MonthHistory] 変更履歴(月)
19
+ attr_reader :history
20
+ # @return [OperatedSolarTerms] 運用時二十四節気
21
+ attr_reader :operated_solar_terms
22
+
23
+ # :reek:LongParameterList {max_params: 6}
24
+ # rubocop:disable Metrics/ParameterLists
25
+
26
+ #
27
+ # 初期化
28
+ #
29
+ # @param [Context] context 暦コンテキスト
30
+ # @param [OperatedSolarTerms] operated_solar_terms 運用時二十四節気
31
+ # @param [MonthLabel] month_label 月表示名
32
+ # @param [FirstDay] first_day 月初日(朔日)
33
+ # @param [Array<SolarTerm>] solar_terms 二十四節気
34
+ # @param [Operation::MonthHistory] history 変更履歴(月)
35
+ #
36
+ def initialize(context:, operated_solar_terms:, month_label: MonthLabel.new,
37
+ first_day: FirstDay.new, solar_terms: [],
38
+ history: Operation::MonthHistory.new)
39
+ super(context: context, month_label: month_label, first_day: first_day,
40
+ solar_terms: solar_terms)
41
+ @history = history
42
+ @operated_solar_terms = operated_solar_terms
43
+ end
44
+
45
+ # rubocop:enable Metrics/ParameterLists
46
+
47
+ #
48
+ # 書き換える
49
+ #
50
+ def rewrite
51
+ rewrite_month
52
+ rewrite_solar_terms
53
+ rewrite_first_day
54
+ end
55
+
56
+ #
57
+ # 月ごとの差分で書き換える
58
+ #
59
+ def rewrite_month
60
+ diff = history.diffs.month
61
+
62
+ @month_label = MonthLabel.new(
63
+ number: OperatedMonth.rewrite_month_fields(
64
+ month_diff: diff, month_label: month_label, name: 'number'
65
+ ),
66
+ is_many_days: OperatedMonth.rewrite_month_fields(
67
+ month_diff: diff, month_label: month_label, name: 'is_many_days'
68
+ ),
69
+ leaped: OperatedMonth.rewrite_month_fields(
70
+ month_diff: diff, month_label: month_label, name: 'leaped'
71
+ )
72
+ )
73
+ end
74
+
75
+ def self.rewrite_month_fields(month_diff:, month_label:, name:)
76
+ method_name = name.intern
77
+
78
+ diff = month_diff.send(method_name)
79
+ default = month_label.send(method_name)
80
+ return default if diff.invalid?
81
+
82
+ diff.actual
83
+ end
84
+
85
+ #
86
+ # 二十四節気ごとの差分で書き換える
87
+ #
88
+ def rewrite_solar_terms
89
+ matched, operated_solar_term = @operated_solar_terms.get(
90
+ western_date: first_day.western_date
91
+ )
92
+
93
+ return unless matched
94
+
95
+ @solar_terms = OperatedMonth.create_operated_solar_terms(
96
+ operated_solar_term: operated_solar_term,
97
+ solar_terms: solar_terms
98
+ )
99
+ end
100
+
101
+ #
102
+ # <Description>
103
+ #
104
+ # @param [<Type>] operated_solar_term <description>
105
+ # @param [Array<SolarTerm>] solar_terms 二十四節気
106
+ #
107
+ # @return [Array<SolarTerm>] 二十四節気
108
+ #
109
+ def self.create_operated_solar_terms(operated_solar_term:, solar_terms:)
110
+ index = operated_solar_term.index
111
+
112
+ # 別の月に移動する二十四節気。割り当てない。
113
+ result = remove_solar_term(index: index, solar_terms: solar_terms)
114
+
115
+ return result if used_solar_term?(index: index, solar_terms: solar_terms)
116
+
117
+ result.push(operated_solar_term)
118
+
119
+ result
120
+ end
121
+
122
+ # :reek:ControlParameter
123
+
124
+ #
125
+ # 二十四節気が使用されているか
126
+ #
127
+ # @param [Integer] index 二十四節気番号
128
+ # @param [Array<SolarTerm>] solar_terms 二十四節気
129
+ #
130
+ # @return [True] 使用
131
+ # @return [False] 未使用
132
+ #
133
+ def self.used_solar_term?(index:, solar_terms:)
134
+ solar_terms.each do |solar_term|
135
+ return true if index == solar_term.index
136
+ end
137
+
138
+ false
139
+ end
140
+
141
+ # :reek:ControlParameter
142
+
143
+ #
144
+ # 対象の二十四節気を除外する
145
+ #
146
+ # @param [Integer] index 二十四節気番号
147
+ # @param [Array<SolarTerm>] solar_terms 二十四節気
148
+ #
149
+ # @return [Array<SolarTerm>] 二十四節気(対象除外済)
150
+ #
151
+ def self.remove_solar_term(index:, solar_terms:)
152
+ result = []
153
+ solar_terms.each do |solar_term|
154
+ next if index == solar_term.index
155
+
156
+ result.push(solar_term)
157
+ end
158
+
159
+ result
160
+ end
161
+
162
+ #
163
+ # 月初日ごとの差分で書き換える
164
+ #
165
+ def rewrite_first_day
166
+ diffs = @history.diffs
167
+ return if diffs.invalid_days?
168
+
169
+ days = diffs.days
170
+
171
+ @first_day = FirstDay.new(
172
+ western_date: rewrite_western_date(days: days),
173
+ remainder: rewrite_remainder(days: days)
174
+ )
175
+ end
176
+
177
+ #
178
+ # 月初日の大余小余を日差分で書き換える
179
+ #
180
+ # @param [Integer] days 日差分
181
+ #
182
+ # @return [Remainder] 月初日の大余小余
183
+ #
184
+ def rewrite_remainder(days:)
185
+ remainder = first_day.remainder.clone
186
+ remainder.add!(
187
+ context.resolver.remainder.new(day: days, minute: 0, second: 0)
188
+ )
189
+
190
+ remainder
191
+ end
192
+
193
+ #
194
+ # 月初日の西暦日を日差分で書き換える
195
+ #
196
+ # @param [Integer] days 日差分
197
+ #
198
+ # @return [Western::Calendar] 月初日の西暦日
199
+ #
200
+ def rewrite_western_date(days:)
201
+ western_date = first_day.western_date.clone
202
+ western_date += days
203
+
204
+ western_date
205
+ end
206
+ end
207
+ end
208
+ end
209
+ end