class SuperDiff::ObjectInspection::InspectionTree
Attributes
disallowed_node_names[R]
nodes[R]
Public Class Methods
new(disallowed_node_names: [], &block)
click to toggle source
# 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
Public Instance Methods
before_each_callbacks()
click to toggle source
# 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)
click to toggle source
# File lib/super_diff/object_inspection/inspection_tree.rb, line 21 def each(&block) nodes.each(&block) end
evaluate_block(object, &block)
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# 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:)
click to toggle source
# 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)
click to toggle source
# 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
Private Instance Methods
add_node(node_class, *args, **options, &block)
click to toggle source
# File lib/super_diff/object_inspection/inspection_tree.rb, line 123 def add_node(node_class, *args, **options, &block) if disallowed_node_names.include?(node_class.name) raise DisallowedNodeError.create(node_name: node_class.name) end append_node(build_node(node_class, *args, **options, &block)) end
append_node(node)
click to toggle source
# File lib/super_diff/object_inspection/inspection_tree.rb, line 131 def append_node(node) nodes.push(node) end
build_node(node_class, *args, **options, &block)
click to toggle source
# File lib/super_diff/object_inspection/inspection_tree.rb, line 135 def build_node(node_class, *args, **options, &block) node_class.new(self, *args, **options, &block) end