Sha256: 1c83d5fc4ff92012c27a48f64b33390483bea0d88669d1617c9cbe75c985d2f5

Contents?: true

Size: 486 Bytes

Versions: 2

Compression:

Stored size: 486 Bytes

Contents

# frozen_string_literal: true

require 'date'

# @api public
# @since 0.1.0
SmartCore::Types::Value.define_type(:Date) do |type|
  type.define_checker do |value|
    value.is_a?(::Date) || value == ::Date::Infinity
  end

  type.define_caster do |value|
    next value if value.is_a?(::Date) || value == ::Date::Infinity

    begin
      ::Date.parse(value)
    rescue ::Date::Error, ::TypeError
      raise(SmartCore::Types::TypeCastingError, 'Non-castable to Date')
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
smart_types-0.2.0 lib/smart_core/types/value/date.rb
smart_types-0.1.0 lib/smart_core/types/value/date.rb