Sha256: e188f27b64553199075f22fb7b5913b983c6ae30c4f8b087c1f031471f100946

Contents?: true

Size: 697 Bytes

Versions: 124

Compression:

Stored size: 697 Bytes

Contents

# frozen_string_literal: true

class ReeHash::HashKeysContract
  extend Ree::Contracts::Truncatable

  def self.valid?(obj)
    return false if !obj.is_a?(Array)
    return false if obj.any? { !valid_item?(_1) }
    true
  end

  def self.to_s
    "[:key0, .., :keyM => [:keyN, .., :keyZ]]"
  end

  def self.message(value, name, lvl = 1)
    "expected #{to_s}, got => #{truncate(value.inspect)}"
  end

  private

  def self.valid_item?(obj)
    return true if obj.is_a?(Symbol)
    return false if !obj.is_a?(Hash)  

    obj.each do |k, v|
      return false if !k.is_a?(Symbol)
      return false if !v.is_a?(Array)
      return false if v.any? { !valid_item?(_1) }
    end

    true
  end
end

Version data entries

124 entries across 124 versions & 1 rubygems

Version Path
ree_lib-1.0.124 lib/ree_lib/packages/ree_hash/package/ree_hash/contracts/hash_keys_contract.rb
ree_lib-1.0.123 lib/ree_lib/packages/ree_hash/package/ree_hash/contracts/hash_keys_contract.rb
ree_lib-1.0.122 lib/ree_lib/packages/ree_hash/package/ree_hash/contracts/hash_keys_contract.rb
ree_lib-1.0.121 lib/ree_lib/packages/ree_hash/package/ree_hash/contracts/hash_keys_contract.rb
ree_lib-1.0.120 lib/ree_lib/packages/ree_hash/package/ree_hash/contracts/hash_keys_contract.rb
ree_lib-1.0.119 lib/ree_lib/packages/ree_hash/package/ree_hash/contracts/hash_keys_contract.rb
ree_lib-1.0.118 lib/ree_lib/packages/ree_hash/package/ree_hash/contracts/hash_keys_contract.rb
ree_lib-1.0.117 lib/ree_lib/packages/ree_hash/package/ree_hash/contracts/hash_keys_contract.rb
ree_lib-1.0.116 lib/ree_lib/packages/ree_hash/package/ree_hash/contracts/hash_keys_contract.rb
ree_lib-1.0.115 lib/ree_lib/packages/ree_hash/package/ree_hash/contracts/hash_keys_contract.rb
ree_lib-1.0.114 lib/ree_lib/packages/ree_hash/package/ree_hash/contracts/hash_keys_contract.rb
ree_lib-1.0.113 lib/ree_lib/packages/ree_hash/package/ree_hash/contracts/hash_keys_contract.rb
ree_lib-1.0.112 lib/ree_lib/packages/ree_hash/package/ree_hash/contracts/hash_keys_contract.rb
ree_lib-1.0.111 lib/ree_lib/packages/ree_hash/package/ree_hash/contracts/hash_keys_contract.rb
ree_lib-1.0.110 lib/ree_lib/packages/ree_hash/package/ree_hash/contracts/hash_keys_contract.rb
ree_lib-1.0.109 lib/ree_lib/packages/ree_hash/package/ree_hash/contracts/hash_keys_contract.rb
ree_lib-1.0.108 lib/ree_lib/packages/ree_hash/package/ree_hash/contracts/hash_keys_contract.rb
ree_lib-1.0.107 lib/ree_lib/packages/ree_hash/package/ree_hash/contracts/hash_keys_contract.rb
ree_lib-1.0.106 lib/ree_lib/packages/ree_hash/package/ree_hash/contracts/hash_keys_contract.rb
ree_lib-1.0.105 lib/ree_lib/packages/ree_hash/package/ree_hash/contracts/hash_keys_contract.rb