zakuro 0.1.2 → 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (60) hide show
  1. checksums.yaml +4 -4
  2. data/lib/zakuro/calculation/base/multi_gengou.rb +101 -0
  3. data/lib/zakuro/calculation/base/multi_gengou_roller.rb +218 -0
  4. data/lib/zakuro/calculation/base/year.rb +107 -0
  5. data/lib/zakuro/calculation/cycle/abstract_remainder.rb +459 -0
  6. data/lib/zakuro/calculation/cycle/abstract_solar_term.rb +151 -0
  7. data/lib/zakuro/calculation/cycle/zodiac.rb +106 -0
  8. data/lib/zakuro/calculation/monthly/first_day.rb +45 -0
  9. data/lib/zakuro/calculation/monthly/initialized_month.rb +120 -0
  10. data/lib/zakuro/calculation/monthly/month.rb +184 -0
  11. data/lib/zakuro/calculation/monthly/month_label.rb +88 -0
  12. data/lib/zakuro/calculation/monthly/operated_month.rb +201 -0
  13. data/lib/zakuro/calculation/range/full_range.rb +210 -0
  14. data/lib/zakuro/calculation/range/operated_range.rb +134 -0
  15. data/lib/zakuro/calculation/range/operated_solar_terms.rb +201 -0
  16. data/lib/zakuro/calculation/range/transfer/western_date_allocation.rb +76 -0
  17. data/lib/zakuro/calculation/range/transfer/year_boundary.rb +142 -0
  18. data/lib/zakuro/calculation/specifier/single_day.rb +109 -0
  19. data/lib/zakuro/calculation/summary/single.rb +129 -0
  20. data/lib/zakuro/merchant.rb +2 -2
  21. data/lib/zakuro/output/error.rb +7 -6
  22. data/lib/zakuro/output/logger.rb +50 -49
  23. data/lib/zakuro/output/response.rb +145 -144
  24. data/lib/zakuro/tools/typeof.rb +2 -2
  25. data/lib/zakuro/version.rb +1 -1
  26. data/lib/zakuro/version/abstract_version.rb +1 -1
  27. data/lib/zakuro/version/context.rb +23 -0
  28. data/lib/zakuro/version/senmyou/cycle/remainder.rb +63 -0
  29. data/lib/zakuro/version/senmyou/cycle/solar_term.rb +31 -0
  30. data/lib/zakuro/version/senmyou/monthly/lunar_phase.rb +186 -182
  31. data/lib/zakuro/version/senmyou/range/annual_range.rb +134 -129
  32. data/lib/zakuro/version/senmyou/senmyou.rb +10 -4
  33. data/lib/zakuro/version/senmyou/stella/lunar_orbit.rb +5 -5
  34. data/lib/zakuro/version/senmyou/stella/solar_average.rb +4 -4
  35. data/lib/zakuro/version/senmyou/stella/solar_location.rb +27 -27
  36. data/lib/zakuro/version/senmyou/stella/solar_orbit.rb +2 -2
  37. data/lib/zakuro/version/senmyou/stella/winter_solstice.rb +4 -4
  38. data/lib/zakuro/version/version_class_resolver.rb +62 -0
  39. data/lib/zakuro/version_factory.rb +2 -2
  40. metadata +24 -22
  41. data/lib/zakuro/cycle/abstract_remainder.rb +0 -456
  42. data/lib/zakuro/cycle/zodiac.rb +0 -103
  43. data/lib/zakuro/version/senmyou/base/era.rb +0 -83
  44. data/lib/zakuro/version/senmyou/base/multi_gengou.rb +0 -98
  45. data/lib/zakuro/version/senmyou/base/multi_gengou_roller.rb +0 -217
  46. data/lib/zakuro/version/senmyou/base/remainder.rb +0 -60
  47. data/lib/zakuro/version/senmyou/base/solar_term.rb +0 -148
  48. data/lib/zakuro/version/senmyou/base/year.rb +0 -104
  49. data/lib/zakuro/version/senmyou/monthly/first_day.rb +0 -44
  50. data/lib/zakuro/version/senmyou/monthly/initialized_month.rb +0 -119
  51. data/lib/zakuro/version/senmyou/monthly/month.rb +0 -181
  52. data/lib/zakuro/version/senmyou/monthly/month_label.rb +0 -87
  53. data/lib/zakuro/version/senmyou/monthly/operated_month.rb +0 -196
  54. data/lib/zakuro/version/senmyou/range/full_range.rb +0 -194
  55. data/lib/zakuro/version/senmyou/range/operated_range.rb +0 -126
  56. data/lib/zakuro/version/senmyou/range/operated_solar_terms.rb +0 -181
  57. data/lib/zakuro/version/senmyou/range/western_date_allocation.rb +0 -68
  58. data/lib/zakuro/version/senmyou/range/year_boundary.rb +0 -138
  59. data/lib/zakuro/version/senmyou/specifier/single_day_specifier.rb +0 -102
  60. data/lib/zakuro/version/senmyou/summary/single.rb +0 -125
