Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/adamluzsi/design_by_contract.rb
Design by contract conventions
https://github.com/adamluzsi/design_by_contract.rb
bdd designbycontract interface ruby signature testing
Last synced: 6 days ago
JSON representation
Design by contract conventions
- Host: GitHub
- URL: https://github.com/adamluzsi/design_by_contract.rb
- Owner: adamluzsi
- License: mit
- Created: 2017-11-21T21:01:57.000Z (almost 7 years ago)
- Default Branch: master
- Last Pushed: 2018-02-06T11:00:23.000Z (over 6 years ago)
- Last Synced: 2024-04-23T17:52:44.901Z (7 months ago)
- Topics: bdd, designbycontract, interface, ruby, signature, testing
- Language: Ruby
- Size: 26.4 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.txt
- Code of conduct: CODE_OF_CONDUCT.md
Awesome Lists containing this project
README
# DesignByContract
This is a collection of techniques to create contract between Objects
## Installation
Add this line to your application's Gemfile:
```ruby
gem 'design_by_contract'
```And then execute:
$ bundle
Or install it yourself as:
$ gem install design_by_contract
## Usage
### Design pattern fulfilling methods
This are created to increase productivity and, make test fail fast in order to eliminate unwanted hiccup
#### Dependency Injection Pattern
interfaces in dependency injection contract placed as last element in the argument description array.
For key or keyreq arguments, it is required to specify the keyword as second parameter for argument description element.```ruby
require 'logger'
class TStoreInterface = DesignByContract::Interface.new(create: [:req, :req], read: [:req])
def initialize(store, logger: Logger.new(STDOUT))
endend
DesignByContract.as_dependency_injection_for T, [
[:req, StoreInterface], # pass as predefined interfaces
[:key, :logger, {info: [:req, :block]}] # or as raw hash based signature
]```
### Under the Hood components
#### Signature
Signatures are the fingerprint of a function.
It can describe how the method should look.
This normally just part of the convention methods under the hood.```ruby
s = DesignByContract::Signature.new [:req, :opt, %i[keyreq keyword] ]
def test(value, value_with_default="def", keyword:)
ends.match?(method(:test)) #=> true
```
#### Interface
The most basic simple use case for interface is to use it for simply validate a class.
Other than that it's also just only the part of the convention methods under the hood.```ruby
i = DesignByContract::Interface.new test: [:req, %i[keyreq keyword] ]
class Good1
def test(value, value_with_default="def", keyword:)
end
endclass Good2
def test(value, keyword:)
end
endclass Good3
def test(value="with_def_still_ok_for_req", keyword:)
end
endclass Bad
def test(value1, value2, keyword:)
end
endi.implemented_by?(Good1) #=> true
i.fulfilled_by?(Good1.new) #=> true
i.match?(Good1.new.method(:test)) #=> truei.implemented_by?(Good2) #=> true
i.fulfilled_by?(Good2.new) #=> true
i.match?(Good2.new.method(:test)) #=> truei.implemented_by?(Good3) #=> true
i.fulfilled_by?(Good3.new) #=> true
i.match?(Good3.new.method(:test)) #=> truei.implemented_by?(Bad) #=> false
i.fulfilled_by?(Bad.new) #=> false
i.match?(Bad.new.method(:test)) #=> false```
## Contributing
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/design_by_contract. 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 DesignByContract project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/design_by_contract/blob/master/CODE_OF_CONDUCT.md).