https://github.com/juanitofatas/safe_operation
Chainable operations
https://github.com/juanitofatas/safe_operation
operations ruby safe safety
Last synced: 3 months ago
JSON representation
Chainable operations
- Host: GitHub
- URL: https://github.com/juanitofatas/safe_operation
- Owner: JuanitoFatas
- License: mit
- Created: 2017-07-09T09:17:41.000Z (almost 8 years ago)
- Default Branch: master
- Last Pushed: 2019-04-06T00:54:35.000Z (about 6 years ago)
- Last Synced: 2025-04-01T20:19:43.152Z (3 months ago)
- Topics: operations, ruby, safe, safety
- Language: Ruby
- Homepage: https://github.com/JuanitoFatas/safe_operation
- Size: 10.7 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE.txt
- Code of conduct: CODE_OF_CONDUCT.md
Awesome Lists containing this project
README
# SafeOperation
[](https://rubygems.org/gems/safe_operation)
[](https://travis-ci.org/JuanitoFatas/safe_operation)Write safer code with SafeOperation.
## Installation
Add this line to your application's Gemfile:
```ruby
gem "safe_operation"
```And then execute:
$ bundle
Or install it yourself as:
$ gem install safe_operation
## Usage
No monkey patch.
```ruby
class RecordNotFound < StandardError; end
class User def self.find(id); new; end; end;
class Guest; end
class Admin; def initialize(user); end; end
class SuperAdmin; def initialize(admin); end; endoperation = SafeOperation.run do
User.find(1)
end# Know if operation succeeded
operation.success?# Get the value of the performed operation
operation.value# Common patterns, reloaded
SafeOperation.run { User.find(1) }.value_or(Guest.new)# Handling exceptions
SafeOperation.run { raise(RecordNotFound) }.value_or_else do |exception|
if exception.is_a?(RecordNotFound)
Guest.new
else
# logging, re-raise, etc.
end
end# Apply on first operation
operation = SafeOperation.
run { User.find(1) }.
and_then { |user| Admin.new(user) }operation.value # #
# Chainable
operation = SafeOperation.
run { User.find(1) }.
and_then { |user| Admin.new(user) }.
and_then { |admin| SuperAdmin.new(admin) }operation.value # #
# Add or_else to handle failed operation
operation = SafeOperation.
run { raise(RecordNotFound) }.
and_then { |user| Admin.new(user) }.
or_else { Guest.new }operation.success? # => false
operation.value # => #
```See test suite for more examples.
## Contributing
This project follows the [Moya Contributors Guidelines][moya].
TLDR: means we give out commit access easily and often.[moya]: https://github.com/Moya/contributors
Bug reports and pull requests are welcome on GitHub at https://github.com/JuanitoFatas/safe_operation.
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](http://opensource.org/licenses/MIT).## Code of Conduct
Everyone interacting in the SafeOperation project’s codebases, issue trackers is expected to follow
the [code of conduct](https://github.com/JuanitoFatas/safe_operation/blob/master/CODE_OF_CONDUCT.md).