Class: Zakuro::Japan::Type::Validation::Both::Date

Inherits:
Object
  • Object
show all
Defined in:
lib/zakuro/era/japan/type/validation/both/date.rb

Overview

Date 日

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hash:, optional: false) ⇒ Date

初期化

Parameters:

  • hash (Hash<String, Strin>)

    日情報



34
35
36
37
38
# File 'lib/zakuro/era/japan/type/validation/both/date.rb', line 34

def initialize(hash:, optional: false)
  @japan = hash['japan']
  @western = hash['western']
  @optional = optional
end

Instance Attribute Details

#japanString (readonly)

Returns 和暦日.

Returns:

  • (String)

    和暦日



22
23
24
# File 'lib/zakuro/era/japan/type/validation/both/date.rb', line 22

def japan
  @japan
end

#optionalTrue, False (readonly)

Returns:

  • (True)

    省略可

  • (False)

    省略不可



27
28
29
# File 'lib/zakuro/era/japan/type/validation/both/date.rb', line 27

def optional
  @optional
end

#westernString (readonly)

Returns 西暦日.

Returns:

  • (String)

    西暦日



24
25
26
# File 'lib/zakuro/era/japan/type/validation/both/date.rb', line 24

def western
  @western
end

Instance Method Details

#japan?True, False

和暦日を検証する

Returns:

  • (True)

    正しい

  • (False)

    正しくない



61
62
63
64
65
# File 'lib/zakuro/era/japan/type/validation/both/date.rb', line 61

def japan?
  return true if optional?(text: japan)

  Japan::Calendar.valid_date_text(text: japan)
end

#optional?(text: '') ⇒ True, False

省略可で省略されているか

Parameters:

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

    文字列

Returns:

  • (True)

    省略あり

  • (False)

    省略なし



87
88
89
90
91
92
93
# File 'lib/zakuro/era/japan/type/validation/both/date.rb', line 87

def optional?(text: '')
  return false unless optional

  return true if text == ''

  false
end

#validateArray<String>

検証する

Returns:

  • (Array<String>)

    不正メッセージ



45
46
47
48
49
50
51
52
53
# File 'lib/zakuro/era/japan/type/validation/both/date.rb', line 45

def validate
  failed = []

  failed.push("invalid japan date. #{japan}") unless japan?

  failed.push("invalid western date. #{western}") unless western?

  failed
end

#western?True, False

西暦日を検証する

Returns:

  • (True)

    正しい

  • (False)

    正しくない



73
74
75
76
77
# File 'lib/zakuro/era/japan/type/validation/both/date.rb', line 73

def western?
  return true if optional?(text: western)

  Western::Calendar.valid_date_text(text: western)
end