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
# File 'lib/super_diff/object_inspection/inspection_tree.rb', line 6

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

  instance_eval(&block) if block
end

Instance Method Details

#before_each_callbacksObject



23
24
25
# File 'lib/super_diff/object_inspection/inspection_tree.rb', line 23

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

#each(&block) ⇒ Object



19
20
21
# File 'lib/super_diff/object_inspection/inspection_tree.rb', line 19

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

#evaluate_block(object, &block) ⇒ Object



55
56
57
# File 'lib/super_diff/object_inspection/inspection_tree.rb', line 55

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

#insert_array_inspection_of(array) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
# File 'lib/super_diff/object_inspection/inspection_tree.rb', line 59

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



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/super_diff/object_inspection/inspection_tree.rb', line 71

def insert_hash_inspection_of(hash)
  keys = hash.keys

  format_keys_as_kwargs = keys.all? { |key| key.is_a?(Symbol) }

  insert_separated_list(keys) do |key|
    if format_keys_as_kwargs
      as_prefix_when_rendering_to_lines { add_text "#{key}: " }
    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



96
97
98
99
100
101
102
103
104
105
106
# File 'lib/super_diff/object_inspection/inspection_tree.rb', line 96

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
      when_rendering_to_string { add_text " " } if index > 0

      evaluate_block(value, &block)
    end
  end
end

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



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

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



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

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