Skip to content

Commit

Permalink
Update types for incomparable fix
Browse files Browse the repository at this point in the history
  • Loading branch information
samuelgiles committed Apr 21, 2021
1 parent ee39735 commit 90f6026
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Releases

UNRELEASED

* Fixed `T::Struct`'s of different classes returning `-1`; now returning `nil` as is expected of incomparable objects
* Fixed incomparable attributes of different returning `-1` instead of `nil`

1.0.0

* Initial release: Extracted from [https://bellroy.com/](Bellroy) projects.
7 changes: 5 additions & 2 deletions lib/t/struct/acts_as_comparable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,19 @@ module ActsAsComparable
extend T::Sig
include ::Comparable

EQUAL = 0
NOT_COMPARABLE = nil

sig { params(other: Object).returns(Integer) }
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, Integer) if compare_result != EQUAL
return T.cast(compare_result, T.nilable(Integer)) if compare_result != EQUAL
end

return EQUAL
end
end
end
Expand Down

0 comments on commit 90f6026

Please sign in to comment.