Sha256: 6d7a3c7ae38dafe12d537b4663eb94951cc7ec6b8c0170975e696cf6cdd59a22

Contents?: true

Size: 546 Bytes

Versions: 3

Compression:

Stored size: 546 Bytes

Contents

# frozen_string_literal: true

require 'date'

using SmartCore::Ext::BasicObjectAsObject

# @api public
# @since 0.1.0
# @version 0.3.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

3 entries across 3 versions & 1 rubygems

Version Path
smart_types-0.6.0 lib/smart_core/types/value/date.rb
smart_types-0.4.0 lib/smart_core/types/value/date.rb
smart_types-0.3.0 lib/smart_core/types/value/date.rb