forked from ecin/adapter
-
Notifications
You must be signed in to change notification settings - Fork 2
Creating an Adapter
jnunemaker edited this page Nov 16, 2012
·
4 revisions
Creating your own adapter is as easy as defining a few methods -- read, write, delete and clear. Here is an example that defines an adapter where the client is a ruby hash. Adapters can also work with options, you can read more at Allowing for Options.
Adapter.define(:some_name) do
def read(key)
client[key]
end
def write(key, value)
client[key] = value
end
def delete(key)
client.delete(key)
end
def clear
client.clear
end
end
adapter = Adapter[:some_name].new({})
adapter.read('foo') # nil
adapter.write('foo', 'bar')
adapter.read('foo') # "foo"
Adapter.define works with a name and block, name and module, or name, module and block. When module and block are provided, the module is included into the adapter first and the block second. This allows you to take an existing adapter defined in a module and tweak a few things in the block (such as encode and decode).
There are some rspec shared examples available if you are creating and testing your own adapter.