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

#initializeMergeResult

Initialize a new merge result



25
26
27
28
29
30
31
32
33
# File 'lib/toml/merge/merge_result.rb', line 25

def initialize
  super
  @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



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

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



41
42
43
44
45
46
47
48
49
50
51
# File 'lib/toml/merge/merge_result.rb', line 41

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



59
60
61
62
63
64
# File 'lib/toml/merge/merge_result.rb', line 59

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



80
81
82
83
84
85
86
87
88
89
# File 'lib/toml/merge/merge_result.rb', line 80

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

  (node.start_line..node.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)


103
104
105
# File 'lib/toml/merge/merge_result.rb', line 103

def content
  to_toml
end

#line_countInteger

Get line count

Returns:

  • (Integer)


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

def line_count
  @lines.size
end

#to_sString

Alias for to_toml (used by SmartMerger#merge)

Returns:

  • (String)


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

def to_s
  to_toml
end

#to_tomlString

Get the merged content as a TOML string

Returns:

  • (String)


94
95
96
97
98
99
# File 'lib/toml/merge/merge_result.rb', line 94

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