Class: Rbdantic::Validators::Types::Date

Inherits:
Base
  • Object
show all
Defined in:
lib/rbdantic/validators/types/date.rb

Instance Attribute Summary

Attributes inherited from Base

#constraints

Instance Method Summary collapse

Methods inherited from Base

#initialize, #validate, #validate_constraints

Constructor Details

This class inherits a constructor from Rbdantic::Validators::Types::Base

Instance Method Details

#coerce(value) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/rbdantic/validators/types/date.rb', line 18

def coerce(value)
  case value
  when ::Date
    # DateTime is a subclass of Date, so this handles both
    # But we need to convert DateTime to Date (drop time info)
    if value.is_a?(::DateTime)
      value.to_date
    else
      value
    end
  when ::Time
    value.to_date
  when ::String
    ::Date.iso8601(value)
  when ::Integer, ::Float
    # Treat as days since Unix epoch (1970-01-01)
    # Integer: whole days (e.g., 0 => 1970-01-01, 1 => 1970-01-02)
    # Float: truncated to whole days (e.g., 1.5 => 1970-01-02)
    ::Date.new(1970, 1, 1) + value
  end
rescue ArgumentError
  nil
end

#expected_type_nameObject



14
15
16
# File 'lib/rbdantic/validators/types/date.rb', line 14

def expected_type_name
  "Date"
end

#matches_type?(value) ⇒ Boolean



10
11
12
# File 'lib/rbdantic/validators/types/date.rb', line 10

def matches_type?(value)
  value.is_a?(::Date) && !value.is_a?(::DateTime)
end