Class: RubyJard::Decorators::InspectionDecorator

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby_jard/decorators/inspection_decorator.rb

Overview

Generate beauty inspection of a particular variable. The inspection doesn’t aim to become a better version of PP. Instead, it’s scope is to generate an overview of a variable within a limited space. So, it only keeps useful information, and tries to reach the very shallow layers of a nested data structure. This class is inspired by Ruby’s PP: github.com/ruby/ruby/blob/master/lib/pp.rb

Constant Summary collapse

PRIMITIVE_TYPES =
{
  # Intertal classes for those values may differ between Ruby versions
  # For example: Bignum is renamed to Integer
  # So, it's safer to use discrete value's class as the key for this mapping.
  true.class.name => :literal,
  false.class.name => :literal,
  1.class.name => :literal,
  1.1.class.name => :literal,
  1.to_r.class.name => :literal, # Rational: (1/1)
  1.to_c.class.name => :literal, # Complex: (1+0i)
  :sym.class.name => :literal,
  //.class.name => :literal, # TODO: create a new class to handle range
  (0..0).class.name => :literal,
  nil.class.name => :text_dim,
  Class.class.name => :text_primary, # Sorry, I lied, Class will never change
  Proc.name => :text_primary # TODO: create a new class to handle proc.
}.freeze

Instance Method Summary collapse

Constructor Details

#initializeInspectionDecorator

Returns a new instance of InspectionDecorator.



40
41
42
43
44
45
46
47
48
49
# File 'lib/ruby_jard/decorators/inspection_decorator.rb', line 40

def initialize
  @klass_decorators = [
    @array_decorator = ArrayDecorator.new(self),
    @string_decorator = StringDecorator.new(self),
    @hash_decorator = HashDecorator.new(self),
    @struct_decorator = StructDecorator.new(self),
    @rails_decorator = RailsDecorator.new(self)
  ]
  @object_decorator = ObjectDecorator.new(self)
end

Instance Method Details

#decorate_multiline(variable, first_line_limit:, lines:, line_limit:, depth: 0) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/ruby_jard/decorators/inspection_decorator.rb', line 65

def decorate_multiline(variable, first_line_limit:, lines:, line_limit:, depth: 0)
  if primitive?(variable)
    return decorate_primitive(variable, first_line_limit)
  end

  @klass_decorators.each do |klass_decorator|
    next unless klass_decorator.match?(variable)

    spans = klass_decorator.decorate_multiline(
      variable,
      first_line_limit: first_line_limit,
      lines: lines,
      line_limit: line_limit,
      depth: depth
    )
    return spans unless spans.nil?
  end
  @object_decorator.decorate_multiline(
    variable,
    first_line_limit: first_line_limit,
    lines: lines,
    line_limit: line_limit,
    depth: depth
  )
end

#decorate_singleline(variable, line_limit:, depth: 0) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/ruby_jard/decorators/inspection_decorator.rb', line 51

def decorate_singleline(variable, line_limit:, depth: 0)
  if primitive?(variable)
    return decorate_primitive(variable, line_limit)
  end

  @klass_decorators.each do |klass_decorator|
    next unless klass_decorator.match?(variable)

    spans = klass_decorator.decorate_singleline(variable, line_limit: line_limit, depth: depth)
    return spans unless spans.nil?
  end
  @object_decorator.decorate_singleline(variable, line_limit: line_limit, depth: depth)
end