@@ -0,0 +1,134 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative './operated_solar_terms'
4
+ require_relative '../../operation/operation'
5
+ require_relative '../../calculation/monthly/operated_month'
6
+
7
+ # :nodoc:
8
+ module Zakuro
9
+ # :nodoc:
10
+ module Calculation
11
+ # :nodoc:
12
+ module Range
13
+ #
14
+ # OperatedRange 運用結果範囲
15
+ #
16
+ # 何らかの理由により、計算された暦とは異なる運用結果である場合、その結果に合わせて計算結果を上書きする
17
+ class OperatedRange
18
+ # @return [Array<Year>] 年データ(完全範囲)
19
+ attr_reader :years
20
+ # @return [OperatedSolarTerms] 運用時二十四節気
21
+ attr_reader :operated_solar_terms
22
+ # @return [Context] 暦コンテキスト
23
+ attr_reader :context
24
+
25
+ #
26
+ # 初期化
27
+ #
28
+ # @param [Context] context 暦コンテキスト
29
+ # @param [Array<Year>] years 年データ(完全範囲)
30
+ #
31
+ def initialize(context:, years: [])
32
+ @context = context
33
+ @years = years
34
+ @operated_solar_terms = OperatedSolarTerms.new(context: context, years: @years)
35
+ @operated_solar_terms.create
36
+ end
37
+
38
+ #
39
+ # 運用結果範囲を取得する
40
+ #
41
+ # @return [Array<Year>] 運用結果範囲
42
+ #
43
+ def get
44
+ rewrite
45
+ end
46
+
47
+ #
48
+ # 運用結果に書き換える
49
+ #
50
+ # @return [Array<Year>] 運用結果範囲
51
+ #
52
+ def rewrite
53
+ operated_years = []
54
+
55
+ years.each do |year|
56
+ operated_year = OperatedRange.rewrite_year(
57
+ year: year, operated_solar_terms: @operated_solar_terms
58
+ )
59
+ operated_years.push(operated_year)
60
+ end
61
+
62
+ operated_years
63
+ end
64
+
65
+ #
66
+ # 年を書き換える
67
+ #
68
+ # @param [Year] year 年
69
+ # @param [OperatedSolarTerms] operated_solar_terms 運用時二十四節気
70
+ #
71
+ # @return [Year] 年
72
+ #
73
+ def self.rewrite_year(year:, operated_solar_terms:)
74
+ result = Base::Year.new(
75
+ multi_gengou: year.multi_gengou, new_year_date: year.new_year_date
76
+ )
77
+ year.months.each do |month|
78
+ result.push(month: resolve_month(
79
+ month: month, operated_solar_terms: operated_solar_terms
80
+ ))
81
+ end
82
+
83
+ result.commit
84
+
85
+ result
86
+ end
87
+
88
+ #
89
+ # 履歴情報の有無に応じた月にする
90
+ #
91
+ # @param [Month] month 月
92
+ # @param [OperatedSolarTerms] operated_solar_terms 運用時二十四節気
93
+ #
94
+ # @return [Month] 月
95
+ #
96
+ def self.resolve_month(month:, operated_solar_terms:)
97
+ history = Operation.specify_history(western_date: month.western_date)
98
+
99
+ return month if history.invalid?
100
+
101
+ OperatedRange.rewrite_month(
102
+ month: month, history: history, operated_solar_terms: operated_solar_terms
103
+ )
104
+ end
105
+
106
+ #
107
+ # 月を運用結果に書き換える
108
+ #
109
+ # @param [Month] month 月
110
+ # @param [Operation::MonthHistory] history 変更履歴
111
+ # @param [OperatedSolarTerms] operated_solar_terms 運用時二十四節気
112
+ #
113
+ # @return [Month] 月(運用結果)
114
+ #
115
+ def self.rewrite_month(month:, history:, operated_solar_terms:)
116
+ return month unless month.western_date == history.western_date
117
+
118
+ operated_month = Monthly::OperatedMonth.new(
119
+ month_label: month.month_label, first_day: month.first_day,
120
+ solar_terms: month.solar_terms, history: history,
121
+ operated_solar_terms: operated_solar_terms
122
+ )
123
+
124
+ operated_month.rewrite
125
+
126
+ Monthly::Month.new(
127
+ month_label: operated_month.month_label, first_day: operated_month.first_day,
128
+ solar_terms: operated_month.solar_terms
129
+ )
130
+ end
131
+ end
132
+ end
133
+ end
134
+ end
@@ -0,0 +1,201 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative '../../operation/operation'
4
+ require_relative '../monthly/month'
5
+
6
+ # TODO: solar_termの中はsrc/destだが、yamlはcalc/actual。統一したい。
7
+
8
+ # :nodoc:
9
+ module Zakuro
10
+ # :nodoc:
11
+ module Calculation
12
+ # :nodoc:
13
+ module Range
14
+ #
15
+ # OperatedSolarTerm 運用時二十四節気
16
+ #
17
+ class OperatedSolarTerms
18
+ # @return [Array<Year>] 完全範囲(年データ)
19
+ attr_reader :years
20
+ # @return [Hash<String, SolarTerm>] 二十四節気の移動元/移動先(西暦日 -> 対応する二十四節気)
21
+ #
22
+ # * 移動元の二十四節気:無効な大余小余あり(削除対象)
23
+ # * 移動先の二十四節気:移動元からの二十四節気(追加対象)
24
+ #
25
+ attr_reader :directions
26
+ # @return [Context] 暦コンテキスト
27
+ attr_reader :context
28
+
29
+ #
30
+ # 初期化
31
+ #
32
+ # @param [Context] context 暦コンテキスト
33
+ # @param [Array<Year>] years 完全範囲(年データ)
34
+ #
35
+ def initialize(context:, years: [])
36
+ @context = context
37
+ @years = years
38
+ @directions = {}
39
+ end
40
+
41
+ #
42
+ # データ生成する
43
+ #
44
+ # @return [<Type>] <description>
45
+ #
46
+ def create
47
+ @directions = create_directions
48
+ end
49
+
50
+ #
51
+ # 二十四節気を取得する
52
+ #
53
+ # @param [Western::Calendar] western_date 月初日の西暦日
54
+ #
55
+ # @return [True] 対象あり
56
+ # @return [False] 対象なし
57
+ # @return [SolarTerm] 二十四節気
58
+ #
59
+ def get(western_date: Western::Calendar.new)
60
+ solar_term_class = context.resolver.solar_term
61
+ solar_term = @directions.fetch(western_date.format, solar_term_class.new)
62
+
63
+ # 合致しない場合
64
+ return false, solar_term_class.new if solar_term.empty?
65
+
66
+ # 合致した上で、二十四節気が移動元(削除対象)の場合
67
+ # 合致した上で、二十四節気が移動先(追加対象)の場合
68
+ [true, solar_term]
69
+ end
70
+
71
+ #
72
+ # 二十四節気の移動元/移動先を生成する
73
+ #
74
+ # @return [Hash<String, SolarTerm>] 二十四節気の移動元/移動先(西暦日 -> 対応する二十四節気)
75
+ #
76
+ def create_directions
77
+ directions = {}
78
+
79
+ years.each do |year|
80
+ OperatedSolarTerms.create_directions_with_months(
81
+ context: context, directions: directions, months: year.months
82
+ )
83
+ end
84
+
85
+ directions
86
+ end
87
+
88
+ # :reek:TooManyStatements { max_statements: 6 }
89
+
90
+ #
91
+ # 年内の全ての月の移動方向を作成する
92
+ #
93
+ # @param [Context] context 暦コンテキスト
94
+ # @param [Hash<String, SolarTerm>] directions 二十四節気の移動元/移動先(西暦日 -> 対応する二十四節気)
95
+ # @param [Array<Month>] months 年内の全ての月
96
+ #
97
+ def self.create_directions_with_months(context:, directions: {}, months: [])
98
+ months.each do |month|
99
+ history = Operation.specify_history(western_date: month.western_date)
100
+
101
+ next if history.invalid?
102
+
103
+ direction = history.diffs.solar_term
104
+
105
+ next if direction.invalid?
106
+
107
+ OperatedSolarTerms.create_directions_each_month(
108
+ context: context, directions: directions, direction: direction, month: month
109
+ )
110
+ end
111
+ end
112
+
113
+ # :reek:LongParameterList {max_params: 4}
114
+
115
+ #
116
+ # 月毎の移動方向を作成する
117
+ #
118
+ # @param [Context] context 暦コンテキスト
119
+ # @param [Month] 月
120
+ # @param [Hash<String, SolarTerm>] directions 二十四節気の移動元/移動先(西暦日 -> 対応する二十四節気)
121
+ # @param [Operation::SolarTerm::Diretion] 二十四節気(移動)
122
+ #
123
+ def self.create_directions_each_month(context:, month:, directions: {},
124
+ direction: Operation::SolarTerm::Diretion.new)
125
+
126
+ month.solar_terms.each do |solar_term|
127
+ OperatedSolarTerms.push_source(context: context, directions: directions,
128
+ direction: direction, solar_term: solar_term)
129
+ end
130
+ OperatedSolarTerms.push_destination(context: context, directions: directions,
131
+ destination: direction.destination)
132
+ end
133
+
134
+ # :reek:LongParameterList {max_params: 4}
135
+
136
+ #
137
+ # 移動先に有効な二十四節気(差し替える二十四節気)を指定する
138
+ #
139
+ # @param [Context] context 暦コンテキスト
140
+ # @param [SolarTerm] solar_term 二十四節気(計算値)
141
+ # @param [Hash<String, SolarTerm>] directions 二十四節気の移動元/移動先(西暦日 -> 対応する二十四節気)
142
+ # @param [Operation::SolarTerm::Direction] source 二十四節気(移動)
143
+ #
144
+ def self.push_source(context:, solar_term:, directions: {},
145
+ direction: Operation::SolarTerm::Direction.new)
146
+ source = direction.source
147
+
148
+ return if source.invalid?
149
+
150
+ return unless source.index == solar_term.index
151
+
152
+ # 移動先に移動元の二十四節気を指定する
153
+ directions[source.to.format] = OperatedSolarTerms.created_source(
154
+ context: context, direction: direction, solar_term: solar_term
155
+ )
156
+ end
157
+
158
+ #
159
+ # 移動先に有効な二十四節気(差し替える二十四節気)を生成する
160
+ #
161
+ # @param [Context] context 暦コンテキスト
162
+ # @param [SolarTerm] solar_term 二十四節気(計算値)
163
+ # @param [Operation::SolarTerm::Direction] source 二十四節気(移動)
164
+ #
165
+ # @return [SolarTerm] 二十四節気(運用値)
166
+ #
167
+ def self.created_source(context:, solar_term:,
168
+ direction: Operation::SolarTerm::Direction.new)
169
+ operated_solar_term = solar_term.clone
170
+ remainder_class_name = context.resolver.remainder
171
+
172
+ unless direction.invalid_days?
173
+ # 二十四節気の大余をずらす
174
+ operated_solar_term.remainder.add!(
175
+ remainder_class_name.new(day: direction.days, minute: 0, second: 0)
176
+ )
177
+ end
178
+
179
+ operated_solar_term
180
+ end
181
+
182
+ #
183
+ # 移動元に無効な二十四節気(連番のみ指定)を指定する
184
+ #
185
+ # @param [Context] context 暦コンテキスト
186
+ # @param [Hash<String, SolarTerm>] directions 二十四節気の移動元/移動先(西暦日 -> 対応する二十四節気)
187
+ # @param [Operation::SolarTerm::Destination] destination 二十四節気(移動先)
188
+ #
189
+ def self.push_destination(context:, directions: {},
190
+ destination: Operation::SolarTerm::Destination.new)
191
+ return if destination.invalid?
192
+
193
+ solar_term_class = context.resolver.solar_term
194
+ directions[destination.from.format] = solar_term_class.new(
195
+ index: destination.index
196
+ )
197
+ end
198
+ end
199
+ end
200
+ end
201
+ end
@@ -0,0 +1,76 @@
1
+ # frozen_string_literal: true
2
+
3
+ # :nodoc:
4
+ module Zakuro
5
+ # :nodoc:
6
+ module Calculation
7
+ # :nodoc:
8
+ module Range
9
+ # :nodoc:
10
+ module Transfer
11
+ #
12
+ # WesternDateAllocation 西暦日の割り当て
13
+ #
14
+ module WesternDateAllocation
15
+ #
16
+ # 月初日の西暦日を更新する
17
+ #
18
+ # @param [Array<Year>] years 完全範囲(月初日なし)
19
+ #
20
+ # @return [Array<Year>] 完全範囲(月初日あり)
21
+ #
22
+ def self.get(years:)
23
+ update_first_day(years: years)
24
+
25
+ years
26
+ end
27
+
28
+ #
29
+ # 月初日の西暦日を更新する
30
+ #
31
+ # @param [Array<Year>] years 完全範囲(月初日なし)
32
+ #
33
+ def self.update_first_day(years:)
34
+ years.each_with_index do |year, index|
35
+ new_year_date = year.new_year_date.clone
36
+
37
+ months = year.months
38
+ update_first_day_within_all_months(
39
+ new_year_date: new_year_date, months: months
40
+ )
41
+
42
+ years[index] = Base::Year.new(
43
+ multi_gengou: year.multi_gengou, new_year_date: new_year_date,
44
+ months: months, total_days: year.total_days
45
+ )
46
+ end
47
+ end
48
+
49
+ #
50
+ # 全ての月で月初日の西暦日を更新する
51
+ #
52
+ # @param [Western::Calendar] new_year_date 元旦
53
+ # @param [Array<Month>] months 月データ
54
+ #
55
+ def self.update_first_day_within_all_months(new_year_date:, months:)
56
+ date = new_year_date.clone
57
+ months.each_with_index do |month, index|
58
+ updated_month = Monthly::Month.new(
59
+ month_label: month.month_label,
60
+ first_day: Monthly::FirstDay.new(
61
+ remainder: month.first_day.remainder,
62
+ western_date: date
63
+ ),
64
+ solar_terms: month.solar_terms
65
+ )
66
+ months[index] = updated_month
67
+
68
+ date = date.clone + updated_month.days
69
+ end
70
+ end
71
+ private_class_method :update_first_day_within_all_months
72
+ end
73
+ end
74
+ end
75
+ end
76
+ end
@@ -0,0 +1,142 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative '../../../calculation/base/multi_gengou_roller'
4
+ require_relative '../../../calculation/base/year'
5
+
6
+ require_relative '../../../era/western'
7
+
8
+ # :nodoc:
9
+ module Zakuro
10
+ # :nodoc:
11
+ module Calculation
12
+ # :nodoc:
13
+ module Range
14
+ # :nodoc:
15
+ module Transfer
16
+ #
17
+ # YearBoundary 年境界
18
+ #
19
+ module YearBoundary
20
+ #
21
+ # 年間範囲内の年データの開始月を変更する
22
+ #
23
+ # @param [Array<Year>] annual_ranges 年データ(冬至基準)
24
+ #
25
+ # @return [Array<Year>] 年データ(元旦基準)
26
+ #
27
+ def self.get(annual_ranges:)
28
+ categorize(annual_ranges: annual_ranges)
29
+ rearranged_years(annual_ranges: annual_ranges)
30
+ end
31
+
32
+ #
33
+ # 年間範囲内の年データの開始月を変更する
34
+ #
35
+ # @param [Array<Year>] annual_ranges 年データ(冬至基準)
36
+ #
37
+ # @return [Array<Year>] 年データ(元旦基準)
38
+ #
39
+ def self.rearranged_years(annual_ranges:)
40
+ years = []
41
+
42
+ (0..(annual_ranges.size - 2)).each do |index|
43
+ year = rearranged_year(annual_ranges: annual_ranges, index: index)
44
+ years.push(year)
45
+ end
46
+
47
+ years
48
+ end
49
+ private_class_method :rearranged_years
50
+
51
+ #
52
+ # 年間範囲を昨年/今年で分類する
53
+ #
54
+ # @param [Array<Year>] annual_range 1年データ
55
+ #
56
+ def self.categorize(annual_ranges:)
57
+ annual_ranges.each do |annual_range|
58
+ categorize_year(annual_range: annual_range)
59
+ end
60
+ end
61
+ private_class_method :categorize
62
+
63
+ #
64
+ # 各月を昨年/今年で分類する
65
+ #
66
+ # @param [Array<Month>] annual_range 1年データ
67
+ #
68
+ def self.categorize_year(annual_range:)
69
+ is_last_year = true
70
+ annual_range.each_with_index do |month, index|
71
+ is_last_year = false if month.number == 1
72
+
73
+ annual_range[index] = Monthly::InitializedMonth.new(
74
+ month_label: month.month_label, first_day: month.first_day,
75
+ solar_terms: month.solar_terms, phase_index: month.phase_index,
76
+ is_last_year: is_last_year
77
+ )
78
+ end
79
+ end
80
+ private_class_method :categorize_year
81
+
82
+ #
83
+ # 年データの開始月を変更する
84
+ #
85
+ # @param [Array<Year>] annual_ranges 年データ(冬至基準)
86
+ # @param [Integer] index 対象年の要素番号
87
+ #
88
+ # @return [Year] 年データ(元旦基準)
89
+ #
90
+ def self.rearranged_year(annual_ranges:, index:)
91
+ current_annual_range = annual_ranges[index]
92
+ next_annual_range = annual_ranges[index + 1]
93
+
94
+ year = push_current_year(annual_range: current_annual_range)
95
+ push_last_year(annual_range: next_annual_range, year: year)
96
+
97
+ year
98
+ end
99
+ private_class_method :rearranged_year
100
+
101
+ #
102
+ # 当年データを生成する
103
+ #
104
+ # @param [Array<Year>] annual_ranges 年データ(冬至基準)
105
+ # @param [Year] year 対象年
106
+ #
107
+ # @return [Year] 当年月ありの対象年
108
+ #
109
+ def self.push_current_year(annual_range:, year: Base::Year.new)
110
+ annual_range.each do |month|
111
+ next if month.is_last_year
112
+
113
+ year.push(month: month)
114
+ end
115
+
116
+ year
117
+ end
118
+ private_class_method :push_current_year
119
+
120
+ #
121
+ # 昨年データを生成する
122
+ #
123
+ # @param [Array<Year>] annual_ranges 年データ(冬至基準)
124
+ # @param [Year] year 対象年
125
+ #
126
+ # @return [Year] 昨年月ありの対象年
127
+ #
128
+ def self.push_last_year(annual_range:, year: Base::Year.new)
129
+ annual_range.each do |month|
130
+ next unless month.is_last_year
131
+
132
+ year.push(month: month)
133
+ end
134
+
135
+ year
136
+ end
137
+ private_class_method :push_last_year
138
+ end
139
+ end
140
+ end
141
+ end
142
+ end