Class: Canon::DiffFormatter::ByObject::BaseFormatter

Inherits:
Object
  • Object
show all
Defined in:
lib/canon/diff_formatter/by_object/base_formatter.rb

Overview

Base class for by-object diff formatters Provides tree visualization for semantic differences

Direct Known Subclasses

JsonFormatter, XmlFormatter

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(use_color: true, visualization_map: nil, show_diffs: :all, theme: nil) ⇒ BaseFormatter

Returns a new instance of BaseFormatter.



14
15
16
17
18
19
20
21
# File 'lib/canon/diff_formatter/by_object/base_formatter.rb', line 14

def initialize(use_color: true, visualization_map: nil,
show_diffs: :all, theme: nil)
  @use_color = use_color
  @visualization_map = visualization_map ||
    Canon::DiffFormatter::DEFAULT_VISUALIZATION_MAP
  @show_diffs = show_diffs
  @theme = theme
end

Instance Attribute Details

#use_colorObject (readonly)

Returns the value of attribute use_color.



12
13
14
# File 'lib/canon/diff_formatter/by_object/base_formatter.rb', line 12

def use_color
  @use_color
end

#visualization_mapObject (readonly)

Returns the value of attribute visualization_map.



12
13
14
# File 'lib/canon/diff_formatter/by_object/base_formatter.rb', line 12

def visualization_map
  @visualization_map
end

Class Method Details

.for_format(format, use_color: true, visualization_map: nil, show_diffs: :all, theme: nil) ⇒ Object

Factory method to create format-specific formatter



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
# File 'lib/canon/diff_formatter/by_object/base_formatter.rb', line 75

def self.for_format(format, use_color: true, visualization_map: nil,
show_diffs: :all, theme: nil)
  case format
  when :xml, :html
    require_relative "xml_formatter"
    XmlFormatter.new(use_color: use_color,
                     visualization_map: visualization_map,
                     show_diffs: show_diffs,
                     theme: theme)
  when :json
    require_relative "json_formatter"
    JsonFormatter.new(use_color: use_color,
                      visualization_map: visualization_map,
                      show_diffs: show_diffs,
                      theme: theme)
  when :yaml
    require_relative "yaml_formatter"
    YamlFormatter.new(use_color: use_color,
                      visualization_map: visualization_map,
                      show_diffs: show_diffs,
                      theme: theme)
  else
    new(use_color: use_color, visualization_map: visualization_map,
        show_diffs: show_diffs, theme: theme)
  end
end

Instance Method Details

#format(differences, _format) ⇒ String

Format differences for display

Parameters:

  • differences (ComparisonResult, Array)

    ComparisonResult object or legacy Array

  • format (Symbol)

    Format type (:xml, :html, :json, :yaml)

Returns:

  • (String)

    Formatted output



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/canon/diff_formatter/by_object/base_formatter.rb', line 27

def format(differences, _format)
  # Handle both ComparisonResult (production) and Array (low-level tests)
  if differences.respond_to?(:equivalent?)
    # ComparisonResult object
    return success_message if differences.equivalent?

    diffs_array = differences.differences
  else
    # Legacy Array
    return success_message if differences.empty?

    diffs_array = differences
  end

  output = []
  output << colorize("Visual Diff:",
                     theme_color(:informative, :content) || :cyan, :bold)

  # Filter differences for display based on show_diffs setting
  filtered_diffs = filter_differences_for_display(diffs_array)

  # Group differences by path for tree building
  tree = build_diff_tree(filtered_diffs)

  # Render tree with line counting
  @line_count = 0
  @max_lines = get_max_diff_lines
  rendered = render_tree(tree)

  # Add truncation notice if needed
  if @truncated
    trunc_color = structure_color(:line_number) || :yellow
    rendered += "\n\n"
    rendered += colorize(
      "... Output truncated at #{@max_lines} lines ...", trunc_color, :bold
    )
    rendered += "\n"
    rendered += colorize(
      "Increase limit via CANON_MAX_DIFF_LINES or config.diff.max_diff_lines", trunc_color
    )
  end

  output << rendered

  output.join("\n")
end