zakuro 0.3.0 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Binary file
@@ -0,0 +1,114 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative './year'
4
+ require_relative './multi_gengou'
5
+
6
+ # :nodoc:
7
+ module Zakuro
8
+ # :nodoc:
9
+ module Calculation
10
+ # :nodoc:
11
+ module Base
12
+ #
13
+ # OperatedYear 年(運用)
14
+ #
15
+ class OperatedYear < Year
16
+ #
17
+ # 初期化
18
+ #
19
+ # @param [Gengou] multi_gengou 元号
20
+ # @param [Array<OperatedMonth>] months 年内の全ての月
21
+ # @param [Integer] total_days 年の日数
22
+ # @param [Western::Calendar] new_year_date 元旦
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
36
+ end
37
+
38
+ #
39
+ # 引数を月の先頭に加える
40
+ #
41
+ # @param [Array<OperatedMonth>] first_months 先頭の月
42
+ #
43
+ def unshift_months(first_months)
44
+ # 逆順で加える
45
+ first_months.reverse_each do |month|
46
+ month.moved
47
+ months.unshift(month)
48
+ end
49
+ end
50
+
51
+ #
52
+ # 引数を月の最後に加える
53
+ #
54
+ # @param [Array<OperatedMonth>] last_months 最後の月
55
+ #
56
+ def push_months(last_months)
57
+ last_months.each do |month|
58
+ month.moved
59
+ months.push(month)
60
+ end
61
+ end
62
+
63
+ #
64
+ # 昨年に属する月を取り出す
65
+ #
66
+ # @return [Array<OperatedMonth>] 昨年に属する月
67
+ #
68
+ def shift_last_year_months
69
+ result, @months = OperatedYear.devide_with(method: :last_year?, arr: months)
70
+
71
+ result
72
+ end
73
+
74
+ #
75
+ # 来年に属する月を取り出す
76
+ #
77
+ # @return [Array<OperatedMonth>] 来年に属する月
78
+ #
79
+ def pop_next_year_months
80
+ result, @months = OperatedYear.devide_with(method: :next_year?, arr: months)
81
+
82
+ result
83
+ end
84
+
85
+ # :reek:TooManyStatements { max_statements: 8 }
86
+
87
+ #
88
+ # メソッドで配列を分離する
89
+ #
90
+ # @param [Symbol] method 条件メソッド
91
+ # @param [Array<Object>] arr 配列
92
+ #
93
+ # @return [Array<Object>] 一致配列
94
+ # @return [Array<Object>] 不一致配列
95
+ #
96
+ def self.devide_with(method:, arr: [])
97
+ match = []
98
+ unmatch = []
99
+
100
+ arr.each do |item|
101
+ if !item.moved? && item.send(method)
102
+ match.push(item)
103
+ next
104
+ end
105
+
106
+ unmatch.push(item)
107
+ end
108
+
109
+ [match, unmatch]
110
+ end
111
+ end
112
+ end
113
+ end
114
+ end
@@ -25,6 +25,9 @@ module Zakuro
25
25
  # 初期化
26
26
  #
27
27
  # @param [Gengou] multi_gengou 元号
28
+ # @param [Array<Month>] months 年内の全ての月
29
+ # @param [Integer] total_days 年の日数
30
+ # @param [Western::Calendar] new_year_date 元旦
28
31
  #
29
32
  def initialize(multi_gengou: MultiGengou.new, new_year_date: Western::Calendar.new,
30
33
  months: [], total_days: 0)
@@ -23,7 +23,7 @@ module Zakuro
23
23
  # @param [Remainder] remainder 西暦日
24
24
  # @param [Western::Calendar] western_date 大余小余
25
25
  #
26
- def initialize(western_date: Western::Calendar.new, remainder: Remainder.new)
26
+ def initialize(western_date: Western::Calendar.new, remainder:)
27
27
  # 西暦日
28
28
  @western_date = western_date
29
29
  # 大余小余
@@ -9,7 +9,7 @@ module Zakuro
9
9
  module Calculation
10
10
  # :nodoc:
11
11
  module Monthly
12
- # :reek:TooManyInstanceVariables { max_instance_variables: 5 }
12
+ # :reek:TooManyInstanceVariables { max_instance_variables: 6 }
13
13
 
14
14
  #
15
15
  # OperatedMonth 月情報(運用)
