Sha256: e0a480dc06bffc8f4aaf5a69a441ceafc4b43a04a36b8387f07e492297102742

Contents?: true

Size: 716 Bytes

Versions: 2

Compression:

Stored size: 716 Bytes

Contents

# frozen_string_literal: true

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

  type.define_caster do |value|
    next value if value.is_a?(::Numeric)

    begin
      SmartCore::Types::Value::Float.cast(value)
    rescue SmartCore::Types::TypeCastingError
      begin
        SmartCore::Types::Value::Integer.cast(value)
      rescue SmartCore::Types::TypeCastingError
        begin
          SmartCore::Types::Value::BigDecimal.cast(value)
        rescue SmartCore::Types::TypeCastingError
          raise(SmartCore::Types::TypeCastingError, 'Non-castable to Numeric')
        end
      end
    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/numeric.rb
smart_types-0.1.0 lib/smart_core/types/value/numeric.rb