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.



12
13
14
# File 'lib/contracts/formatters.rb', line 12

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

Instance Method Details

#array_contract(array) ⇒ Object

Formats Array contracts.



37
38
39
40
# File 'lib/contracts/formatters.rb', line 37

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

#contract(contract = @contract) ⇒ Object

Formats any type of Contract.



17
18
19
20
21
22
23
24
25
26
# File 'lib/contracts/formatters.rb', line 17

def contract(contract = @contract)
  case contract
  when Hash
    hash_contract(contract)
  when Array
    array_contract(contract)
  else
    InspectWrapper.create(contract, full: @full)
  end
end

#hash_contract(hash) ⇒ Object

Formats Hash contracts.



29
30
31
32
33
34
# File 'lib/contracts/formatters.rb', line 29

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: @full))
  end
end