Module: T::Struct::ActsAsComparable

Extended by:
T::Sig
Includes:
Comparable
Defined in:
lib/t/struct/acts_as_comparable.rb

Constant Summary collapse

EQUAL =
0
NOT_COMPARABLE =
nil

Instance Method Summary collapse

Instance Method Details

#<=>(other) ⇒ Object



14
15
16
17
18
19
20
21
22
23
# File 'lib/t/struct/acts_as_comparable.rb', line 14

def <=>(other)
  return NOT_COMPARABLE if other.class != T.unsafe(self).class

  T.unsafe(self).class.decorator.props.keys.each do |attribute_key|
    compare_result = T.unsafe(self).send(attribute_key) <=> other.send(attribute_key)
    return T.cast(compare_result, T.nilable(Integer)) if compare_result != EQUAL
  end

  return EQUAL
end

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


26
27
28
# File 'lib/t/struct/acts_as_comparable.rb', line 26

def eql?(other)
  self == other
end

#hashObject



31
32
33
# File 'lib/t/struct/acts_as_comparable.rb', line 31

def hash
  T.unsafe(self).class.decorator.props.keys.map { |attribute_key| T.unsafe(self).send(attribute_key).hash }.hash
end