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
# File 'lib/contracts/formatters.rb', line 8

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

Instance Method Details

#array_contract(array) ⇒ Object

Formats Array contracts.


32
33
34
35
# File 'lib/contracts/formatters.rb', line 32

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.


13
14
15
16
17
18
19
20
21
# File 'lib/contracts/formatters.rb', line 13

def contract(contract = @contract)
  if contract.is_a?(Hash)
    hash_contract(contract)
  elsif contract.is_a?(Array)
    array_contract(contract)
  else
    InspectWrapper.create(contract, @full)
  end
end

#hash_contract(hash) ⇒ Object

Formats Hash contracts.


24
25
26
27
28
29
# File 'lib/contracts/formatters.rb', line 24

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