Class: Canon::Comparison::YamlComparator

Inherits:
Object
  • Object
show all
Defined in:
lib/canon/comparison/yaml_comparator.rb

Overview

YAML comparison class Handles comparison of YAML objects with various options

Constant Summary collapse

DEFAULT_OPTS =

Default comparison options for YAML

{
  # Output options
  verbose: false,

  # Match system options
  match_profile: nil,
  match: nil,
  preprocessing: nil,
  global_profile: nil,
  global_options: nil,

  # Diff display options
  diff: nil,
}.freeze

Class Method Summary collapse

Class Method Details

.equivalent?(yaml1, yaml2, opts = {}) ⇒ Boolean, ComparisonResult

Compare two YAML objects for equivalence

Parameters:

  • yaml1 (String, Hash, Array)

    First YAML

  • yaml2 (String, Hash, Array)

    Second YAML

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

    Comparison options

Returns:

  • (Boolean, ComparisonResult)

    true if equivalent, or ComparisonResult if verbose



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/canon/comparison/yaml_comparator.rb', line 36

def equivalent?(yaml1, yaml2, opts = {})
  opts = DEFAULT_OPTS.merge(opts)

  # Resolve match options with format-specific defaults
  match_opts_hash = MatchOptions::Yaml.resolve(
    format: :yaml,
    match_profile: opts[:match_profile],
    match: opts[:match],
    preprocessing: opts[:preprocessing],
    global_profile: opts[:global_profile],
    global_options: opts[:global_options],
  )

  # Wrap in ResolvedMatchOptions for consistency with XML/HTML/JSON
  Canon::Comparison::ResolvedMatchOptions.new(
    match_opts_hash,
    format: :yaml,
  )

  # Store resolved match options for use in comparison logic
  opts[:match_opts] = match_opts_hash

  # Parse YAML if strings
  obj1 = parse_yaml(yaml1)
  obj2 = parse_yaml(yaml2)

  differences = []
  result = RubyObjectComparator.compare_objects(obj1, obj2, opts,
                                                differences, "")

  if opts[:verbose]
    # Format YAML for display
    yaml_str1 = obj1.is_a?(String) ? obj1 : YAML.dump(obj1)
    yaml_str2 = obj2.is_a?(String) ? obj2 : YAML.dump(obj2)

    ComparisonResult.new(
      differences: differences,
      preprocessed_strings: [yaml_str1, yaml_str2],
      format: :yaml,
      match_options: match_opts_hash,
    )
  else
    result == Comparison::EQUIVALENT
  end
end