class Contracts::Formatters::Expected
Used to format contracts for the `Expected:` field of error output.
Public Class Methods
new(contract, full = true)
click to toggle source
@param full [Boolean] if false only unique `to_s` values will be output,
non unique values become empty string.
# File lib/contracts/formatters.rb, line 8 def initialize(contract, full = true) @contract, @full = contract, full end
Public Instance Methods
array_contract(array)
click to toggle source
Formats Array contracts.
# 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)
click to toggle source
Formats any type of Contract.
# 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)
click to toggle source
Formats Hash contracts.
# 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