Class: Contracts::Formatters::Expected

Inherits:
Object
  • Object
show all
Defined in:
lib/contracts-ruby2/lib/contracts/formatters.rb,
lib/contracts-ruby3/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.



10
11
12
# File 'lib/contracts-ruby2/lib/contracts/formatters.rb', line 10

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

Instance Method Details

#array_contract(array) ⇒ Object

Formats Array contracts.



34
35
36
37
# File 'lib/contracts-ruby2/lib/contracts/formatters.rb', line 34

def array_contract(array)
  @full = true
  array.map { |v| InspectWrapper.create(contract(v), @full) }
end

#contract(contract = @contract) ⇒ Object

Formats any type of Contract.



15
16
17
18
19
20
21
22
23
# File 'lib/contracts-ruby2/lib/contracts/formatters.rb', line 15

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.



26
27
28
29
30
31
# File 'lib/contracts-ruby2/lib/contracts/formatters.rb', line 26

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
end