Sha256: b4aa329fe378a850aad0b9166cdc8250bc1f705d043417a486dae1080990d578

Contents?: true

Size: 719 Bytes

Versions: 6

Compression:

Stored size: 719 Bytes

Contents

# frozen_string_literal: true

using SmartCore::Ext::BasicObjectAsObject

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

  type.define_caster do |value|
    begin
      ::Kernel.Hash(value)
    rescue ::TypeError
      begin
        # NOTE:
        # - ::Kernel.Hash does not invoke `#to_h` under the hood (it invokes `#to_hash`)
        # - ::Kernel.Hash is used to validate the returned value from `#to_h`
        ::Kernel.Hash(value.to_h)
      rescue ::TypeError, ::NoMethodError, ::ArgumentError
        raise(SmartCore::Types::TypeCastingError, 'Non-castable to Hash')
      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/hash.rb
smart_types-0.7.1 lib/smart_core/types/value/hash.rb
smart_types-0.7.0 lib/smart_core/types/value/hash.rb
smart_types-0.6.0 lib/smart_core/types/value/hash.rb
smart_types-0.4.0 lib/smart_core/types/value/hash.rb
smart_types-0.3.0 lib/smart_core/types/value/hash.rb