Class: Rbs::Merge::MergeResult

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

Overview

Result container for RBS file merge operations. Inherits from Ast::Merge::MergeResultBase for shared functionality.

Tracks merged content, decisions made during merge, and provides methods to reconstruct the final merged RBS file.

Examples:

Basic usage

result = MergeResult.new(template_analysis, dest_analysis)
result.add_from_template(0)
result.add_from_destination(1)
merged_content = result.to_s

See Also:

  • Ast::Merge::MergeResultBase

Constant Summary collapse

DECISION_FREEZE_BLOCK =

Decision indicating content was preserved from a freeze block

Returns:

  • (Symbol)
:freeze_block
DECISION_TEMPLATE =

Decision indicating content came from the template

Returns:

  • (Symbol)
:template
DECISION_DESTINATION =

Decision indicating content came from the destination (customization preserved)

Returns:

  • (Symbol)
:destination
DECISION_ADDED =

Decision indicating content was added from template (new in template)

Returns:

  • (Symbol)
:added
DECISION_RECURSIVE =

Decision indicating content was recursively merged

Returns:

  • (Symbol)
:recursive

Instance Method Summary collapse

Constructor Details

#initialize(template_analysis, dest_analysis) ⇒ MergeResult

Initialize a new merge result

Parameters:

  • template_analysis (FileAnalysis)

    Analysis of the template file

  • dest_analysis (FileAnalysis)

    Analysis of the destination file



42
43
44
# File 'lib/rbs/merge/merge_result.rb', line 42

def initialize(template_analysis, dest_analysis)
  super(template_analysis: template_analysis, dest_analysis: dest_analysis)
end

Instance Method Details

#add_freeze_block(freeze_node) ⇒ void

This method returns an undefined value.

Add content from a freeze block

Parameters:

  • freeze_node (FreezeNode)

    The freeze block to add



75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/rbs/merge/merge_result.rb', line 75

def add_freeze_block(freeze_node)
  lines = (freeze_node.start_line..freeze_node.end_line).map do |ln|
    @dest_analysis.line_at(ln)
  end
  @lines.concat(lines)
  @decisions << {
    decision: DECISION_FREEZE_BLOCK,
    source: :destination,
    start_line: freeze_node.start_line,
    end_line: freeze_node.end_line,
    lines: lines.length,
  }
end

#add_from_destination(index, decision: DECISION_DESTINATION) ⇒ void

This method returns an undefined value.

Add content from the destination at the given statement index

Parameters:

  • index (Integer)

    Statement index in destination

  • decision (Symbol) (defaults to: DECISION_DESTINATION)

    Decision type (default: DECISION_DESTINATION)



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

def add_from_destination(index, decision: DECISION_DESTINATION)
  statement = @dest_analysis.statements[index]
  return unless statement

  lines = extract_lines(statement, @dest_analysis)
  @lines.concat(lines)
  @decisions << {decision: decision, source: :destination, index: index, lines: lines.length}
end

#add_from_template(index, decision: DECISION_TEMPLATE) ⇒ void

This method returns an undefined value.

Add content from the template at the given statement index

Parameters:

  • index (Integer)

    Statement index in template

  • decision (Symbol) (defaults to: DECISION_TEMPLATE)

    Decision type (default: DECISION_TEMPLATE)



50
51
52
53
54
55
56
57
# File 'lib/rbs/merge/merge_result.rb', line 50

def add_from_template(index, decision: DECISION_TEMPLATE)
  statement = @template_analysis.statements[index]
  return unless statement

  lines = extract_lines(statement, @template_analysis)
  @lines.concat(lines)
  @decisions << {decision: decision, source: :template, index: index, lines: lines.length}
end

#add_raw(lines, decision:) ⇒ void

This method returns an undefined value.

Add raw content lines

Parameters:

  • lines (Array<String>)

    Lines to add

  • decision (Symbol)

    Decision type



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

def add_raw(lines, decision:)
  @lines.concat(lines)
  @decisions << {decision: decision, source: :raw, lines: lines.length}
end

#add_recursive_merge(merged_content, template_index:, dest_index:) ⇒ void

This method returns an undefined value.

Add recursively merged content

Parameters:

  • merged_content (String)

    The merged content string

  • template_index (Integer)

    Template statement index

  • dest_index (Integer)

    Destination statement index



94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/rbs/merge/merge_result.rb', line 94

def add_recursive_merge(merged_content, template_index:, dest_index:)
  # Split without trailing newlines for consistency with other methods
  lines = merged_content.split("\n", -1)
  # Remove trailing empty element if content ended with newline
  lines.pop if lines.last == ""
  @lines.concat(lines)
  @decisions << {
    decision: DECISION_RECURSIVE,
    source: :merged,
    template_index: template_index,
    dest_index: dest_index,
    lines: lines.length,
  }
end

#empty?Boolean

Check if any content has been added

Returns:

  • (Boolean)


132
133
134
# File 'lib/rbs/merge/merge_result.rb', line 132

def empty?
  @lines.empty?
end

#summaryHash

Get summary of merge decisions

Returns:

  • (Hash)

    Summary with counts by decision type



138
139
140
141
142
143
144
145
# File 'lib/rbs/merge/merge_result.rb', line 138

def summary
  counts = @decisions.group_by { |d| d[:decision] }.transform_values(&:count)
  {
    total_decisions: @decisions.length,
    total_lines: @lines.length,
    by_decision: counts,
  }
end

#to_sString

Convert the merged result to a string

Returns:

  • (String)

    The merged RBS content



120
121
122
123
124
125
126
127
128
# File 'lib/rbs/merge/merge_result.rb', line 120

def to_s
  return "" if @lines.empty?

  # Lines are stored without trailing newlines, so join with newlines
  result = @lines.join("\n")
  # Ensure file ends with newline if content is non-empty
  result += "\n" unless result.end_with?("\n")
  result
end