I just published my first gem on RubyGems, and would love to hear your feedback!
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.
Add this line to your application's Gemfile:
This is how you define behaviors with
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
class Dog extend Behaves behaves_like Animal end
Voilà, that's all it takes to define behaviors! Now if
Dog does not implement
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.
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
Best of all? No more runtime errors for defining behaviors!
Do let me know what you think!