Sha256: 2c9371d3be172087a1faf72f1a9361f64d726dff1d71f43c7780b1034d8e0140

Contents?: true

Size: 948 Bytes

Versions: 1

Compression:

Stored size: 948 Bytes

Contents

# typed: strict
# frozen_string_literal: true

module T
  class Struct
    module ActsAsComparable
      extend T::Sig
      include ::Comparable

      EQUAL = 0
      NOT_COMPARABLE = nil

      sig { params(other: Object).returns(T.nilable(Integer)) }
      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

      sig { params(other: Object).returns(T::Boolean) }
      def eql?(other)
        self == other
      end

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

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
sorbet-struct-comparable-1.3.0 lib/t/struct/acts_as_comparable.rb