Sha256: 105a1441a667ca266ec592532f8a4f1a8a27760bbdf0b57cb53c2ac0aeea175e

Contents?: true

Size: 752 Bytes

Versions: 6

Compression:

Stored size: 752 Bytes

Contents

# frozen_string_literal: true

require 'bigdecimal'
require 'bigdecimal/util'

using SmartCore::Ext::BasicObjectAsObject

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

6 entries across 6 versions & 1 rubygems

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