@@ -40,6 +40,7 @@ module Zakuro
40
40
  solar_terms: solar_terms)
41
41
  @history = history
42
42
  @operated_solar_terms = operated_solar_terms
43
+ @moved = false
43
44
  end
44
45
 
45
46
  # rubocop:enable Metrics/ParameterLists
@@ -99,9 +100,9 @@ module Zakuro
99
100
  end
100
101
 
101
102
  #
102
- # <Description>
103
+ # 運用情報によって改変した二十四節気を作成する
103
104
  #
104
- # @param [<Type>] operated_solar_term <description>
105
+ # @param [<Type>] operated_solar_term 運用情報(二十四節気)
105
106
  # @param [Array<SolarTerm>] solar_terms 二十四節気
106
107
  #
107
108
  # @return [Array<SolarTerm>] 二十四節気
@@ -203,6 +204,49 @@ module Zakuro
203
204
 
204
205
  western_date
205
206
  end
207
+
208
+ #
209
+ # 運用情報では昨年の月か
210
+ #
211
+ # @return [True] 昨年の月
212
+ # @return [False] 今年/来年の月
213
+ #
214
+ def last_year?
215
+ history_month_number.last_year?
216
+ end
217
+
218
+ #
219
+ # 運用情報では来年の月か
220
+ #
221
+ # @return [True] 来年の月
222
+ # @return [False] 今年/昨年の月
223
+ #
224
+ def next_year?
225
+ history_month_number.next_year?
226
+ end
227
+
228
+ #
229
+ # 別の年に移動したか
230
+ #
231
+ # @return [True] 移動済
232
+ # @return [False] 移動なし
233
+ #
234
+ def moved?
235
+ @moved
236
+ end
237
+
238
+ #
239
+ # 移動済とする
240
+ #
241
+ def moved
242
+ @moved = true
243
+ end
244
+
245
+ private
246
+
247
+ def history_month_number
248
+ @history.diffs.month.number
249
+ end
206
250
  end
207
251
  end
208
252
  end
@@ -2,6 +2,7 @@
2
2
 
3
3
  require_relative './operated_solar_terms'
4
4
  require_relative '../../operation/operation'
5
+ require_relative '../base/operated_year'
5
6
  require_relative '../../calculation/monthly/operated_month'
6
7
 
7
8
  # :nodoc:
@@ -41,13 +42,19 @@ module Zakuro
41
42
  # @return [Array<Year>] 運用結果範囲
42
43
  #
43
44
  def get
44
- rewrite
45
+ operated_years = rewrite
46
+
47
+ OperatedRange.move(operated_years: operated_years)
48
+
49
+ OperatedRange.commit(operated_years: operated_years)
50
+
51
+ operated_years
45
52
  end
46
53
 
47
54
  #
48
55
  # 運用結果に書き換える
49
56
  #
50
- # @return [Array<Year>] 運用結果範囲
57
+ # @return [Array<OperatedYear>] 運用結果範囲
51
58
  #
52
59
  def rewrite
53
60
  operated_years = []
@@ -63,6 +70,54 @@ module Zakuro
63
70
  operated_years
64
71
  end
65
72
 
73
+ #
74
+ # 運用情報で年を跨ぐ月をその年に寄せる
75
+ #
76
+ # @param [Array<OperatedYear>] operated_years 運用結果範囲
77
+ #
78
+ def self.move(operated_years:)
79
+ # FIXME: この方式は完全ではない。範囲の1年前/1年後が必要
80
+ move_into_next_year(operated_years: operated_years)
81
+ move_into_last_year(operated_years: operated_years)
82
+ end
83
+
84
+ #
85
+ # 運用情報では来年に属する月を来年に寄せる
86
+ #
87
+ # @param [Array<OperatedYear>] operated_years 運用結果範囲
88
+ #
89
+ def self.move_into_next_year(operated_years:)
90
+ operated_years.each_cons(2) do |current_year, next_year|
91
+ months = current_year.pop_next_year_months
92
+
93
+ next_year.unshift_months(months)
94
+ end
95
+ end
96
+
97
+ #
98
+ # 運用情報では昨年に属する月を昨年に寄せる
99
+ #
100
+ # @param [Array<OperatedYear>] operated_years 運用結果範囲
101
+ #
102
+ def self.move_into_last_year(operated_years:)
103
+ rerversed_year = operated_years.reverse!
104
+ rerversed_year.each_cons(2) do |current_year, last_year|
105
+ months = current_year.shift_last_year_months
106
+ last_year.push_months(months)
107
+ end
108
+
109
+ rerversed_year.reverse!
110
+ end
111
+
112
+ #
113
+ # 年を確定させる
114
+ #
115
+ # @param [Array<OperatedYear>] operated_years 運用結果範囲
116
+ #
117
+ def self.commit(operated_years:)
118
+ operated_years.each(&:commit)
119
+ end
120
+
66
121
  #
