Sha256: 8ab523c3d28bfb2941155b6ee6b4236f928bdc360625307156ac49fbf70d4530

Contents?: true

Size: 792 Bytes

Versions: 1

Compression:

Stored size: 792 Bytes

Contents

# typed: strict
# frozen_string_literal: true

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

      LESS_THAN_OTHER = -1
      EQUAL = 0

      sig { params(other: Object).returns(Integer) }
      def <=>(other)
        result = EQUAL
        return LESS_THAN_OTHER if other.class != T.unsafe(self).class

        T.unsafe(self).class.decorator.props.keys.map do |attribute_key|
          compare_result = T.unsafe(self).send(attribute_key) <=> other.send(attribute_key)
          result = if compare_result.nil?
                    LESS_THAN_OTHER
                   else
                     T.cast(compare_result, Integer)
                   end
          break if result != EQUAL
        end
        result
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

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