specify methods all backends must implement

Signed-off-by: Dominik Richter <dominik.richter@gmail.com>
This commit is contained in:
Dominik Richter 2015-09-02 11:53:25 +02:00
parent 05b4167971
commit 1c2ab098f5
2 changed files with 50 additions and 14 deletions

View file

@ -10,7 +10,18 @@ module Vulcano::Plugins
end end
def self.__register(name, obj) def self.__register(name, obj)
Vulcano::Backend.registry[name] = obj cl = Class.new(obj) do
include BackendCommon
end
Vulcano::Backend.registry[name] = cl
end
module BackendCommon
%w{ file run_command os }.each do |name|
define_method name do |*args|
raise NotImplementedError.new("Backend must implement the #{name}() method.")
end
end
end end
class FileCommon class FileCommon
@ -47,27 +58,27 @@ module Vulcano::Plugins
end end
def block_device? def block_device?
@file.type == :block_device type == :block_device
end end
def character_device? def character_device?
@file.type == :character_device type == :character_device
end end
def socket? def socket?
@file.type == :socket type == :socket
end end
def directory? def directory?
@file.type == :directory type == :directory
end end
def symlink? def symlink?
@file.type == :symlink type == :symlink
end end
def pipe? def pipe?
@file.type == :pipe? type == :pipe?
end end
def mode?(mode) def mode?(mode)

View file

@ -8,12 +8,37 @@ describe 'Vulcano::Plugins::Backend' do
it 'provides a name method for registering' do it 'provides a name method for registering' do
@child.must_respond_to :name @child.must_respond_to :name
end
describe 'when registering a plugin' do
before do
@child.name 'test' @child.name 'test'
# check if it's there @reg = Vulcano::Backend.registry
reg = Vulcano::Backend.registry end
reg.keys.must_include 'test'
reg['test'].must_equal @child after do
# cleanup @reg.delete('test')
reg.delete('test') end
it 'must have the backend registered' do
@reg.keys.must_include 'test'
@reg['test'].ancestors[2].must_equal @child
end
it 'must raise an error if file is not implemented' do
t = @reg['test'].new
proc { t.run_command }.must_raise NotImplementedError
end
it 'must raise an error if run_command is not implemented' do
t = @reg['test'].new
proc { t.file }.must_raise NotImplementedError
end
it 'must raise an error if os is not implemented' do
t = @reg['test'].new
proc { t.os }.must_raise NotImplementedError
end end
end end
end