Class: Zakuro::Calculation::Range::AbstractOperationRange

Inherits:
Object
  • Object
show all
Defined in:
lib/zakuro/calculation/range/abstract_operation_range.rb

Overview

AbstractOperationRange 運用結果範囲

何らかの理由により、計算された暦とは異なる運用結果である場合、その結果に合わせて計算結果を上書きする

Direct Known Subclasses

DatedOperationRange, NamedOperationRange

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(context:, scroll:, years: []) ⇒ AbstractOperationRange

初期化

Parameters:



34
35
36
37
38
39
40
# File 'lib/zakuro/calculation/range/abstract_operation_range.rb', line 34

def initialize(context:, scroll:, years: [])
  @context = context
  @years = years
  @scroll = scroll
  @operated_solar_term = OperatedSolarTerm.new(context: context, years: @years)
  operated_solar_term.create
end

Instance Attribute Details

#contextContext::Context (readonly)

Returns 暦コンテキスト.

Returns:



25
26
27
# File 'lib/zakuro/calculation/range/abstract_operation_range.rb', line 25

def context
  @context
end

#operated_solar_termOperatedSolarTerm (readonly)

Returns 運用時二十四節気.

Returns:



23
24
25
# File 'lib/zakuro/calculation/range/abstract_operation_range.rb', line 23

def operated_solar_term
  @operated_solar_term
end

#yearsArray<Year> (readonly)

Returns 年データ(完全範囲).

Returns:

  • (Array<Year>)

    年データ(完全範囲)



21
22
23
# File 'lib/zakuro/calculation/range/abstract_operation_range.rb', line 21

def years
  @years
end

Class Method Details

.commit(operated_years:) ⇒ Object

年を確定させる

Parameters:

  • operated_years (Array<OperatedYear>)

    運用結果範囲



124
125
126
# File 'lib/zakuro/calculation/range/abstract_operation_range.rb', line 124

def commit(operated_years:)
  operated_years.each(&:commit)
end

.move(operated_years:) ⇒ Object

運用情報で年を跨ぐ月をその年に寄せる

Parameters:

  • operated_years (Array<OperatedYear>)

    運用結果範囲



86
87
88
89
# File 'lib/zakuro/calculation/range/abstract_operation_range.rb', line 86

def move(operated_years:)
  move_into_next_year(operated_years: operated_years)
  move_into_last_year(operated_years: operated_years)
end

.move_into_last_year(operated_years:) ⇒ Object

運用情報では昨年に属する月を昨年に寄せる

Parameters:

  • operated_years (Array<OperatedYear>)

    運用結果範囲



109
110
111
112
113
114
115
116
117
# File 'lib/zakuro/calculation/range/abstract_operation_range.rb', line 109

def move_into_last_year(operated_years:)
  rerversed_year = operated_years.reverse!
  rerversed_year.each_cons(2) do |current_year, last_year|
    months = current_year.shift_last_year_months
    last_year.push_months(months)
  end

  rerversed_year.reverse!
end

.move_into_next_year(operated_years:) ⇒ Object

運用情報では来年に属する月を来年に寄せる

Parameters:

  • operated_years (Array<OperatedYear>)

    運用結果範囲



96
97
98
99
100
101
102
# File 'lib/zakuro/calculation/range/abstract_operation_range.rb', line 96

def move_into_next_year(operated_years:)
  operated_years.each_cons(2) do |current_year, next_year|
    months = current_year.pop_next_year_months

    next_year.unshift_months(months)
  end
end

.reset_meta(years: []) ⇒ Object

メタ情報を更新する

Parameters:

  • years (Array<Base::Year>) (defaults to: [])

    完全範囲



197
198
199
200
201
202
203
204
205
206
# File 'lib/zakuro/calculation/range/abstract_operation_range.rb', line 197

def reset_meta(years: [])
  months = []
  years.each do |year|
    months |= year.months
  end

  months.each_cons(2) do |last, current|
    current.reset_meta(last: last)
  end
end

.resolve_month(context:, month:, operated_solar_term:) ⇒ Month

履歴情報の有無に応じた月にする

Parameters:

  • context (Context)

    暦コンテキスト

  • month (Month)

  • operated_solar_term (OperatedSolarTerm)

    運用時二十四節気

Returns:

  • (Month)



158
159
160
161
162
163
164
165
# File 'lib/zakuro/calculation/range/abstract_operation_range.rb', line 158

def resolve_month(context:, month:, operated_solar_term:)
  history = Operation.specify_history(western_date: month.western_date)

  rewrite_month(
    context: context, month: month, history: history,
    operated_solar_term: operated_solar_term
  )
end

.rewrite_month(context:, month:, history:, operated_solar_term:) ⇒ Monthly::Month

月を運用結果に書き換える

Parameters:

Returns:



179
180
181
182
183
184
185
186
187
188
189
190
# File 'lib/zakuro/calculation/range/abstract_operation_range.rb', line 179

def rewrite_month(context:, month:, history:, operated_solar_term:)
  operated_month = Monthly::OperatedMonth.new(
    context: context,
    month_label: month.month_label, first_day: month.first_day,
    solar_terms: month.solar_terms, history: history, gengou: month.gengou,
    operated_solar_term: operated_solar_term, meta: month.meta
  )

  operated_month.rewrite unless history.invalid?

  operated_month
end

.rewrite_year(year:, operated_solar_term:) ⇒ OperatedYear

年を書き換える

Parameters:

  • year (Year)

  • operated_solar_term (OperatedSolarTerm)

    運用時二十四節気

Returns:

  • (OperatedYear)



136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/zakuro/calculation/range/abstract_operation_range.rb', line 136

def rewrite_year(year:, operated_solar_term:)
  context = year.context
  result = Base::OperatedYear.new(context: context)
  year.months.each do |month|
    result.push(month: resolve_month(
      context: context, month: month,
      operated_solar_term: operated_solar_term
    ))
  end

  result
end

Instance Method Details

#getArray<Year>

運用結果範囲を取得する

Returns:

  • (Array<Year>)

    運用結果範囲



47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/zakuro/calculation/range/abstract_operation_range.rb', line 47

def get
  operated_years = rewrite

  self.class.move(operated_years: operated_years)

  self.class.commit(operated_years: operated_years)

  Transfer::GengouScroller.set(scroll: @scroll, years: operated_years)

  self.class.reset_meta(years: operated_years)

  operated_years
end

#rewriteArray<OperatedYear>

運用結果に書き換える

Returns:

  • (Array<OperatedYear>)

    運用結果範囲



66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/zakuro/calculation/range/abstract_operation_range.rb', line 66

def rewrite
  operated_years = []

  years.each do |year|
    operated_year = self.class.rewrite_year(
      year: year,
      operated_solar_term: operated_solar_term
    )
    operated_years.push(operated_year)
  end

  operated_years
end