Module: T::Props::PrettyPrintable

Includes:
Plugin
Included in:
Serializable
Defined in:
lib/types/props/pretty_printable.rb

Defined Under Namespace

Modules: DecoratorMethods

Constant Summary

Constants included from Helpers

Helpers::Private

Instance Method Summary collapse

Methods included from Helpers

#abstract!, #final!, #interface!, #mixes_in_class_methods, #requires_ancestor, #sealed!

Methods included from Sig

#sig

Instance Method Details

#inspectObject

Return a string representation of this object and all of its props in a single line



36
37
38
39
40
# File 'lib/types/props/pretty_printable.rb', line 36

def inspect
  string = +""
  PP.singleline_pp(self, string)
  string
end

#pretty_inspectObject

Return a pretty string representation of this object and all of its props



43
44
45
46
47
# File 'lib/types/props/pretty_printable.rb', line 43

def pretty_inspect
  string = +""
  PP.pp(self, string)
  string
end

#pretty_print(pp) ⇒ Object

Override the PP gem with something that’s similar, but gives us a hook to do redaction and customization



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/types/props/pretty_printable.rb', line 9

def pretty_print(pp)
  klass = T.unsafe(T.cast(self, Object).class).decorator
  multiline = pp.is_a?(PP)
  pp.group(1, "<#{klass.inspect_class_with_decoration(self)}", ">") do
    klass.all_props.sort.each do |prop|
      pp.breakable
      val = klass.get(self, prop)
      rules = klass.prop_rules(prop)
      pp.text("#{prop}=")
      if (custom_inspect = rules[:inspect])
        inspected = if T::Utils.arity(custom_inspect) == 1
          custom_inspect.call(val)
        else
          custom_inspect.call(val, {multiline: multiline})
        end
        pp.text(inspected.nil? ? "nil" : inspected)
      elsif rules[:sensitivity] && !rules[:sensitivity].empty? && !val.nil?
        pp.text("<REDACTED #{rules[:sensitivity].join(', ')}>")
      else
        val.pretty_print(pp)
      end
    end
    klass.pretty_print_extra(self, pp)
  end
end