Class: Lutaml::Model::ComparableModel::DiffContext
- Inherits:
-
Object
- Object
- Lutaml::Model::ComparableModel::DiffContext
- Defined in:
- lib/lutaml/model/comparable_model.rb
Overview
DiffContext handles the comparison between two objects
Instance Attribute Summary collapse
-
#highlight_diff ⇒ Object
readonly
Returns the value of attribute highlight_diff.
-
#level ⇒ Object
Returns the value of attribute level.
-
#obj1 ⇒ Object
readonly
Returns the value of attribute obj1.
-
#obj2 ⇒ Object
readonly
Returns the value of attribute obj2.
-
#root_tree ⇒ Object
Returns the value of attribute root_tree.
-
#show_unchanged ⇒ Object
readonly
Returns the value of attribute show_unchanged.
-
#tree_lines ⇒ Object
Returns the value of attribute tree_lines.
-
#use_colors ⇒ Object
readonly
Returns the value of attribute use_colors.
Instance Method Summary collapse
-
#calculate_diff_score ⇒ Float
Calculates the normalized diff score.
-
#diff_tree(indent = "") ⇒ String
Generates a diff tree between the two objects.
-
#initialize(obj1, obj2, **options) ⇒ DiffContext
constructor
Initializes a new DiffContext.
Constructor Details
#initialize(obj1, obj2, **options) ⇒ DiffContext
Initializes a new DiffContext
249 250 251 252 253 254 255 256 257 258 |
# File 'lib/lutaml/model/comparable_model.rb', line 249 def initialize(obj1, obj2, **) @obj1 = obj1 @obj2 = obj2 @show_unchanged = .fetch(:show_unchanged, false) @highlight_diff = .fetch(:highlight_diff, false) @use_colors = .fetch(:use_colors, true) @level = 0 @tree_lines = [] @root_tree = Tree.new(obj1.class.to_s) end |
Instance Attribute Details
#highlight_diff ⇒ Object (readonly)
Returns the value of attribute highlight_diff.
242 243 244 |
# File 'lib/lutaml/model/comparable_model.rb', line 242 def highlight_diff @highlight_diff end |
#level ⇒ Object
Returns the value of attribute level.
243 244 245 |
# File 'lib/lutaml/model/comparable_model.rb', line 243 def level @level end |
#obj1 ⇒ Object (readonly)
Returns the value of attribute obj1.
242 243 244 |
# File 'lib/lutaml/model/comparable_model.rb', line 242 def obj1 @obj1 end |
#obj2 ⇒ Object (readonly)
Returns the value of attribute obj2.
242 243 244 |
# File 'lib/lutaml/model/comparable_model.rb', line 242 def obj2 @obj2 end |
#root_tree ⇒ Object
Returns the value of attribute root_tree.
243 244 245 |
# File 'lib/lutaml/model/comparable_model.rb', line 243 def root_tree @root_tree end |
#show_unchanged ⇒ Object (readonly)
Returns the value of attribute show_unchanged.
242 243 244 |
# File 'lib/lutaml/model/comparable_model.rb', line 242 def show_unchanged @show_unchanged end |
#tree_lines ⇒ Object
Returns the value of attribute tree_lines.
243 244 245 |
# File 'lib/lutaml/model/comparable_model.rb', line 243 def tree_lines @tree_lines end |
#use_colors ⇒ Object (readonly)
Returns the value of attribute use_colors.
242 243 244 |
# File 'lib/lutaml/model/comparable_model.rb', line 242 def use_colors @use_colors end |
Instance Method Details
#calculate_diff_score ⇒ Float
Calculates the normalized diff score
271 272 273 274 275 276 277 278 279 |
# File 'lib/lutaml/model/comparable_model.rb', line 271 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
262 263 264 265 266 267 |
# File 'lib/lutaml/model/comparable_model.rb', line 262 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 |