https://github.com/amrrbakry/resultt
A simple library to wrap execution result in smart success or error objects.
https://github.com/amrrbakry/resultt
error result resultt ruby success
Last synced: about 2 months ago
JSON representation
A simple library to wrap execution result in smart success or error objects.
- Host: GitHub
- URL: https://github.com/amrrbakry/resultt
- Owner: amrrbakry
- License: mit
- Created: 2019-10-03T14:58:49.000Z (about 6 years ago)
- Default Branch: master
- Last Pushed: 2020-03-02T16:24:19.000Z (over 5 years ago)
- Last Synced: 2025-08-09T21:05:01.451Z (about 2 months ago)
- Topics: error, result, resultt, ruby, success
- Language: Ruby
- Homepage:
- Size: 17.6 KB
- Stars: 3
- Watchers: 0
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
- Code of conduct: CODE_OF_CONDUCT.md
Awesome Lists containing this project
README
# Resultt
A simple library to wrap execution result in smart success or error objects.
```ruby
success_result = Result do
1 + 1
end
# => #success_result.success? # true
success_result.value # 2
success_result.error? # falseerror_result = Result do
raise StandardError, 'error result'
end
# => #>error_result.error? # true
error_result.error # #
```
```ruby
result = Result do
# do something
endcase result
when Resultt::Success then 'it is a success!'
when Resultt::Error then 'oh no! error!'
end
```## Installation
Add this line to your application's Gemfile:
```ruby
gem 'resultt'
```And then execute:
$ bundle
Or install it yourself as:
$ gem install resultt
## Usage
extend `Resultt` in your class and you're good to go!
```ruby
class MyClass
extend Resulttdef foo
Result do
# do something
end
end
end
```
#### `map` and `map_error`
it is possible to map on values and errors in `Resultt` objects using `map` and `map_error` respectively:
```ruby
result = Result { 1 + 1 }
# => #
mapped_result = result.map { |value| value + 1 }
# => #result = Result { raise StandardError, 'error' }
# => #>
mapped_result = result.map_error { |error| error.class.to_s }
# => #
```
#### `Success` and `Error`
it is also possible to wrap values or errors in `Resultt::Success` or `Resultt::Error` objects directly:
```ruby
Success('ok')
# => #
Error('err')
# => #
```### Nil values
`Result` will return `Resultt::NilValueError` if the block passed to it evaluates to `nil`. This is to avoid having success results with `nil` values.
```ruby
Result do
nil
end
# => #>
```
## DevelopmentAfter 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/amrrbakry/resultt. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
## License
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
## Code of Conduct
Everyone interacting in the Resultt project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/amrrbakry/resultt/blob/master/CODE_OF_CONDUCT.md).