Class: Zakuro::Calculation::Base::Year

Inherits:
Object
  • Object
show all
Defined in:
lib/zakuro/calculation/base/year.rb

Overview

Year 年

Direct Known Subclasses

OperatedYear

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(context: Context::Context.new, months: [], total_days: 0) ⇒ Year

初期化

Parameters:

  • context (Context::Context) (defaults to: Context::Context.new)

    暦コンテキスト

  • months (Array<Month>) (defaults to: [])

    年内の全ての月

  • total_days (Integer) (defaults to: 0)

    年の日数



33
34
35
36
37
# File 'lib/zakuro/calculation/base/year.rb', line 33

def initialize(context: Context::Context.new, months: [], total_days: 0)
  @context = context
  @months = months
  @total_days = total_days
end

Instance Attribute Details

#contextContext::Context (readonly)

Returns 暦コンテキスト.

Returns:



20
21
22
# File 'lib/zakuro/calculation/base/year.rb', line 20

def context
  @context
end

#monthsArray<Month> (readonly)

Returns 年内の全ての月.

Returns:

  • (Array<Month>)

    年内の全ての月



22
23
24
# File 'lib/zakuro/calculation/base/year.rb', line 22

def months
  @months
end

#total_daysInteger (readonly)

Returns 年の日数.

Returns:

  • (Integer)

    年の日数



24
25
26
# File 'lib/zakuro/calculation/base/year.rb', line 24

def total_days
  @total_days
end

Instance Method Details

#commitObject

年の日数を確定する



42
43
44
45
46
47
48
49
# File 'lib/zakuro/calculation/base/year.rb', line 42

def commit
  @total_days = 0
  months.each do |month|
    @total_days += month.days
  end

  self
end

#duplicated?(month:) ⇒ True

Note:

昨年11月1日から今年1月1日の前日までで、去年データと重複する場合は登録スキップする

すでに登録済みの月と重複しているか判定する

Parameters:

  • month (Month)

Returns:

  • (True)

    重複している

  • (True)

    重複していない



113
114
115
116
117
118
# File 'lib/zakuro/calculation/base/year.rb', line 113

def duplicated?(month:)
  months.each do |existed|
    return true if existed.same?(other: month)
  end
  false
end

#new_year_dateWestern::Calendar

年初を取得する

Returns:



70
71
72
73
74
75
76
# File 'lib/zakuro/calculation/base/year.rb', line 70

def new_year_date
  return Western::Calendar.new unless months

  return Western::Calendar.new if months.empty?

  months[0].western_date
end

#next_yearMultiGengou

次年にする

Parameters:

Returns:

  • (MultiGengou)

    自身



59
60
61
62
63
# File 'lib/zakuro/calculation/base/year.rb', line 59

def next_year
  @total_days = 0

  self
end

#push(month:) ⇒ Object

月を追加する

Parameters:

  • month (Month)



95
96
97
98
99
100
101
# File 'lib/zakuro/calculation/base/year.rb', line 95

def push(month:)
  return if duplicated?(month: month)

  months.push(month)

  nil
end

#zodiac_nameString

十干十二支を取得する

Returns:

  • (String)

    十干十二支



83
84
85
86
87
88
# File 'lib/zakuro/calculation/base/year.rb', line 83

def zodiac_name
  date = new_year_date
  return '' if date.invalid?

  Cycle::Zodiac.year_name(western_year: date.year)
end