Sha256: b3a54824f8eecd64ee379c9ceb992ce285720e888a68366f67b62c809f6bab82

Contents?: true

Size: 1017 Bytes

Versions: 6

Compression:

Stored size: 1017 Bytes

Contents

# frozen_string_literal: true

# @api public
# @since 0.1.0
# @version 0.3.0
SmartCore::Types::Value.define_type(:TimeBased) do |type|
  type.define_checker do |value|
    SmartCore::Types::Value::Time.valid?(value) ||
      SmartCore::Types::Value::DateTime.valid?(value) ||
      SmartCore::Types::Value::Date.valid?(value)
  end

  type.define_caster do |value|
    next value if SmartCore::Types::Value::Time.valid?(value)
    next value if SmartCore::Types::Value::DateTime.valid?(value)
    next value if SmartCore::Types::Value::Date.valid?(value)

    begin
      SmartCore::Types::Value::Time.cast(value)
    rescue SmartCore::Types::TypeCastingError
      begin
        SmartCore::Types::Value::DateTime.cast(value)
      rescue SmartCore::Types::TypeCastingError
        begin
          SmartCore::Types::Value::Date.cast(value)
        rescue SmartCore::Types::TypeCastingError
          raise(SmartCore::Types::TypeCastingError, 'Non-castable to time-based type')
        end
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
smart_types-0.8.0 lib/smart_core/types/value/time_based.rb
smart_types-0.7.1 lib/smart_core/types/value/time_based.rb
smart_types-0.7.0 lib/smart_core/types/value/time_based.rb
smart_types-0.6.0 lib/smart_core/types/value/time_based.rb
smart_types-0.4.0 lib/smart_core/types/value/time_based.rb
smart_types-0.3.0 lib/smart_core/types/value/time_based.rb