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



157
158
159
160
161
162
163
164
165
166
# File 'lib/lutaml/model/comparable_model.rb', line 157

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.



150
151
152
# File 'lib/lutaml/model/comparable_model.rb', line 150

def highlight_diff
  @highlight_diff
end

#levelObject

Returns the value of attribute level.



151
152
153
# File 'lib/lutaml/model/comparable_model.rb', line 151

def level
  @level
end

#obj1Object (readonly)

Returns the value of attribute obj1.



150
151
152
# File 'lib/lutaml/model/comparable_model.rb', line 150

def obj1
  @obj1
end

#obj2Object (readonly)

Returns the value of attribute obj2.



150
151
152
# File 'lib/lutaml/model/comparable_model.rb', line 150

def obj2
  @obj2
end

#root_treeObject

Returns the value of attribute root_tree.



151
152
153
# File 'lib/lutaml/model/comparable_model.rb', line 151

def root_tree
  @root_tree
end

#show_unchangedObject (readonly)

Returns the value of attribute show_unchanged.



150
151
152
# File 'lib/lutaml/model/comparable_model.rb', line 150

def show_unchanged
  @show_unchanged
end

#tree_linesObject

Returns the value of attribute tree_lines.



151
152
153
# File 'lib/lutaml/model/comparable_model.rb', line 151

def tree_lines
  @tree_lines
end

#use_colorsObject (readonly)

Returns the value of attribute use_colors.



150
151
152
# File 'lib/lutaml/model/comparable_model.rb', line 150

def use_colors
  @use_colors
end

Instance Method Details

#calculate_diff_scoreFloat

Calculates the normalized diff score



179
180
181
182
183
184
185
186
187
# File 'lib/lutaml/model/comparable_model.rb', line 179

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



170
171
172
173
174
175
# File 'lib/lutaml/model/comparable_model.rb', line 170

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