Class: CSVPlusPlus::Entities::Date

Inherits:
Entity
  • Object
show all
Extended by:
T::Sig
Defined in:
lib/csv_plus_plus/entities/date.rb

Overview

A date value

Constant Summary collapse

DATE_STRING_REGEXP =

TODO: support time granularity?

%r{^\d{1,2}[/-]\d{1,2}[/-]\d{1,4}?$}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(value) ⇒ Date

Returns a new instance of Date.

Parameters:

  • value (::String)

    The user-inputted date value



32
33
34
35
36
37
38
39
40
41
42
# File 'lib/csv_plus_plus/entities/date.rb', line 32

def initialize(value)
  super()

  parsed =
    begin
      ::Date.parse(value)
    rescue ::Date::Error
      ::Date.strptime(value, '%d/%m/%yyyy')
    end
  @value = ::T.let(parsed, ::Date)
end

Instance Attribute Details

#valueDate (readonly)

The parsed date

Returns:

  • (Date)

    the current value of value



9
10
11
# File 'lib/csv_plus_plus/entities/date.rb', line 9

def value
  @value
end

Class Method Details

.valid_date?(date_string) ⇒ Boolean

Is the given string a valid date?

Parameters:

  • date_string (::String)

Returns:



23
24
25
26
27
28
# File 'lib/csv_plus_plus/entities/date.rb', line 23

def self.valid_date?(date_string)
  new(date_string)
  true
rescue ::Date::Error
  false
end

Instance Method Details

#==(other) ⇒ Boolean

Parameters:

  • other (BasicObject)

Returns:



56
57
58
59
60
61
62
63
# File 'lib/csv_plus_plus/entities/date.rb', line 56

def ==(other)
  case other
  when self.class
    other.value == @value
  else
    false
  end
end

#evaluate(_position) ⇒ ::String

Parameters:

  • _position (Position)

Returns:

  • (::String)


48
49
50
# File 'lib/csv_plus_plus/entities/date.rb', line 48

def evaluate(_position)
  @value.strftime('%m/%d/%y')
end