Sha256: 6ef0482de0ca055951d8c128ea444c6910c3af3cf2864b33089bb0199e53e681

Contents?: true

Size: 690 Bytes

Versions: 2

Compression:

Stored size: 690 Bytes

Contents

# frozen_string_literal: true

require 'bigdecimal'
require 'bigdecimal/util'

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

  type.define_caster do |value|
    if SmartCore::Types::Value::Float.valid?(value)
      value = SmartCore::Types::Value::String.cast(value)
    end

    begin
      ::Kernel.BigDecimal(value)
    rescue ::ArgumentError, ::TypeError
      begin
        ::Kernel.BigDecimal(value.to_d)
      rescue ::ArgumentError, ::TypeError, ::NoMethodError
        raise(SmartCore::Types::TypeCastingError, 'Non-castable to BigDecimal')
      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/big_decimal.rb
smart_types-0.1.0 lib/smart_core/types/value/big_decimal.rb