Class: Contracts::Formatters::Expected

Inherits:
Object
  • Object
show all
Defined in:
lib/contracts/formatters.rb

Overview

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

Instance Method Summary collapse

Constructor Details

#initialize(contract, full = true) ⇒ Expected

Returns a new instance of Expected.

Parameters:

  • full (Boolean) (defaults to: true)

    if false only unique ‘to_s` values will be output, non unique values become empty string.



8
9
10
11
# File 'lib/contracts/formatters.rb', line 8

def initialize(contract, full = true)
  @contract = contract
  @full     = full
end

Instance Method Details

#array_contract(array) ⇒ Object

Formats Array contracts.



29
30
31
32
# 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) ⇒ Object

Formats any type of Contract.



14
15
16
17
18
# 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) ⇒ Object

Formats Hash contracts.



21
22
23
24
25
26
# 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