Module: Contracts::Formatters::InspectWrapper

Included in:
ClassInspectWrapper, ObjectInspectWrapper
Defined in:
lib/contracts-ruby2/lib/contracts/formatters.rb,
lib/contracts-ruby3/lib/contracts/formatters.rb

Overview

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

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.create(value, full: true) ⇒ ClassInspectWrapper, ObjectInspectWrapper

InspectWrapper is a factory, will never be an instance



45
46
47
48
49
50
51
# File 'lib/contracts-ruby2/lib/contracts/formatters.rb', line 45

def self.create(value, full = true)
  if value.class == Class
    ClassInspectWrapper
  else
    ObjectInspectWrapper
  end.new(value, full)
end

Instance Method Details

#delim(value) ⇒ Object



72
73
74
# File 'lib/contracts-ruby2/lib/contracts/formatters.rb', line 72

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

#initialize(value, full) ⇒ Object

Parameters:

  • full (Boolean)

    if false only unique ‘to_s` values will be output, non unique values become empty string.



55
56
57
# File 'lib/contracts-ruby2/lib/contracts/formatters.rb', line 55

def initialize(value, full)
  @value, @full = value, full
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.



64
65
66
67
68
69
70
# File 'lib/contracts-ruby2/lib/contracts/formatters.rb', line 64

def inspect
  return "" unless full?
  return @value.inspect if empty_val?
  return @value.to_s if plain?
  return delim(@value.to_s) if useful_to_s?
  useful_inspect
end

#to_sObject

Eliminates eronious quotes in output that plain inspect includes.



77
78
79
# File 'lib/contracts-ruby2/lib/contracts/formatters.rb', line 77

def to_s
  inspect
end