class Contracts::Formatters::Expected

Used to format contracts for the `Expected:` field of error output.

Public Class Methods

new(contract, full = true) click to toggle source

@param full [Boolean] if false only unique `to_s` values will be output,

non unique values become empty string.
# File lib/contracts/formatters.rb, line 8
def initialize(contract, full = true)
  @contract = contract
  @full     = full
end

Public Instance Methods

array_contract(array) click to toggle source

Formats Array contracts.

# File lib/contracts/formatters.rb, line 29
def array_contract(array)
  @full = true
  array.map { |v| InspectWrapper.create(contract(v), @full) }.inspect
end
contract(contract = @contract) click to toggle source

Formats any type of Contract.

# File lib/contracts/formatters.rb, line 14
def contract(contract = @contract)
  return hash_contract(contract)  if contract.is_a?(Hash)
  return array_contract(contract) if contract.is_a?(Array)
  InspectWrapper.create(contract, @full)
end
hash_contract(hash) click to toggle source

Formats Hash contracts.

# File lib/contracts/formatters.rb, line 21
def hash_contract(hash)
  @full = true # Complex values output completely, overriding @full
  hash.inject({}) do |repr, (k, v)|
    repr.merge(k => InspectWrapper.create(contract(v), @full))
  end.inspect
end