module Contracts::Formatters::InspectWrapper
A wrapper class to produce correct inspect behaviour for different contract values - constants, Class contracts, instance contracts etc.
Public Class Methods
create(value, full = true)
click to toggle source
InspectWrapper
is a factory, will never be an instance @return [ClassInspectWrapper, ObjectInspectWrapper]
# File lib/contracts/formatters.rb, line 40 def self.create(value, full = true) inspector_klass(value).new(value, full) end
inspector_klass(value)
click to toggle source
# File lib/contracts/formatters.rb, line 44 def self.inspector_klass(value) return ClassInspectWrapper if value.class == Class ObjectInspectWrapper end
new(value, full)
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 51 def initialize(value, full) @value = value @full = full end
Public Instance Methods
delim(value)
click to toggle source
# File lib/contracts/formatters.rb, line 69 def delim(value) @full ? "(#{value})" : value.to_s end
inspect()
click to toggle source
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.
# File lib/contracts/formatters.rb, line 61 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_s()
click to toggle source
Eliminates erroneous quotes in output that plain inspect includes.
# File lib/contracts/formatters.rb, line 74 def to_s inspect end
Private Instance Methods
empty_to_s?()
click to toggle source
# File lib/contracts/formatters.rb, line 100 def empty_to_s? @value.to_s.empty? end
empty_val?()
click to toggle source
# File lib/contracts/formatters.rb, line 80 def empty_val? @value.nil? || @value == "" end
full?()
click to toggle source
# File lib/contracts/formatters.rb, line 84 def full? @full || @value.is_a?(Hash) || @value.is_a?(Array) || (!plain? && useful_to_s?) end
plain?()
click to toggle source
Not a type of contract that can have a custom to_s
defined
# File lib/contracts/formatters.rb, line 92 def plain? !@value.is_a?(Builtin::CallableClass) && @value.class != Class end
strip_prefix(val)
click to toggle source
# File lib/contracts/formatters.rb, line 104 def strip_prefix(val) val.gsub(/^Contracts::Builtin::/, "") end
useful_to_s?()
click to toggle source
# File lib/contracts/formatters.rb, line 96 def useful_to_s? !empty_to_s? && custom_to_s? end