Class: SuperDiff::ObjectInspection::InspectionTree

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/super_diff/object_inspection/inspection_tree.rb

Defined Under Namespace

Classes: DisallowedNodeError, UpdateTieredLines

Instance Method Summary collapse

Constructor Details

#initialize(disallowed_node_names: [], &block) ⇒ InspectionTree

Returns a new instance of InspectionTree.



6
7
8
9
10
11
12
13
# File 'lib/super_diff/object_inspection/inspection_tree.rb', line 6

def initialize(disallowed_node_names: [], &block)
  @disallowed_node_names = disallowed_node_names
  @nodes = []

  if block
    instance_eval(&block)
  end
end

Instance Method Details

#before_each_callbacksObject



25
26
27
# File 'lib/super_diff/object_inspection/inspection_tree.rb', line 25

def before_each_callbacks
  @_before_each_callbacks ||= Hash.new { |h, k| h[k] = [] }
end

#each(&block) ⇒ Object



21
22
23
# File 'lib/super_diff/object_inspection/inspection_tree.rb', line 21

def each(&block)
  nodes.each(&block)
end

#evaluate_block(object, &block) ⇒ Object



58
59
60
# File 'lib/super_diff/object_inspection/inspection_tree.rb', line 58

def evaluate_block(object, &block)
  instance_exec(object, &block)
end

#insert_array_inspection_of(array) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
# File 'lib/super_diff/object_inspection/inspection_tree.rb', line 62

def insert_array_inspection_of(array)
  insert_separated_list(array) do |value|
    # Have to do these shenanigans so that if value is a hash, Ruby
    # doesn't try to interpret it as keyword args
    if SuperDiff::Helpers.ruby_version_matches?(">= 2.7.1")
      add_inspection_of(value, **{})
    else
      add_inspection_of(*[value, {}])
    end
  end
end

#insert_hash_inspection_of(hash) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/super_diff/object_inspection/inspection_tree.rb', line 74

def insert_hash_inspection_of(hash)
  keys = hash.keys

  format_keys_as_kwargs = keys.all? do |key|
    key.is_a?(Symbol)
  end

  insert_separated_list(keys) do |key|
    if format_keys_as_kwargs
      as_prefix_when_rendering_to_lines do
        add_text "#{key}: "
      end
    else
      as_prefix_when_rendering_to_lines do
        add_inspection_of key, as_lines: false
        add_text " => "
      end
    end

    # Have to do these shenanigans so that if hash[key] is a hash, Ruby
    # doesn't try to interpret it as keyword args
    if SuperDiff::Helpers.ruby_version_matches?(">= 2.7.1")
      add_inspection_of(hash[key], **{})
    else
      add_inspection_of(*[hash[key], {}])
    end
  end
end

#insert_separated_list(enumerable, &block) ⇒ Object



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/super_diff/object_inspection/inspection_tree.rb', line 103

def insert_separated_list(enumerable, &block)
  enumerable.each_with_index do |value, index|
    as_lines_when_rendering_to_lines(
      add_comma: index < enumerable.size - 1,
    ) do
      if index > 0
        when_rendering_to_string do
          add_text " "
        end
      end

      evaluate_block(value, &block)
    end
  end
end

#render_to_lines(object, type:, indentation_level:) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/super_diff/object_inspection/inspection_tree.rb', line 36

def render_to_lines(object, type:, indentation_level:)
  nodes.
    each_with_index.
    reduce([TieredLines.new, "", ""]) do |
      (tiered_lines, prelude, prefix),
      (node, index)
    |
      UpdateTieredLines.call(
        object: object,
        type: type,
        indentation_level: indentation_level,
        nodes: nodes,
        tiered_lines: tiered_lines,
        prelude: prelude,
        prefix: prefix,
        node: node,
        index: index,
      )
    end.
    first
end

#render_to_string(object) ⇒ Object



29
30
31
32
33
34
# File 'lib/super_diff/object_inspection/inspection_tree.rb', line 29

def render_to_string(object)
  nodes.reduce("") do |string, node|
    result = node.render_to_string(object)
    string + result
  end
end