Class: Contracts::Formatters::Expected

Inherits:
Object
  • Object
show all
Defined in:
lib/yard-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


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

def array_contract(array)
  @full = true
  array.map{ |v| InspectWrapper.new(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/yard-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.new(contract, @full)
  end
end

#hash_contract(hash) ⇒ Object

Formats Hash contracts.


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

def hash_contract(hash)
  @full = true
  hash.inject({}) { |repr, (k, v)|
    repr.merge(k => InspectWrapper.new(contract(v), @full))
  }.inspect
end