module T::Props::PrettyPrintable::DecoratorMethods

Public Instance Methods

inspect_instance(instance, multiline: false, indent: ' ') click to toggle source
# File lib/types/props/pretty_printable.rb, line 30
def inspect_instance(instance, multiline: false, indent: '  ')
  components =
    inspect_instance_components(
      instance,
      multiline: multiline,
      indent: indent
    )
      .reject(&:empty?)

  # Not using #<> here as that makes pry highlight these objects
  # as if they were all comments, whereas this makes them look
  # like the structured thing they are.
  if multiline
    "#{components[0]}:\n" + T.must(components[1..-1]).join("\n")
  else
    "<#{components.join(' ')}>"
  end
end
valid_rule_key?(key) click to toggle source
Calls superclass method
# File lib/types/props/pretty_printable.rb, line 22
def valid_rule_key?(key)
  super || key == :inspect
end

Private Instance Methods

inspect_instance_components(instance, multiline:, indent:) click to toggle source
# File lib/types/props/pretty_printable.rb, line 53
        def inspect_instance_components(instance, multiline:, indent:)
  pretty_props = T.unsafe(self).all_props.map do |prop|
    [prop, inspect_prop_value(instance, prop, multiline: multiline, indent: indent)]
  end

  joined_props = join_props_with_pretty_values(
    pretty_props,
    multiline: multiline,
    indent: indent
  )

  [
    T.unsafe(self).decorated_class.to_s,
    joined_props,
  ]
end
inspect_prop_value(instance, prop, multiline:, indent:) click to toggle source
# File lib/types/props/pretty_printable.rb, line 75
        def inspect_prop_value(instance, prop, multiline:, indent:)
  val = T.unsafe(self).get(instance, prop)
  rules = T.unsafe(self).prop_rules(prop)
  if (custom_inspect = rules[:inspect])
    if T::Utils.arity(custom_inspect) == 1
      custom_inspect.call(val)
    else
      custom_inspect.call(val, {multiline: multiline, indent: indent})
    end
  elsif rules[:sensitivity] && !rules[:sensitivity].empty? && !val.nil?
    "<REDACTED #{rules[:sensitivity].join(', ')}>"
  else
    val.inspect
  end
end
join_props_with_pretty_values(pretty_kvs, multiline:, indent: ' ') click to toggle source
# File lib/types/props/pretty_printable.rb, line 95
        def join_props_with_pretty_values(pretty_kvs, multiline:, indent: '  ')
  pairs = pretty_kvs
    .sort_by {|k, _v| k.to_s}
    .map {|k, v| "#{k}=#{v}"}

  if multiline
    indent + pairs.join("\n#{indent}")
  else
    pairs.join(', ')
  end
end