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

Included in:
Serializable
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



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/lutaml/model/comparable_model.rb', line 105

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



128
129
130
131
132
# File 'lib/lutaml/model/comparable_model.rb', line 128

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