Class: Contracts::Formatters::InspectWrapper

Inherits:
Object
  • Object
show all
Defined in:
lib/yard-contracts/formatters.rb

Overview

A wrapper class to produce correct inspect behaviour for different contract values - constants, Class contracts, instance contracts etc.

Instance Method Summary collapse

Constructor Details

#initialize(value, full = true) ⇒ InspectWrapper

Returns a new instance of InspectWrapper


41
42
43
# File 'lib/yard-contracts/formatters.rb', line 41

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

Instance Method Details

#delim(value) ⇒ Object


58
59
60
# File 'lib/yard-contracts/formatters.rb', line 58

def delim(value)
  @full ? "(#{value})" : "#{value}"
end

#inspectObject

Inspect different types of contract values. Contracts module prefix will be removed from classes. Custom to_s messages will be wrapped in round brackets to differentiate from standard Strings. Primitive values e.g. 42, true, nil will be left alone.


50
51
52
53
54
55
56
# File 'lib/yard-contracts/formatters.rb', line 50

def inspect
  return '' unless full?
  return @value.inspect if empty_val?
  return @value.to_s if plain?
  return delim(@value.to_s) if has_useful_to_s?
  @value.inspect.gsub(/^Contracts::/, '')
end

#to_sObject

Eliminates eronious quotes in output that plain inspect includes.


63
64
65
# File 'lib/yard-contracts/formatters.rb', line 63

def to_s
  inspect
end