67
122
  # 年を書き換える
68
123
  #
@@ -70,10 +125,10 @@ module Zakuro
70
125
  # @param [Year] year 年
71
126
  # @param [OperatedSolarTerms] operated_solar_terms 運用時二十四節気
72
127
  #
73
- # @return [Year] 年
128
+ # @return [OperatedYear] 年
74
129
  #
75
130
  def self.rewrite_year(context:, year:, operated_solar_terms:)
76
- result = Base::Year.new(
131
+ result = Base::OperatedYear.new(
77
132
  multi_gengou: year.multi_gengou, new_year_date: year.new_year_date
78
133
  )
79
134
  year.months.each do |month|
@@ -83,8 +138,6 @@ module Zakuro
83
138
  ))
84
139
  end
85
140
 
86
- result.commit
87
-
88
141
  result
89
142
  end
90
143
 
@@ -100,8 +153,6 @@ module Zakuro
100
153
  def self.resolve_month(context:, month:, operated_solar_terms:)
101
154
  history = Operation.specify_history(western_date: month.western_date)
102
155
 
103
- return month if history.invalid?
104
-
105
156
  OperatedRange.rewrite_month(
106
157
  context: context, month: month, history: history,
107
158
  operated_solar_terms: operated_solar_terms
@@ -121,8 +172,6 @@ module Zakuro
121
172
  # @return [Month] 月(運用結果)
122
173
  #
123
174
  def self.rewrite_month(context:, month:, history:, operated_solar_terms:)
124
- return month unless month.western_date == history.western_date
125
-
126
175
  operated_month = Monthly::OperatedMonth.new(
127
176
  context: context,
128
177
  month_label: month.month_label, first_day: month.first_day,
@@ -130,13 +179,9 @@ module Zakuro
130
179
  operated_solar_terms: operated_solar_terms
131
180
  )
132
181
 
133
- operated_month.rewrite
182
+ operated_month.rewrite unless history.invalid?
134
183
 
135
- Monthly::Month.new(
136
- context: context,
137
- month_label: operated_month.month_label, first_day: operated_month.first_day,
138
- solar_terms: operated_month.solar_terms
139
- )
184
+ operated_month
140
185
  end
141
186
  end
142
187
  end
@@ -3,103 +3,103 @@ name: "南北朝前+南朝"
3
3
  # 元中9年閏10月4日
4
4
  end_date: "1392-11-18"
5
5
  list:
6
- - name: "允恭"
6
+ - name: "允恭天皇"
7
7
  # 34年1月1日
8
8
  start_year: 34
9
9
  start_date: "445-1-24"
10
10
  new_year_date: "445-1-24"
11
11
  note: ""
12
- - name: "安康"
12
+ - name: "安康天皇"
13
13
  # 1年1月1日
14
14
  start_year: 1
15
15
  start_date: "454-2-14"
16
16
  new_year_date: "454-2-14"
17
17
  note: ""
18
- - name: "雄略"
18
+ - name: "雄略天皇"
19
19
  # 1年1月1日
20
20
  start_year: 1
21
21
  start_date: "457-2-10"
22
22
  new_year_date: "457-2-10"
23
23
  note: ""
24
- - name: "清寧"
24
+ - name: "清寧天皇"
25
25
  # 1年1月1日
26
26
  start_year: 1
27
27
  start_date: "480-1-28"
28
28
  new_year_date: "480-1-28"
29
29
  note: ""
30
- - name: "顕宗"
30
+ - name: "顕宗天皇"
31
31
  # 1年1月1日
32
32
  start_year: 1
33
33
  start_date: "485-2-1"
34
34
  new_year_date: "485-2-1"
35
35
  note: ""
36
- - name: "仁賢"
36
+ - name: "仁賢天皇"
37
37
  # 1年1月1日
38
38
  start_year: 1
39
39
  start_date: "488-1-29"
40
40
  new_year_date: "488-1-29"
41
41
  note: ""
42
- - name: "武烈"
42
+ - name: "武烈天皇"
43
43
  # 1年1月1日
44
44
  start_year: 1
45
45
  start_date: "499-1-28"
46
46
  new_year_date: "499-1-28"
47
47
  note: ""
48
- - name: "継体"
48
+ - name: "継体天皇"
49
49
  # 1年1月1日
50
50
  start_year: 1
51
51
  start_date: "507-1-29"
52
52
  new_year_date: "507-1-29"
53
53
  note: ""
54
- - name: "安閑"
54
+ - name: "安閑天皇"
55
55
  # 1年1月1日
56
56
  start_year: 1
57
57
  start_date: "534-1-30"
58
58
  new_year_date: "534-1-30"
59
59
  note: ""
60
- - name: "宣化"
60
+ - name: "宣化天皇"
61
61
  # 1年1月1日
62
62
  start_year: 1
63
63
  start_date: "536-2-8"
64
64
  new_year_date: "536-2-8"
65
65
  note: ""
66
- - name: "欽明"
66
+ - name: "欽明天皇"
67
67
  # 1年1月1日
68
68
  start_year: 1
69
69
  start_date: "540-1-25"
70
70
  new_year_date: "540-1-25"
71
71
  note: ""
72
- - name: "敏達"
72
+ - name: "敏達天皇"
73
73
  # 1年1月1日
74
74
  start_year: 1
75
75
  start_date: "572-1-31"
76
76
  new_year_date: "572-1-31"
77
77
  note: ""
78
- - name: "用明"
78
+ - name: "用明天皇"
79
79
  # 1年1月1日
80
80
  start_year: 1
81
81
  start_date: "586-1-25"
82
82
  new_year_date: "586-1-25"
83
83
  note: ""
84
- - name: "崇峻"
84
+ - name: "崇峻天皇"
85
85
  # 1年1月1日
86
86
  start_year: 1
87
87
  start_date: "588-2-3"
88
88
  new_year_date: "588-2-3"
89
89
  note: ""
90
- - name: "推古"
90
+ - name: "推古天皇"
91
91
  # 1年1月1日
92
92
  start_year: 1
93
93
  start_date: "593-2-7"
94
94
  new_year_date: "593-2-7"
95
95
  note: ""
96
- - name: "舒明"
96
+ - name: "舒明天皇"
97
97
  # 1年1月1日
98
98
  start_year: 1
99
99
  start_date: "629-1-30"
100
100
  new_year_date: "629-1-30"
101
101
  note: ""
102
- - name: "皇極"
102
+ - name: "皇極天皇"
103
103
  # 1年1月1日
104
104
  start_year: 1
105
105
  start_date: "642-2-5"
@@ -117,19 +117,19 @@ list:
117
117
  start_date: "650-3-22"
118
118
  new_year_date: "650-2-7"
119
119
  note: ""
120
- - name: "斉明"
120
+ - name: "斉明天皇"
121
121
  # 1年1月1日
122
122
  start_year: 1
123
123
  start_date: "655-2-12"
124
124
  new_year_date: "655-2-12"
125
125
  note: ""
126
- - name: "天智"
126
+ - name: "天智天皇"
127
127
  # 1年1月1日
128
128
  start_year: 1
129
129
  start_date: "662-1-25"
130
130
  new_year_date: "662-1-25"
131
131
  note: ""
132
- - name: "天武"
132
+ - name: "天武天皇"
133
133
  # 1年1月1日
134
134
  start_year: 1
135
135
  start_date: "672-2-4"
@@ -141,13 +141,13 @@ list:
141
141
  start_date: "686-8-14"
142
142
  new_year_date: "686-1-30"
143
143
  note: ""
144
- - name: "持統"
144
+ - name: "持統天皇"
145
145
  # 1年1月1日
146
146
  start_year: 1
147
147
  start_date: "687-2-18"
148
148
  new_year_date: "687-2-18"
149
149
  note: ""
150
- - name: "文武"
150
+ - name: "文武天皇"
151
151
  # 1年1月1日
152
152
  start_year: 1
153
153
  start_date: "697-1-28"