Class: StrictDateType

Inherits:
ActiveModel::Type::Value
  • Object
show all
Defined in:
app/types/strict_date_type.rb

Overview

The strict date does not take any rubbish. It does not convert strings into year zero for example. So, allows for much better validation. It can also return an @see InvalidDate instance or a @see BlankDate instance if the date is either blank or invalid. This is then used by the custom validators. This means that any code using the value can rely on it being a date or nil. Any code that needs to display the invalid values can still call 'day', 'month' or 'year' on it etc..

Instance Method Summary collapse

Constructor Details

#initialize(invalid_date_class: InvalidDate, blank_date_class: BlankDate, **args) ⇒ StrictDateType

Returns a new instance of StrictDateType


10
11
12
13
14
# File 'app/types/strict_date_type.rb', line 10

def initialize(invalid_date_class: InvalidDate, blank_date_class: BlankDate, **args)
  self.invalid_date_class = invalid_date_class
  self.blank_date_class = blank_date_class
  super(*args)
end

Instance Method Details

#cast(value) ⇒ Date, ...

Casts the value to a date

Parameters:

  • value (Object)

    The value to cast to a date or nil

Returns:


19
20
21
22
23
# File 'app/types/strict_date_type.rb', line 19

def cast(value)
  return value if value.nil? || value.is_a?(Date)
  raise "The strict date type must be presented with a hash containing day, month and year" unless value.is_a?(Hash)
  cast_value(value.symbolize_keys)
end