Interfacable
Impose interfaces on classes and let this gem automatically check that the interface constraints are met.
Installation
Add this line to your application's Gemfile:
gem 'interfacable'
And then execute:
$ bundle install
Usage
In this example:
class Class
include Interfacable
end
module Carrier
def call(number); end
def text(number, text); end
end
class Giffgaff
implements Carrier
end
An attempt to load this code will result in the following error:
Giffgaff must implement: (Interfacable::Error)
- Carrier#text
- Carrier#call
It will keep failing until Giffgaff
defines those methods.
Correctly. E.g.:
class Giffgaff
def call(number); end
def text(number); end
end
Will fail because of method signature mismatch:
Giffgaff must implement correctly: (Interfacable::Error)
- Carrier#text:
- expected arguments: (req, req)
- actual arguments: (req)
Rails
For extra piece of mind, we can noop interface checking in production:
# config/initializers/interfacable.rb
class Class
include Interfacable
def implements(*args); end if Rails.env.production?
end