Module: Lutaml::Model::ComparableModel::ClassMethods

Defined in:
lib/lutaml/model/comparable_model.rb

Overview

Class methods added to the class that includes ComparableModel

Instance Method Summary collapse

Instance Method Details

#diff_treeString

Generates a diff tree between two objects of the same class

Parameters:

  • obj1 (Object)

    The first object to compare

  • obj2 (Object)

    The second object to compare

  • options (Hash)

    Options for diff generation

Returns:

  • (String)

    A string representation of the diff tree



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/lutaml/model/comparable_model.rb', line 79

def diff_tree
  if @obj1.nil? && @obj2.nil?
    @root_tree = Tree.new("Both objects are nil")
  elsif @obj1.nil?
    @root_tree = Tree.new("First object is nil")
    format_single_value(@obj2, @root_tree, @obj2.class.to_s)
  elsif @obj2.nil?
    @root_tree = Tree.new("Second object is nil")
    format_single_value(@obj1, @root_tree, @obj1.class.to_s)
  else
    traverse_diff do |name, type, value1, value2, is_last|
      format_attribute_diff(name, type, value1, value2, is_last)
    end
  end

  @root_tree.to_s
end

#diff_with_score(obj1, obj2, **options) ⇒ Array<Float, String>

Generates a diff tree and calculates a diff score between two objects of the same class

Parameters:

  • obj1 (Object)

    The first object to compare

  • obj2 (Object)

    The second object to compare

  • options (Hash)

    Options for diff generation

Returns:

  • (Array<Float, String>)

    An array containing the normalized diff score and the diff tree



102
103
104
105
106
# File 'lib/lutaml/model/comparable_model.rb', line 102

def diff_with_score(obj1, obj2, **options)
  context = DiffContext.new(obj1, obj2, **options)
  indent = options[:indent] || ""
  [context.calculate_diff_score, context.diff_tree(indent)]
end