Sha256: f12446d44d8dfa3b851f207cd4274e576781c2065bf1d5f20c5850aa848cf4fa

Contents?: true

Size: 659 Bytes

Versions: 2

Compression:

Stored size: 659 Bytes

Contents

# frozen_string_literal: true

# @api public
# @since 0.1.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

2 entries across 2 versions & 1 rubygems

Version Path
smart_types-0.2.0 lib/smart_core/types/value/hash.rb
smart_types-0.1.0 lib/smart_core/types/value/hash.rb