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…