Class: Zakuro::Japan::Type::Base::Gengou

Inherits:
Object
  • Object
show all
Defined in:
lib/zakuro/era/japan/type/base/gengou.rb

Overview

Gengou 元号情報

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name: '', start_year: Both::Year.new, start_date: SwitchDate.new, last_date: Western::Calendar.new, last_year: Both::Year::INVALID) ⇒ Gengou

初期化

Parameters:

  • name (String) (defaults to: '')

    元号名

  • start_year (Both::Year) (defaults to: Both::Year.new)

    開始年(和暦/西暦)

  • start_date (SwitchDate) (defaults to: SwitchDate.new)

    開始日(和暦/西暦)

  • last_date (Integer) (defaults to: Western::Calendar.new)

    終了年

  • last_date (Western::Calendar) (defaults to: Western::Calendar.new)

    終了日



38
39
40
41
42
43
44
45
46
47
# File 'lib/zakuro/era/japan/type/base/gengou.rb', line 38

def initialize(name: '', start_year: Both::Year.new,
               start_date: SwitchDate.new,
               last_date: Western::Calendar.new,
               last_year: Both::Year::INVALID)
  @name = name
  @start_year = start_year
  @start_date = start_date
  @last_year = last_year
  @last_date = last_date
end

Instance Attribute Details

#last_dateWestern::Calendar (readonly)

Returns 終了日.

Returns:



27
28
29
# File 'lib/zakuro/era/japan/type/base/gengou.rb', line 27

def last_date
  @last_date
end

#last_yearInteger (readonly)

Returns 終了年.

Returns:

  • (Integer)

    終了年



25
26
27
# File 'lib/zakuro/era/japan/type/base/gengou.rb', line 25

def last_year
  @last_year
end

#nameString (readonly)

Returns 元号名.

Returns:

  • (String)

    元号名



19
20
21
# File 'lib/zakuro/era/japan/type/base/gengou.rb', line 19

def name
  @name
end

#start_dateSwitchDate (readonly)

Returns 開始日(和暦/西暦).

Returns:



23
24
25
# File 'lib/zakuro/era/japan/type/base/gengou.rb', line 23

def start_date
  @start_date
end

#start_yearBoth::Year (readonly)

Returns 開始年(和暦/西暦).

Returns:



21
22
23
# File 'lib/zakuro/era/japan/type/base/gengou.rb', line 21

def start_year
  @start_year
end

Class Method Details

.valid_date(date:) ⇒ True, False

日付が不正なしかどうかを確認する

Parameters:

Returns:

  • (True)

    不正なし

  • (False)

    不正



167
168
169
170
171
# File 'lib/zakuro/era/japan/type/base/gengou.rb', line 167

def valid_date(date:)
  return false unless date

  date.is_a?(Western::Calendar)
end

.valid_year(year:) ⇒ True, False

年が不正なしかどうかを確認する

Parameters:

  • year (Integer)

Returns:

  • (True)

    不正なし

  • (False)

    不正



153
154
155
156
157
# File 'lib/zakuro/era/japan/type/base/gengou.rb', line 153

def valid_year(year:)
  return false unless year

  year.is_a?(Integer)
end

Instance Method Details

#convert_next_start_date_to_last_date(next_start_date: Western::Calendar.new) ⇒ Object

次の元号の開始日から、元号の終了日に変換する

Parameters:

  • next_start_date (Western::Calendar) (defaults to: Western::Calendar.new)

    次回開始日

Raises:

  • (ArgumentError)

    引数エラー



103
104
105
106
107
108
109
# File 'lib/zakuro/era/japan/type/base/gengou.rb', line 103

def convert_next_start_date_to_last_date(next_start_date: Western::Calendar.new)
  raise ArgumentError, 'invalid value. cannot convert' if next_start_date.invalid?

  @last_date = next_start_date.clone - 1

  nil
end

#convert_next_start_year_to_last_year(next_start_year:) ⇒ Object

次の元号の開始年から、元号の終了年に変換する

Parameters:

  • next_start_year (Integer)

    次回開始年



85
86
87
88
89
90
91
92
93
94
# File 'lib/zakuro/era/japan/type/base/gengou.rb', line 85

def convert_next_start_year_to_last_year(next_start_year:)
  if start_year.western >= next_start_year
    @last_year = next_start_year
    return
  end

  @last_year = next_start_year - 1

  nil
end

#include?(date:) ⇒ True, False

指定した日が元号に含まれるか

Parameters:

Returns:

  • (True)

    含まれる

  • (False)

    含まれない



119
120
121
# File 'lib/zakuro/era/japan/type/base/gengou.rb', line 119

def include?(date:)
  date >= start_date.western && date <= last_date
end

#invalid?True, False

不正か

Returns:

  • (True)

    不正

  • (False)

    不正なし



129
130
131
132
# File 'lib/zakuro/era/japan/type/base/gengou.rb', line 129

def invalid?
  start_year.japan == -1 || start_year.invalid? ||
    start_date.invalid? || last_date.invalid?
end

#to_sString

インスタンス値(文字列)を取得する

Returns:

  • (String)

    インスタンス値(文字列)



139
140
141
142
# File 'lib/zakuro/era/japan/type/base/gengou.rb', line 139

def to_s
  "name: #{@name}, start_year: #{start_year.format}, " \
  "start_date: #{start_date.format}, last_date: #{last_date.format}"
end

#write_last_date(last_date:) ⇒ Object

終了日を更新する

Parameters:

Raises:

  • (ArgumentError)

    引数エラー



71
72
73
74
75
76
77
78
# File 'lib/zakuro/era/japan/type/base/gengou.rb', line 71

def write_last_date(last_date:)
  unless self.class.valid_date(date: last_date)
    raise ArgumentError, "invalid date format. [#{last_date}]"
  end

  @last_date = last_date
  nil
end

#write_last_year(last_year:) ⇒ Object

終了年を更新する

Parameters:

  • last_year (Integer)

    終了年

Raises:

  • (ArgumentError)

    引数エラー



56
57
58
59
60
61
62
# File 'lib/zakuro/era/japan/type/base/gengou.rb', line 56

def write_last_year(last_year:)
  unless self.class.valid_year(year: last_year)
    raise ArgumentError, "invalid year format. [#{last_year}]"
  end

  @last_year = last_year
end