Class: Lutaml::Model::ComparableModel::DiffContext

Inherits:
Object
  • Object
show all
Defined in:
lib/lutaml/model/comparable_model.rb

Overview

DiffContext handles the comparison between two objects

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(obj1, obj2, **options) ⇒ DiffContext

Initializes a new DiffContext

Parameters:

  • obj1 (Object)

    The first object to compare

  • obj2 (Object)

    The second object to compare

  • options (Hash)

    Options for diff generation



117
118
119
120
121
122
123
124
125
126
# File 'lib/lutaml/model/comparable_model.rb', line 117

def initialize(obj1, obj2, **options)
  @obj1 = obj1
  @obj2 = obj2
  @show_unchanged = options.fetch(:show_unchanged, false)
  @highlight_diff = options.fetch(:highlight_diff, false)
  @use_colors = options.fetch(:use_colors, true)
  @level = 0
  @tree_lines = []
  @root_tree = Tree.new(obj1.class.to_s)
end

Instance Attribute Details

#highlight_diffObject (readonly)

Returns the value of attribute highlight_diff.



110
111
112
# File 'lib/lutaml/model/comparable_model.rb', line 110

def highlight_diff
  @highlight_diff
end

#levelObject

Returns the value of attribute level.



111
112
113
# File 'lib/lutaml/model/comparable_model.rb', line 111

def level
  @level
end

#obj1Object (readonly)

Returns the value of attribute obj1.



110
111
112
# File 'lib/lutaml/model/comparable_model.rb', line 110

def obj1
  @obj1
end

#obj2Object (readonly)

Returns the value of attribute obj2.



110
111
112
# File 'lib/lutaml/model/comparable_model.rb', line 110

def obj2
  @obj2
end

#root_treeObject

Returns the value of attribute root_tree.



111
112
113
# File 'lib/lutaml/model/comparable_model.rb', line 111

def root_tree
  @root_tree
end

#show_unchangedObject (readonly)

Returns the value of attribute show_unchanged.



110
111
112
# File 'lib/lutaml/model/comparable_model.rb', line 110

def show_unchanged
  @show_unchanged
end

#tree_linesObject

Returns the value of attribute tree_lines.



111
112
113
# File 'lib/lutaml/model/comparable_model.rb', line 111

def tree_lines
  @tree_lines
end

#use_colorsObject (readonly)

Returns the value of attribute use_colors.



110
111
112
# File 'lib/lutaml/model/comparable_model.rb', line 110

def use_colors
  @use_colors
end

Instance Method Details

#calculate_diff_scoreFloat

Calculates the normalized diff score

Returns:

  • (Float)

    The normalized diff score



139
140
141
142
143
144
145
146
147
# File 'lib/lutaml/model/comparable_model.rb', line 139

def calculate_diff_score
  total_score = 0
  total_attributes = 0
  traverse_diff do |_, _, value1, value2, _|
    total_score += calculate_attribute_score(value1, value2)
    total_attributes += 1
  end
  total_attributes.positive? ? total_score / total_attributes : 0
end

#diff_tree(indent = "") ⇒ String

Generates a diff tree between the two objects

Returns:

  • (String)

    A string representation of the diff tree



130
131
132
133
134
135
# File 'lib/lutaml/model/comparable_model.rb', line 130

def diff_tree(indent = "")
  traverse_diff do |name, type, value1, value2, is_last|
    format_attribute_diff(name, type, value1, value2, is_last)
  end
  @root_tree.to_s(indent)
end