Class: SuperDiff::Core::InspectionTree
- Inherits:
-
Object
- Object
- SuperDiff::Core::InspectionTree
- Includes:
- Enumerable
- Defined in:
- lib/super_diff/core/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.
8 9 10 11 12 13 |
# File 'lib/super_diff/core/inspection_tree.rb', line 8 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
25 26 27 |
# File 'lib/super_diff/core/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/core/inspection_tree.rb', line 21 def each(&block) nodes.each(&block) end |
#evaluate_block(*args, &block) ⇒ Object
56 57 58 |
# File 'lib/super_diff/core/inspection_tree.rb', line 56 def evaluate_block(*args, &block) block.call(self, *args) end |
#insert_array_inspection_of(array) ⇒ Object
60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/super_diff/core/inspection_tree.rb', line 60 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 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
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/super_diff/core/inspection_tree.rb', line 72 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 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
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/super_diff/core/inspection_tree.rb', line 99 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.positive? # 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
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/super_diff/core/inspection_tree.rb', line 35 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 |
# File 'lib/super_diff/core/inspection_tree.rb', line 29 def render_to_string(object) nodes.reduce('') do |string, node| string + node.render_to_string(object) end end |