https://github.com/samuelgiles/sorbet-struct-comparable
Comparable T::Struct's for the equality focused typed Ruby developer.
https://github.com/samuelgiles/sorbet-struct-comparable
compare comparison equality equals ruby sorbet struct structs type-safety typed types
Last synced: 4 months ago
JSON representation
Comparable T::Struct's for the equality focused typed Ruby developer.
- Host: GitHub
- URL: https://github.com/samuelgiles/sorbet-struct-comparable
- Owner: samuelgiles
- License: mit
- Created: 2020-06-16T06:26:28.000Z (almost 5 years ago)
- Default Branch: master
- Last Pushed: 2023-04-10T07:11:24.000Z (about 2 years ago)
- Last Synced: 2024-12-30T04:02:26.835Z (4 months ago)
- Topics: compare, comparison, equality, equals, ruby, sorbet, struct, structs, type-safety, typed, types
- Language: Ruby
- Homepage:
- Size: 359 KB
- Stars: 31
- Watchers: 5
- Forks: 5
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README

[](https://badge.fury.io/rb/sorbet-struct-comparable) 
# Making T::Struct's comparable since 2020If you just want some simple equality checking on your `T::Struct`'s then you've come to the right place.
Out of the box Sorbet's super useful `T::Struct`'s () are not comparable meaning two separate instances (i.e. different `#object_id`'s) of a `T::Struct` with identical attributes are not equal.
This behaviour can be confusing at first glance, especially if/when you start using `T::Struct`'s alongside RSpec where expectations such as `it { is_expected.to eq my_expected_struct }` will fail.
It makes sense for this comparable behaviour to not be baked into `T::Struct`'s by default since defining what makes two instances of something equal could be seen by some as business logic, but for the 90% of cases where you just want to run `<=>` over your attributes this mixin is a solution.
## Installation
Add this line to your application's Gemfile:
```ruby
gem 'sorbet-struct-comparable'
```And then execute:
$ bundle install
Or install it yourself as:
$ gem install sorbet-struct-comparable
## Usage
Just include `T::Struct::ActsAsComparable` in your struct, done!
```ruby
require 'sorbet-struct-comparable'class BlogPost < T::Struct
include T::Struct::ActsAsComparableclass Topic < T::Enum
enums do
Cycling = new
Music = new
Walking = new
end
endconst :title, String
const :topic, Topic
endblog_post = BlogPost.new(title: 'My Blog Post', topic: BlogPost::Topic::Cycling)
different_blog_post = BlogPost.new(title: 'Another Blog Post', topic: BlogPost::Topic::Walking)
identical_blog_post = BlogPost.new(title: 'My Blog Post', topic: BlogPost::Topic::Cycling)blog_post == different_blog_post
# => false
blog_post == identical_blog_post
# => true
```## Development
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
## Contributing
Bug reports and pull requests are welcome on GitHub at https://github.com/samuelgiles/sorbet-struct-comparable.