https://github.com/bibendi/simple_contracts
Plain Old Ruby Object Implementation of Contract.
https://github.com/bibendi/simple_contracts
contracts docker ruby
Last synced: about 2 months ago
JSON representation
Plain Old Ruby Object Implementation of Contract.
- Host: GitHub
- URL: https://github.com/bibendi/simple_contracts
- Owner: bibendi
- License: mit
- Created: 2018-07-08T07:40:52.000Z (almost 8 years ago)
- Default Branch: master
- Last Pushed: 2018-10-10T11:54:13.000Z (over 7 years ago)
- Last Synced: 2025-11-16T20:10:47.121Z (8 months ago)
- Topics: contracts, docker, ruby
- Language: Ruby
- Homepage:
- Size: 33.2 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.txt
Awesome Lists containing this project
README
[](https://travis-ci.org/bibendi/simple_contracts)
[](https://codeclimate.com/github/bibendi/simple_contracts/maintainability)
[](https://cultofmartians.com/tasks/simple-contracts.html)
# SimpleContracts
## Plain Old Ruby Object Implementation of Contract
This project contains the most simple implementation of Contract written in Ruby (and maybe later in other languages).
The Contract is inspired by Design by Contracts approach and pushes Fail Fast techinque further.
So, Contract is a class with the only public method , that validates some action/behavior agains Contract Rules:
- Guarantees - the rules that SHOULD be valid for each check of behavior
- Expectations - list of all expected states that COULD be valid for the behavior check
Contract validates, that:
- ALL Guarantees were met
- AT LEAST ONE Expectations was met
Otherwise, Contract raises an exception with details, at least on what step behavior was broken.
## Installation
Add this line to your application's Gemfile:
```ruby
gem 'simple_contracts'
```
And then execute:
$ bundle
Or install it yourself as:
$ gem install simple_contracts
## Usage
```ruby
class TwitterContract < SimpleContracts::Base
def initialize(post)
super
@post = post
end
private
def guarantee_verified_delete
return true if Twitter::REST::Client.statuses(@post.tweet_id).empty?
false
end
def expect_some_action1
...
end
def expect_some_action2
...
end
# ... other rules
end
@post = Post.find(params.require(:post_id))
# Use synchronously, (raises exception, "Fails Fast"™):
TwitterContract.(@post, async: false) { TwitterAPI.destroy(@post) }
# Use asynchronously (does not affect TwitterAPI.destroy,
# but tracks any problems with TwitterContract validation)
TwitterContract.(@post) { TwitterAPI.destroy(@post) }
```
## Contributing
Bug reports and pull requests are welcome on GitHub at https://github.com/bibendi/simple_contracts.
## License
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).