Class: Zakuro::Japan::Calendar

Inherits:
Object
  • Object
show all
Defined in:
lib/zakuro/era/japan/calendar.rb

Overview

Calendar 年月日情報(和暦)

Constant Summary collapse

INVALID =

Returns 無効値.

Returns:

  • (Integer)

    無効値

-1
# @return [String] 空文字列
EMPTY =

Returns 空文字列.

Returns:

  • (String)

    空文字列

''
LEAPED_TEXT =

Returns 閏を示す文字列.

Returns:

  • (String)

    閏を示す文字列

''
FORMAT =

Returns 和暦日フォーマット.

Returns:

  • (Regexp)

    和暦日フォーマット

/^([一-龥]{2,4})([0-9]+)年(#{LEAPED_TEXT})?([0-9]+)月([0-9]+)日$/.freeze
DEFAULT_OUTPUT_FORM =

Returns 出力用デフォルトフォーマット.

Returns:

  • (String)

    出力用デフォルトフォーマット

'%s%02d年%s%02d月%02d日'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(gengou: EMPTY, year: INVALID, leaped: false, month: INVALID, day: INVALID) ⇒ Calendar

初期化

Parameters:

  • gengou (String) (defaults to: EMPTY)

    元号

  • year (Integer) (defaults to: INVALID)

    元号年

  • leaped (True, False) (defaults to: false)

  • month (Integer) (defaults to: INVALID)

  • day (Integer) (defaults to: INVALID)



47
48
49
50
51
52
53
# File 'lib/zakuro/era/japan/calendar.rb', line 47

def initialize(gengou: EMPTY, year: INVALID, leaped: false, month: INVALID, day: INVALID)
  @gengou = gengou
  @year = year
  @leaped = leaped
  @month = month
  @day = day
end

Instance Attribute Details

#dayInteger (readonly)

Returns 日.

Returns:

  • (Integer)



36
37
38
# File 'lib/zakuro/era/japan/calendar.rb', line 36

def day
  @day
end

#gengouString (readonly)

Returns 元号.

Returns:

  • (String)

    元号



27
28
29
# File 'lib/zakuro/era/japan/calendar.rb', line 27

def gengou
  @gengou
end

#leapedTrue, False (readonly)

Returns:

  • (True)

    閏あり

  • (False)

    閏なし



32
33
34
# File 'lib/zakuro/era/japan/calendar.rb', line 32

def leaped
  @leaped
end

#monthInteger (readonly)

Returns 月.

Returns:

  • (Integer)



34
35
36
# File 'lib/zakuro/era/japan/calendar.rb', line 34

def month
  @month
end

#yearInteger (readonly)

Returns 元号年.

Returns:

  • (Integer)

    元号年



29
30
31
# File 'lib/zakuro/era/japan/calendar.rb', line 29

def year
  @year
end

Class Method Details

.parse(regex: FORMAT, text: '') ⇒ Calendar

年月日情報(和暦)を生成する

Parameters:

  • regex (Regexp) (defaults to: FORMAT)

    正規表現

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

    和暦日文字列

Returns:

  • (Calendar)

    年月日情報(和暦)



100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/zakuro/era/japan/calendar.rb', line 100

def parse(regex: FORMAT, text: '')
  return Calendar.new unless valid_date_text(regex: regex, text: text)

  matched = text.match(regex)

  Calendar.new(
    gengou: matched[1],
    year: Tool::Typeconv.to_i(text: matched[2], default: INVALID),
    leaped: matched[3] ? true : false,
    month: Tool::Typeconv.to_i(text: matched[4], default: INVALID),
    day: Tool::Typeconv.to_i(text: matched[5], default: INVALID)
  )
end

.valid_date_text(regex: FORMAT, text: '') ⇒ True

日付文字列を検証する

Parameters:

  • regex (Regexp) (defaults to: FORMAT)

    正規表現

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

    和暦日文字列

Returns:

  • (True)

    正しい

  • (True)

    正しくない



123
124
125
126
127
128
129
130
131
# File 'lib/zakuro/era/japan/calendar.rb', line 123

def valid_date_text(regex: FORMAT, text: '')
  return false unless text

  matched = text.match(regex)

  return false unless matched

  matched.size == 6
end

Instance Method Details

#format(form: DEFAULT_OUTPUT_FORM) ⇒ String

文字列にする

Returns:

  • (String)

    和暦日フォーマット文字列



70
71
72
73
# File 'lib/zakuro/era/japan/calendar.rb', line 70

def format(form: DEFAULT_OUTPUT_FORM)
  leaped_text = @leaped ? LEAPED_TEXT : ''
  super(form, @gengou, @year, leaped_text, @month, @day)
end

#invalid?True, False

無効か

Returns:

  • (True)

    無効

  • (False)

    有効



61
62
63
# File 'lib/zakuro/era/japan/calendar.rb', line 61

def invalid?
  @gengou == EMPTY || @year == INVALID || @month == INVALID || @day == INVALID
end

#same_month?(leaped:, month:) ⇒ True

同月か

年と日は無視する

Parameters:

  • leaped (True, False)

  • month (Integer)

Returns:

  • (True)

    同月

  • (True)

    同月ではない



85
86
87
88
89
# File 'lib/zakuro/era/japan/calendar.rb', line 85

def same_month?(leaped:, month:)
  return false unless @leaped == leaped

  @month == month
end