Hey guys!
I just published my first gem on RubyGems, and would love to hear your feedback!
Behaves
Behaves is a gem that helps you define behaviors between classes. Say goodbye to runtime error when defining behaviors.
Behaves is especially useful for dealing with adapter patterns by making sure that all of your adapters define the required behaviors. See usage below for more examples.
Detailed explanations in the sections below.
Installation
Add this line to your application's Gemfile:
gem 'behaves'
Usage
This is how you define behaviors with behaves
.
First, define required methods on the Behavior Object
with the implements
method, which take a list of methods.
class Animal
extend Behaves
implements :speak, :eat
end
Then, 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
.
class Dog
extend Behaves
behaves_like Animal
end
Voilà, that's all it takes to define behaviors! Now if Dog
does not implement speak
and…
Description
Behaves is a gem that helps you maintain contracts between different classes. This is especially useful for dealing for adapter patterns by making sure that all of your adapters define the required behaviors.
The idea for Behaves
stemmed from my research into adapter pattern in Ruby and José Valim's article on Mocks and explicit contracts.
I found that the current idiom to achieve behaviors
in Ruby is through Inheritence
, and then subsequently defining a "required" (I put quotation marks around it, because it's not exactly required
until you run it) method, which does nothing except raising a NotImplementedError
. While I don't necessarily think it's bad, I do think that there could be an alternative that's more explicit, less boilerplate, cleaner ancestors hierachy, thus the birth of Behaves
.
Best of all? No more runtime errors for defining behaviors!
Do let me know what you think!
Top comments (2)
Ooh I haven't had a chance to dig into the repo or source but this sounds interesting!
Definitely bookmarked and will take a look soon!
Thanks! Would love to hear your opinions :D