Class: SuperDiff::ObjectInspection::InspectionTree
- Inherits:
-
Object
- Object
- SuperDiff::ObjectInspection::InspectionTree
- Includes:
- Enumerable
- Defined in:
- lib/super_diff/object_inspection/inspection_tree.rb
Defined Under Namespace
Classes: DisallowedNodeError, UpdateTieredLines
Instance Method Summary collapse
- #before_each_callbacks ⇒ Object
- #each(&block) ⇒ Object
- #evaluate_block(*args, &block) ⇒ Object
-
#initialize(disallowed_node_names: [], &block) ⇒ InspectionTree
constructor
A new instance of InspectionTree.
- #insert_array_inspection_of(array) ⇒ Object
- #insert_hash_inspection_of(hash) ⇒ Object
- #insert_separated_list(enumerable, &block) ⇒ Object
- #render_to_lines(object, type:, indentation_level:) ⇒ Object
- #render_to_string(object) ⇒ Object
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 = [] evaluate_block(&block) if block end |
Instance Method Details
#before_each_callbacks ⇒ Object
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(*args, &block) ⇒ Object
54 55 56 |
# File 'lib/super_diff/object_inspection/inspection_tree.rb', line 54 def evaluate_block(*args, &block) block.call(self, *args) end |
#insert_array_inspection_of(array) ⇒ Object
58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/super_diff/object_inspection/inspection_tree.rb', line 58 def insert_array_inspection_of(array) insert_separated_list(array) do |t, 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") t.add_inspection_of(value, **{}) else t.add_inspection_of(*[value, {}]) end end end |
#insert_hash_inspection_of(hash) ⇒ Object
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/super_diff/object_inspection/inspection_tree.rb', line 70 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 |t1, key| if format_keys_as_kwargs # stree-ignore t1.as_prefix_when_rendering_to_lines do |t2| t2.add_text "#{key}: " end else t1.as_prefix_when_rendering_to_lines do |t2| t2.add_inspection_of key, as_lines: false t2.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") t1.add_inspection_of(hash[key], **{}) else t1.add_inspection_of(*[hash[key], {}]) end end end |
#insert_separated_list(enumerable, &block) ⇒ Object
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/super_diff/object_inspection/inspection_tree.rb', line 97 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 |t1| if index > 0 # stree-ignore t1.when_rendering_to_string do |t2| t2.add_text " " end end t1.evaluate_block(value, &block) end end end |
#render_to_lines(object, type:, indentation_level:) ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/super_diff/object_inspection/inspection_tree.rb', line 33 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 |
# File 'lib/super_diff/object_inspection/inspection_tree.rb', line 27 def render_to_string(object) nodes.reduce("") do |string, node| string + node.render_to_string(object) end end |