Class: Contracts::Formatters::Expected
- Inherits:
-
Object
- Object
- Contracts::Formatters::Expected
- Defined in:
- lib/contracts/formatters.rb
Overview
Used to format contracts for the ‘Expected:` field of error output.
Instance Method Summary collapse
-
#array_contract(array) ⇒ Object
Formats Array contracts.
-
#contract(contract = @contract) ⇒ Object
Formats any type of Contract.
-
#hash_contract(hash) ⇒ Object
Formats Hash contracts.
-
#initialize(contract, full: true) ⇒ Expected
constructor
A new instance of Expected.
Constructor Details
#initialize(contract, full: true) ⇒ Expected
Returns a new instance of Expected.
15 16 17 |
# File 'lib/contracts/formatters.rb', line 15 def initialize(contract, full: true) @contract, @full = contract, full end |
Instance Method Details
#array_contract(array) ⇒ Object
Formats Array contracts.
40 41 42 43 |
# File 'lib/contracts/formatters.rb', line 40 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.
20 21 22 23 24 25 26 27 28 29 |
# File 'lib/contracts/formatters.rb', line 20 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.
32 33 34 35 36 37 |
# File 'lib/contracts/formatters.rb', line 32 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 |