{"id":16072998,"url":"https://github.com/juanitofatas/safe_operation","last_synced_at":"2025-07-04T11:40:10.816Z","repository":{"id":56894137,"uuid":"96673549","full_name":"JuanitoFatas/safe_operation","owner":"JuanitoFatas","description":"Chainable operations","archived":false,"fork":false,"pushed_at":"2019-04-06T00:54:35.000Z","size":11,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-06-28T15:03:26.211Z","etag":null,"topics":["operations","ruby","safe","safety"],"latest_commit_sha":null,"homepage":"https://github.com/JuanitoFatas/safe_operation","language":"Ruby","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/JuanitoFatas.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-07-09T09:17:41.000Z","updated_at":"2019-04-06T00:55:15.000Z","dependencies_parsed_at":"2022-08-21T01:20:21.217Z","dependency_job_id":null,"html_url":"https://github.com/JuanitoFatas/safe_operation","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"purl":"pkg:github/JuanitoFatas/safe_operation","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/JuanitoFatas%2Fsafe_operation","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/JuanitoFatas%2Fsafe_operation/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/JuanitoFatas%2Fsafe_operation/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/JuanitoFatas%2Fsafe_operation/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/JuanitoFatas","download_url":"https://codeload.github.com/JuanitoFatas/safe_operation/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/JuanitoFatas%2Fsafe_operation/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":263503772,"owners_count":23476815,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["operations","ruby","safe","safety"],"created_at":"2024-10-09T08:01:58.720Z","updated_at":"2025-07-04T11:40:10.789Z","avatar_url":"https://github.com/JuanitoFatas.png","language":"Ruby","funding_links":[],"categories":[],"sub_categories":[],"readme":"# SafeOperation\n\n[![Gem Version](https://badge.fury.io/rb/safe_operation.svg)](https://rubygems.org/gems/safe_operation)\n[![Build Status](https://travis-ci.org/JuanitoFatas/safe_operation.svg?branch=master)](https://travis-ci.org/JuanitoFatas/safe_operation)\n\nWrite safer code with SafeOperation.\n\n## Installation\n\nAdd this line to your application's Gemfile:\n\n```ruby\ngem \"safe_operation\"\n```\n\nAnd then execute:\n\n    $ bundle\n\nOr install it yourself as:\n\n    $ gem install safe_operation\n\n## Usage\n\nNo monkey patch.\n\n```ruby\nclass RecordNotFound \u003c StandardError; end\nclass User def self.find(id); new; end; end;\nclass Guest; end\nclass Admin; def initialize(user); end; end\nclass SuperAdmin; def initialize(admin); end; end\n\noperation = SafeOperation.run do\n  User.find(1)\nend\n\n# Know if operation succeeded\noperation.success?\n\n# Get the value of the performed operation\noperation.value\n\n# Common patterns, reloaded\nSafeOperation.run { User.find(1) }.value_or(Guest.new)\n\n# Handling exceptions\nSafeOperation.run { raise(RecordNotFound) }.value_or_else do |exception|\n  if exception.is_a?(RecordNotFound)\n    Guest.new\n  else\n    # logging, re-raise, etc.\n  end\nend\n\n# Apply on first operation\noperation = SafeOperation.\n  run { User.find(1) }.\n  and_then { |user| Admin.new(user) }\n\noperation.value # #\u003cAdmin ...\u003e\n\n# Chainable\noperation = SafeOperation.\n  run { User.find(1) }.\n  and_then { |user| Admin.new(user) }.\n  and_then { |admin| SuperAdmin.new(admin) }\n\noperation.value # #\u003cSuperAdmin ...\u003e\n\n# Add or_else to handle failed operation\noperation = SafeOperation.\n  run { raise(RecordNotFound) }.\n  and_then { |user| Admin.new(user) }.\n  or_else { Guest.new }\n\noperation.success? # =\u003e false\noperation.value # =\u003e #\u003cGuest ...\u003e\n```\n\nSee test suite for more examples.\n\n## Contributing\n\nThis project follows the [Moya Contributors Guidelines][moya].\nTLDR: means we give out commit access easily and often.\n\n[moya]: https://github.com/Moya/contributors\n\nBug reports and pull requests are welcome on GitHub at https://github.com/JuanitoFatas/safe_operation.\nThis project is intended to be a safe, welcoming space for collaboration, and contributors are\nexpected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.\n\n## License\n\nThe gem is available as open source under the terms of the\n[MIT License](http://opensource.org/licenses/MIT).\n\n## Code of Conduct\n\nEveryone interacting in the SafeOperation project’s codebases, issue trackers is expected to follow\nthe [code of conduct](https://github.com/JuanitoFatas/safe_operation/blob/master/CODE_OF_CONDUCT.md).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjuanitofatas%2Fsafe_operation","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjuanitofatas%2Fsafe_operation","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjuanitofatas%2Fsafe_operation/lists"}