Class: Json::Merge::ObjectMatchRefiner

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

Overview

Match refiner for JSON objects and array elements that didn’t match by exact signature.

This refiner uses fuzzy matching to pair JSON nodes that have:

  • Similar key names in objects (e.g., databaseUrl vs database_url)
  • Array elements with similar structure or content
  • Objects with overlapping keys but different values

The matching algorithm considers:

  • Key name similarity for object pairs (Levenshtein distance)
  • Value type and content similarity
  • Structural overlap for nested objects

Examples:

Basic usage

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

With custom weights

refiner = ObjectMatchRefiner.new(
  threshold: 0.5,
  key_weight: 0.6,
  value_weight: 0.4
)

See Also:

  • Ast::Merge::MatchRefinerBase

Constant Summary collapse

DEFAULT_KEY_WEIGHT =

Default weight for key similarity

0.7
DEFAULT_VALUE_WEIGHT =

Default weight for value similarity

0.3

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(threshold: DEFAULT_THRESHOLD, key_weight: DEFAULT_KEY_WEIGHT, value_weight: DEFAULT_VALUE_WEIGHT, **options) ⇒ ObjectMatchRefiner

Initialize an object match refiner.

Parameters:

  • threshold (Float) (defaults to: DEFAULT_THRESHOLD)

    Minimum score to accept a match (default: 0.5)

  • key_weight (Float) (defaults to: DEFAULT_KEY_WEIGHT)

    Weight for key similarity (default: 0.7)

  • value_weight (Float) (defaults to: DEFAULT_VALUE_WEIGHT)

    Weight for value similarity (default: 0.3)



47
48
49
50
51
# File 'lib/json/merge/object_match_refiner.rb', line 47

def initialize(threshold: DEFAULT_THRESHOLD, key_weight: DEFAULT_KEY_WEIGHT, value_weight: DEFAULT_VALUE_WEIGHT, **options)
  super(threshold: threshold, **options)
  @key_weight = key_weight
  @value_weight = value_weight
end

Instance Attribute Details

#key_weightFloat (readonly)

Returns Weight for key similarity (0.0-1.0).

Returns:

  • (Float)

    Weight for key similarity (0.0-1.0)



37
38
39
# File 'lib/json/merge/object_match_refiner.rb', line 37

def key_weight
  @key_weight
end

#value_weightFloat (readonly)

Returns Weight for value similarity (0.0-1.0).

Returns:

  • (Float)

    Weight for value similarity (0.0-1.0)



40
41
42
# File 'lib/json/merge/object_match_refiner.rb', line 40

def value_weight
  @value_weight
end

Instance Method Details

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

Find matches between unmatched JSON nodes.

Handles both object key-value pairs and array elements.

Parameters:

  • template_nodes (Array)

    Unmatched nodes from template

  • dest_nodes (Array)

    Unmatched nodes from destination

  • context (Hash) (defaults to: {})

    Additional context

Returns:

  • (Array<MatchResult>)

    Array of node matches



61
62
63
64
65
66
67
68
69
# File 'lib/json/merge/object_match_refiner.rb', line 61

def call(template_nodes, dest_nodes, context = {})
  # Match object pairs (key-value entries)
  pair_matches = match_pairs(template_nodes, dest_nodes)

  # Match array elements (objects within arrays)
  array_matches = match_array_objects(template_nodes, dest_nodes)

  pair_matches + array_matches
end