Sha256: f2b5bcadb922f7e3653712d0674b075800d5a731a3cdeaa9f1d1f0ac18e99df4

Contents?: true

Size: 514 Bytes

Versions: 2

Compression:

Stored size: 514 Bytes

Contents

# frozen_string_literal: true

require 'date'

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

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

    begin
      ::DateTime.parse(value)
    rescue ::Date::Error, ::TypeError
      raise(SmartCore::Types::TypeCastingError, 'Non-castable to DateTime')
    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_time.rb
smart_types-0.1.0 lib/smart_core/types/value/date_time.rb