Class: Toml::Merge::MergeResult

Inherits:
Ast::Merge::MergeResultBase
  • Object
show all
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



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

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



22
23
24
# File 'lib/toml/merge/merge_result.rb', line 22

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



76
77
78
# File 'lib/toml/merge/merge_result.rb', line 76

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



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

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



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

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



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

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)


114
115
116
# File 'lib/toml/merge/merge_result.rb', line 114

def content
  to_toml
end

#line_countInteger

Get line count

Returns:

  • (Integer)


126
127
128
# File 'lib/toml/merge/merge_result.rb', line 126

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)



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

def lines_array
  @lines
end

#to_sString

Alias for to_toml (used by SmartMerger#merge)

Returns:

  • (String)


120
121
122
# File 'lib/toml/merge/merge_result.rb', line 120

def to_s
  to_toml
end

#to_tomlString

Get the merged content as a TOML string

Returns:

  • (String)


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

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