Class: RubyJard::Decorators::ObjectDecorator

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

Overview

Default decorator for non-primitive data structure. It is aimed to replace default ‘inspect`. If a variable re-implement `#inspect`, it hornors this decision, but still try to parse the result. Otherwise, it use `Kernel#to_s`, and try to push instance variables into the result.

Constant Summary collapse

DEFAULT_INSPECTION_PATTERN =
/#<(.*:0x[0-9a-z]+)(.*)>/i.freeze

Instance Method Summary collapse

Constructor Details

#initialize(generic_decorator) ⇒ ObjectDecorator

Returns a new instance of ObjectDecorator.



13
14
15
16
# File 'lib/ruby_jard/decorators/object_decorator.rb', line 13

def initialize(generic_decorator)
  @generic_decorator = generic_decorator
  @attributes_decorator = RubyJard::Decorators::AttributesDecorator.new(generic_decorator)
end

Instance Method Details

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



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/ruby_jard/decorators/object_decorator.rb', line 26

def decorate_multiline(variable, first_line_limit:, lines:, line_limit:, depth: 0)
  singleline = decorate_singleline(variable, line_limit: first_line_limit)
  return [singleline] if singleline.map(&:content_length).sum < line_limit

  spans = [decorate_native_inspection(variable, line_limit: first_line_limit, with_children: false)]

  item_count = 0
  instance_variables = RubyJard::Reflection.call_instance_variables(variable)
  instance_variables.each do |key|
    spans << @attributes_decorator.pair(
      key, RubyJard::Reflection.call_instance_variable_get(variable, key),
      line_limit: line_limit, process_key: false, depth: depth + 1
    )

    item_count += 1
    break if item_count >= lines - 2
  end

  if instance_variables.length > item_count
    spans << [
      RubyJard::Span.new(
        content: "#{instance_variables.length - item_count} more...",
        margin_left: 2, styles: :text_dim
      )
    ]
  end

  spans
end

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



18
19
20
21
22
23
24
# File 'lib/ruby_jard/decorators/object_decorator.rb', line 18

def decorate_singleline(variable, line_limit:, depth: 0)
  if native_inspect?(variable)
    decorate_native_inspection(variable, line_limit: line_limit, depth: depth)
  else
    decorate_custom_inspection(variable, line_limit: line_limit)
  end
end