Module: Lutaml::Model::ComparableModel

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

Overview

ComparableModel module provides functionality to compare and diff two objects of the same class, based on their attribute values.

Defined Under Namespace

Modules: ClassMethods Classes: DiffContext, Tree

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



8
9
10
# File 'lib/lutaml/model/comparable_model.rb', line 8

def self.included(base)
  base.extend(ClassMethods)
end

Instance Method Details

#eql?(other) ⇒ Boolean Also known as: ==

Checks if two objects are equal based on their attributes

Parameters:

  • other (Object)

    The object to compare with

Returns:

  • (Boolean)

    True if objects are equal, false otherwise



15
16
17
18
19
20
# File 'lib/lutaml/model/comparable_model.rb', line 15

def eql?(other)
  other.class == self.class &&
    self.class.attributes.all? do |attr, _|
      send(attr) == other.send(attr)
    end
end

#hashInteger

Generates a hash value for the object

Returns:

  • (Integer)

    The hash value



26
27
28
29
30
# File 'lib/lutaml/model/comparable_model.rb', line 26

def hash
  ([self.class] + self.class.attributes.map do |attr, _|
                    send(attr).hash
                  end).hash
end