Class: Prism::Merge::MethodMatchRefiner

Inherits:
Ast::Merge::MatchRefinerBase
  • Object
show all
Defined in:
lib/prism/merge/method_match_refiner.rb

Overview

Match refiner for Ruby methods that didn't match by exact signature.

This refiner uses fuzzy matching to pair methods that have:

  • Similar names (e.g., process_user vs process_users)
  • Same name but different parameter signatures
  • Renamed methods that perform similar functions

The matching algorithm considers:

  • Method name similarity (Levenshtein distance)
  • Parameter count and name similarity
  • Method body similarity (optional, for high-confidence matches)

Examples:

Basic usage

refiner = MethodMatchRefiner.new(threshold: 0.6)
matches = refiner.call(template_nodes, dest_nodes)

With custom weights

refiner = MethodMatchRefiner.new(
  threshold: 0.5,
  name_weight: 0.6,
  params_weight: 0.4
)

See Also:

  • Ast::Merge::MatchRefinerBase

Constant Summary collapse

DEFAULT_NAME_WEIGHT =

Default weight for method name similarity

Ruby::Merge::MethodSimilarity::DEFAULT_NAME_WEIGHT
DEFAULT_PARAMS_WEIGHT =

Default weight for parameter similarity

Ruby::Merge::MethodSimilarity::DEFAULT_PARAMS_WEIGHT

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(threshold: DEFAULT_THRESHOLD, name_weight: DEFAULT_NAME_WEIGHT, params_weight: DEFAULT_PARAMS_WEIGHT, **options) ⇒ MethodMatchRefiner

Initialize a method match refiner.

Parameters:

  • threshold (Float) (defaults to: DEFAULT_THRESHOLD)

    Minimum score to accept a match (default: 0.5)

  • name_weight (Float) (defaults to: DEFAULT_NAME_WEIGHT)

    Weight for name similarity (default: 0.7)

  • params_weight (Float) (defaults to: DEFAULT_PARAMS_WEIGHT)

    Weight for parameter similarity (default: 0.3)



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

def initialize(threshold: DEFAULT_THRESHOLD, name_weight: DEFAULT_NAME_WEIGHT,
               params_weight: DEFAULT_PARAMS_WEIGHT, **options)
  super(threshold: threshold, node_types: [:def], **options)
  @name_weight = name_weight
  @params_weight = params_weight
  @method_similarity = Ruby::Merge::MethodSimilarity.new(
    name_weight: name_weight,
    params_weight: params_weight
  )
end

Instance Attribute Details

#name_weightFloat (readonly)

Returns Weight for name similarity (0.0-1.0).

Returns:

  • (Float)

    Weight for name similarity (0.0-1.0)



37
38
39
# File 'lib/prism/merge/method_match_refiner.rb', line 37

def name_weight
  @name_weight
end

#params_weightFloat (readonly)

Returns Weight for parameter similarity (0.0-1.0).

Returns:

  • (Float)

    Weight for parameter similarity (0.0-1.0)



40
41
42
# File 'lib/prism/merge/method_match_refiner.rb', line 40

def params_weight
  @params_weight
end

Instance Method Details

#call(template_nodes, dest_nodes, _context = {}) ⇒ Array<MatchResult>

Find matches between unmatched method definitions.

Parameters:

  • template_nodes (Array)

    Unmatched nodes from template

  • dest_nodes (Array)

    Unmatched nodes from destination

  • context (Hash)

    Additional context

Returns:

  • (Array<MatchResult>)

    Array of method matches



64
65
66
67
68
69
70
71
72
73
# File 'lib/prism/merge/method_match_refiner.rb', line 64

def call(template_nodes, dest_nodes, _context = {})
  template_methods = template_nodes.select { |n| method_node?(n) }
  dest_methods = dest_nodes.select { |n| method_node?(n) }

  return [] if template_methods.empty? || dest_methods.empty?

  greedy_match(template_methods, dest_methods) do |t_node, d_node|
    compute_method_similarity(t_node, d_node)
  end
end