Class: Toml::Merge::MergeResult

Inherits:
Ast::Merge::MergeResultBase
  • Object
show all
Includes:
Ast::Merge::StructuredReviewApplySupport
Defined in:
lib/toml/merge/merge_result.rb

Overview

Tracks the result of a merge operation, including the merged content, decisions made, and statistics.

Inherits decision constants and base functionality from Ast::Merge::MergeResultBase.

Examples:

Basic usage

result = MergeResult.new
result.add_line('key = "value"', decision: :kept_template, source: :template)
result.to_toml # => 'key = "value"\n'

Constant Summary collapse

DECISION_KEPT_TEMPLATE =

Inherit decision constants from base class

Ast::Merge::MergeResultBase::DECISION_KEPT_TEMPLATE
DECISION_KEPT_DEST =
Ast::Merge::MergeResultBase::DECISION_KEPT_DEST
DECISION_MERGED =
Ast::Merge::MergeResultBase::DECISION_MERGED
DECISION_ADDED =
Ast::Merge::MergeResultBase::DECISION_ADDED

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(**options) ⇒ MergeResult

Initialize a new merge result

Parameters:

  • options (Hash)

    Additional options for forward compatibility



33
34
35
36
37
38
39
40
41
# File 'lib/toml/merge/merge_result.rb', line 33

def initialize(**options)
  super(**options)
  @statistics = {
    template_lines: 0,
    dest_lines: 0,
    merged_lines: 0,
    total_decisions: 0
  }
end

Instance Attribute Details

#statisticsHash (readonly)

Returns Statistics about the merge.

Returns:

  • (Hash)

    Statistics about the merge



24
25
26
# File 'lib/toml/merge/merge_result.rb', line 24

def statistics
  @statistics
end

Instance Method Details

#add_blank_line(decision: DECISION_MERGED, source: :merged) ⇒ Object

Add a blank line

Parameters:

  • decision (Symbol) (defaults to: DECISION_MERGED)

    Decision for the blank line

  • source (Symbol) (defaults to: :merged)

    Source



78
79
80
# File 'lib/toml/merge/merge_result.rb', line 78

def add_blank_line(decision: DECISION_MERGED, source: :merged)
  add_line('', decision: decision, source: source)
end

#add_line(line, decision:, source:, original_line: nil) ⇒ Object

Add a single line to the result

Parameters:

  • line (String)

    Line content

  • decision (Symbol)

    Decision that led to this line

  • source (Symbol)

    Source of the line (:template, :destination, :merged)

  • original_line (Integer, nil) (defaults to: nil)

    Original line number



49
50
51
52
53
54
55
56
57
58
59
# File 'lib/toml/merge/merge_result.rb', line 49

def add_line(line, decision:, source:, original_line: nil)
  @lines << {
    content: line,
    decision: decision,
    source: source,
    original_line: original_line
  }

  track_statistics(decision, source)
  track_decision(decision, source, line: original_line)
end

#add_lines(lines, decision:, source:, start_line: nil) ⇒ Object

Add multiple lines to the result

Parameters:

  • lines (Array<String>)

    Lines to add

  • decision (Symbol)

    Decision for all lines

  • source (Symbol)

    Source of the lines

  • start_line (Integer, nil) (defaults to: nil)

    Starting original line number



67
68
69
70
71
72
# File 'lib/toml/merge/merge_result.rb', line 67

def add_lines(lines, decision:, source:, start_line: nil)
  lines.each_with_index do |line, idx|
    original_line = start_line ? start_line + idx : nil
    add_line(line, decision: decision, source: source, original_line: original_line)
  end
end

#add_node(node, decision:, source:, analysis:) ⇒ Object

Add content from a node wrapper

Parameters:

  • node (NodeWrapper)

    Node to add

  • decision (Symbol)

    Decision that led to keeping this node

  • source (Symbol)

    Source of the node

  • analysis (FileAnalysis)

    Analysis for accessing source lines



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/toml/merge/merge_result.rb', line 88

def add_node(node, decision:, source:, analysis:)
  return unless node.start_line

  # Use effective_end_line for tables to include associated pairs on Citrus backend
  # (Citrus has flat structure where pairs are siblings, not children of tables)
  end_line = node.respond_to?(:effective_end_line) ? node.effective_end_line : node.end_line
  return unless end_line

  (node.start_line..end_line).each do |line_num|
    line = analysis.line_at(line_num)
    next unless line

    add_line(line.chomp, decision: decision, source: source, original_line: line_num)
  end
end

#contentString

Alias for to_toml

Returns:

  • (String)


116
117
118
# File 'lib/toml/merge/merge_result.rb', line 116

def content
  to_toml
end

#line_countInteger

Get line count

Returns:

  • (Integer)


128
129
130
# File 'lib/toml/merge/merge_result.rb', line 128

def line_count
  @lines.size
end

#lines_arrayArray<Hash>

Returns Raw line data for post-processing (e.g., sorting).

Returns:

  • (Array<Hash>)

    Raw line data for post-processing (e.g., sorting)



27
28
29
# File 'lib/toml/merge/merge_result.rb', line 27

def lines_array
  @lines
end

#to_sString

Alias for to_toml (used by SmartMerger#merge)

Returns:

  • (String)


122
123
124
# File 'lib/toml/merge/merge_result.rb', line 122

def to_s
  to_toml
end

#to_tomlString

Get the merged content as a TOML string

Returns:

  • (String)


107
108
109
110
111
112
# File 'lib/toml/merge/merge_result.rb', line 107

def to_toml
  content = @lines.map { |l| l[:content] }.join("\n")
  # Ensure trailing newline
  content += "\n" unless content.end_with?("\n") || content.empty?
  content
end