{"id":16068586,"url":"https://github.com/markbates/breach","last_synced_at":"2025-07-04T23:08:36.334Z","repository":{"id":66380849,"uuid":"59237447","full_name":"markbates/breach","owner":"markbates","description":null,"archived":false,"fork":false,"pushed_at":"2016-05-19T22:52:07.000Z","size":28,"stargazers_count":3,"open_issues_count":0,"forks_count":0,"subscribers_count":5,"default_branch":"master","last_synced_at":"2025-02-07T18:51:34.033Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"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/markbates.png","metadata":{"files":{"readme":"README.md","changelog":null,"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,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2016-05-19T19:43:32.000Z","updated_at":"2023-06-04T07:04:46.000Z","dependencies_parsed_at":null,"dependency_job_id":"472455cd-2b1d-4ff0-bc6e-5ffc195752e9","html_url":"https://github.com/markbates/breach","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/markbates/breach","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/markbates%2Fbreach","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/markbates%2Fbreach/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/markbates%2Fbreach/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/markbates%2Fbreach/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/markbates","download_url":"https://codeload.github.com/markbates/breach/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/markbates%2Fbreach/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":263632058,"owners_count":23491530,"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":[],"created_at":"2024-10-09T06:21:50.269Z","updated_at":"2025-07-04T23:08:36.329Z","avatar_url":"https://github.com/markbates.png","language":"Ruby","funding_links":[],"categories":[],"sub_categories":[],"readme":"# breach\n\nbreach provides completely-optional method type checking, including support for interfaces, in Ruby, using concise and expressive syntax, allowing you to bring design-by-contract principles to Ruby.\n\n## breach is currently under early development and as such, everything in this doc is aspirational, until it isn't.\n\n## Usage\n\nYou can use breach for simple input type-checking:\n\n```ruby\ndef print_a_string_three_times(str)\n  puts str * 3\nend\ntype_check :print_a_string_three_times, inputs: [String]\n\nprint_a_string_three_times(DateTime.new) # raises Breach::InputTypeCheckFailed\n```\n\nSo, breach automatically will wrap the `print_a_string_three_times` method and, prior to executing it, check the input to make sure it's a `String`. If not, you'll get a `Breach::InputTypeCheckFailed` exception raised right then and there, instead of running into a much more opaque and potentially misleading error later on.\n\nbreach really shines when you want to implement interfaces/abstract classes:\n\n```ruby\nmodule Writer\n  extend Breach::Interface\n\n  defines :write, inputs: [String], returns: [Numeric]\nend\n\n\nclass MyWriter\n  implements Writer\n\n  def write(s)\n    return 1\n  end\nend\n\nmw = MyWriter.new\nmw.write(\"Bingo bongo\") # works\nmw.write(3) # raises Breach::InputTypeCheckFailed\n```\n\nWhen used with interfaces, breach will also catch return type errors before they propagate out into the rest of your application, providing you a much more useful exception:\n\n```ruby\nmodule Writer\n  extend Breach::Interface\n\n  defines :write, inputs: [String], returns: [Numeric]\nend\n\n\nclass MyWriter\n  implements Writer\n\n  def write(s)\n    return \"Flippy floppy\" # note: not a Numeric\n  end\nend\n\nmw = MyWriter.new\nmw.write(\"Any string\") # raises Breach::ReturnTypeCheckFailed\n```\n\nIf you need to support multiple potential classes of input (or return types), use an array:\n\n```ruby\nmodule Writer\n  extend Breach::Interface\n\n  defines :write, inputs: [String, [Hash, nil]], returns: [Numeric]\nend\n\nclass MyWriter\n  implements Writer\n\n  def write(s)\n    return \"Flappy foo\"\n  end\nend\nmw = MyWriter.new\nmw.write(\"Hi there\", {foo: \"bar\"}) # works\nmw.write(\"Interfaces are cool!\", nil) # works\nmw.write(\"A bridge too far!\", DateTime.new) # raises Breach::InputTypeCheckFailed\n```\n\n* todo: optional args\n* todo: splat args\n\n\nNot only will your run-time errors be more useful, it'll also catch interface implementation problems *at require time*:\n\n```ruby\nclass NotActuallyAWriter\n  implements Writer\n  # no write method\nend # raises a Breach::NotImplementedError!\n```\n\nSince breach's interface definitions are just regular mix-ins, you can compose them together:\n\n```ruby\nmodule Writer\n  extend Breach::Interface\n\n  defines :write, inputs: [String, [Hash, nil], :optional], returns: [Numberable]\nend\n\nmodule Reader\n  extend Breach::Interface\n\n  defines :read, inputs: [File], returns: [String]\nend\n\nmodule ReadWriter\n  extend Breach::Interface\n  include Writer\n  include Reader\nend\n```\n\n## Installation\n\nAdd this line to your application's Gemfile:\n\n```ruby\ngem 'breach'\n```\n\nAnd then execute:\n\n    $ bundle\n\nOr install it yourself as:\n\n    $ gem install breach\n\n## Development\n\nAfter checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.\n\nTo 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).\n\n## Contributing\n\nBug reports and pull requests are welcome [on GitHub](http://github.com/markbates/breach). 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.\n\n## License\n\nThe gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmarkbates%2Fbreach","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmarkbates%2Fbreach","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmarkbates%2Fbreach/lists"}