{"id":18030585,"url":"https://github.com/edisonywh/behaves","last_synced_at":"2025-04-13T08:18:09.562Z","repository":{"id":48822360,"uuid":"190758508","full_name":"edisonywh/behaves","owner":"edisonywh","description":"Define behaviors and contracts between your code.","archived":false,"fork":false,"pushed_at":"2021-07-09T21:14:22.000Z","size":49,"stargazers_count":91,"open_issues_count":9,"forks_count":7,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-04-13T08:18:07.134Z","etag":null,"topics":["gem","ruby"],"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/edisonywh.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}},"created_at":"2019-06-07T14:36:05.000Z","updated_at":"2020-08-18T19:08:34.000Z","dependencies_parsed_at":"2022-09-23T22:12:53.177Z","dependency_job_id":null,"html_url":"https://github.com/edisonywh/behaves","commit_stats":null,"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/edisonywh%2Fbehaves","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/edisonywh%2Fbehaves/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/edisonywh%2Fbehaves/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/edisonywh%2Fbehaves/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/edisonywh","download_url":"https://codeload.github.com/edisonywh/behaves/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248681519,"owners_count":21144703,"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":["gem","ruby"],"created_at":"2024-10-30T09:14:39.271Z","updated_at":"2025-04-13T08:18:09.535Z","avatar_url":"https://github.com/edisonywh.png","language":"Ruby","funding_links":[],"categories":[],"sub_categories":[],"readme":"![CircleCI Badge](https://img.shields.io/circleci/build/github/edisonywh/behaves.svg)\n![RubyGems Badge](https://img.shields.io/gem/v/behaves.svg)\n![Code Climate maintainability](https://img.shields.io/codeclimate/maintainability/edisonywh/behaves.svg)\n\n# Behaves\n\nBehaves is a gem that helps you define behaviors between classes. **Say goodbye to runtime error when defining behaviors.**\n\nBehaves is especially useful for dealing with adapter patterns by making sure that all of your adapters define the required behaviors. See [usage below](https://github.com/edisonywh/behaves#usage) for more examples.\n\n*Detailed explanations in the sections below.*\n\n## Installation\n\nAdd this line to your application's Gemfile:\n\n```ruby\ngem 'behaves'\n```\n\n## Usage\n\nThis is how you define behaviors with `behaves`.\n\nFirst, define required methods on the `Behavior Object` with the `implements` method, which take a list of methods.\n```ruby\nclass Animal\n  extend Behaves\n\n  implements :speak, :eat\nend\n```\n\nThen, you can turn any object (the `Behaving Object`) to behave like the `Behavior Object` by using the `behaves_like` method, which takes a `Behavior Object`.\n```ruby\nclass Dog\n  extend Behaves\n\n  behaves_like Animal\nend\n```\n\nVoilà, that's all it takes to define behaviors! Now if `Dog` does not implement `speak` and `eat`, your code will then throw error **on file load**, instead of **at runtime**.\n\n```diff\n- NotImplementedError: Expected `Dog` to behave like `Animal`, but `speak, eat` are not implemented.\n```\n\nThis is in stark contrast to defining behaviors with inheritance. Let's take a look.\n\n### Inheritance-based behaviors\n\n```ruby\n# Inheritance - potential runtime error.\nclass Animal\n  def speak\n    raise NotImplementedError, \"Animals need to be able to speak!\"\n  end\n\n  def eat\n    raise NotImplementedError, \"Animals need to be able to eat!\"\n  end\nend\n\nclass Dog \u003c Animal\n  def speak\n    \"woof\"\n  end\nend\n```\n\n1) It is unclear that `Dog` has a certain set of behaviors to adhere to.\n\n2) Notice how `Dog` does not implement `#eat`? Inheritance-based behaviors have no guarantee that `Dog` adheres to a certain set of behaviors, which means you can run into runtime errors like this.\n\n```ruby\ncorgi = Dog.new\ncorgi.eat\n# =\u003e NotImplementedError, \"Animals need to be able to eat!\"\n```\n\n3) Another problem is you have now defined `Animal#speak` and `Animal#eat`, two stub methods of which they do nothing but raise an undesirable `NotImplementedError`.\n\nThe power of `Behaves` does not stop here either.\n\n## Features\n\n### Multi-behaviors\n\n`Behaves` allow you to define multiple behavior for a single behaving object. **This is not possible with inheritance**.\n\n```ruby\nclass Predator\n  extend Behaves\n\n  implements :hunt\nend\n\nclass Prey\n  extend Behaves\n\n  implements :run, :hide\nend\n\nclass Shark\n  extend Behaves\n\n  # Shark is both a `Predator` and a `Prey`\n  behaves_like Predator\n  behaves_like Prey\nend\n```\n\n### Inject Behaviors\n\nWhen someone decides to use `behaves` to define behaviors, they in turn lose the ability to utilize some other aspect of inheritance, one of it being inheriting methods.\n\nSo, `Behaves` now ship with a feature called `inject_behaviors` for that need!\n\n```ruby\nclass Dad\n  extend Behaves\n\n  implements :speak, :eat\n\n  inject_behaviors do\n    def traits; \"Dad's traits!\"; end\n  end\nend\n\nclass Child\n  extend Behaves\n\n  behaves_like Dad\n\n  def speak; \"BABA\"; end\n  def eat; \"NOM NOM\"; end\nend\n\n# Child.new.traits #=\u003e \"Dad's traits!\"\n```\n\nThis extends to more than just method implementation too, you can do anything you want! That's because the code inside `inject_behaviors` run in the context of the `Behaving Object`, also `self` inside `injected_behaviors` refers to the `Behaving Object`.\n\n*Do note that if you use this extensively, you might be better off using inheritance, since this will create more `Method` objects than inheritance.*\n\n### Private Behaviors\n\nPrivate behaviors can be defined like so:\n\n```ruby\nclass Interface\n  extend Behaves\n\n  implements :foo\n  implements :bar, private: true\nend\n\nclass Implementor\n  extend Behaves\n\n  behaves_like Interface\n\n  def foo\n    123\n  end\n\n  private\n\n  def bar\n    456\n  end\nend\n```\n\n## Tips\nIf you do not want to type `extend Behaves` every time, you can monkey patch `Behaves` onto `Object` class, like so:\n\n\u003e Object.send(:extend, Behaves)\n\n## Thoughts\n\nThe idea for `Behaves` stemmed from my research into [adapter pattern in Ruby](https://www.sitepoint.com/using-and-testing-the-adapter-design-pattern/) and José Valim's article on [Mocks and explicit contracts](http://blog.plataformatec.com.br/2015/10/mocks-and-explicit-contracts/).\n\nI found that the current idiom to achieve `behaviors` in Ruby is through inheritence, and then subsequently defining 'required' methods, which does nothing except raising a `NotImplementedError`. This approach is fragile, as it **does not guarantee behaviors**, runs the **risk of runtime errors**, and has an **opaque implementation**.\n\nThus with this comes the birth of `Behaves`.\n\nAlso referring to the article by José Valim, I really liked the idea of being able to use Mock as a noun. However, while the idea sounds good, you've now introduced a new problem in your codebase -- your Mock and your original Object might deviate from their implementation later on. Not a good design if it breaks. Elixir has `@behaviors` \u0026 `@callback` built in to keep them in sync. `Behaves` is inspired by that.\n\n## Development\n\nAfter checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` 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 at https://github.com/edisonywh/behaves. 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](https://opensource.org/licenses/MIT).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fedisonywh%2Fbehaves","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fedisonywh%2Fbehaves","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fedisonywh%2Fbehaves/lists"}