Class: Alf::Adapter
- Inherits:
-
Object
- Object
- Alf::Adapter
- Extended by:
- Support::Registry
- Defined in:
- lib/alf-adapter/alf/adapter.rb,
lib/alf-adapter-fs/alf/adapter/folder.rb,
lib/alf-adapter/alf/adapter/connection.rb,
lib/alf-adapter-fs/alf/adapter/folder/connection.rb,
lib/alf-adapter/alf/adapter/connection/schema_cached.rb
Direct Known Subclasses
Defined Under Namespace
Classes: Connection, Folder
Instance Attribute Summary collapse
-
#conn_spec ⇒ Object
readonly
The connection specification.
Class Method Summary collapse
-
.autodetect(conn_spec) ⇒ Class
Auto-detect the connection class to use for specific arguments.
-
.factor(conn_spec) ⇒ Adapter
Builds an adapter instance through the autodetection adapter mechanism.
-
.recognizes?(args) ⇒ Boolean
Returns true if args can be used for get an adapter instance, false otherwise.
-
.register(name, clazz) ⇒ Object
Register an adapter class under a specific name.
Instance Method Summary collapse
-
#connect ⇒ Object
Yields the block with a connection and closes it afterwards.
-
#connection ⇒ Object
Returns a low-level connection on this adapter.
-
#initialize(conn_spec) ⇒ Adapter
constructor
Creates an adapter instance.
Methods included from Support::Registry
each, listen, listeners, register, registered
Constructor Details
#initialize(conn_spec) ⇒ Adapter
Creates an adapter instance.
76 77 78 |
# File 'lib/alf-adapter/alf/adapter.rb', line 76 def initialize(conn_spec) @conn_spec = conn_spec end |
Instance Attribute Details
#conn_spec ⇒ Object (readonly)
The connection specification
71 72 73 |
# File 'lib/alf-adapter/alf/adapter.rb', line 71 def conn_spec @conn_spec end |
Class Method Details
.autodetect(conn_spec) ⇒ Class
Auto-detect the connection class to use for specific arguments.
This method returns an instance of the first registered Connection class that returns true to an invocation of recognizes?(args). It raises an ArgumentError if no such class can be found.
35 36 37 38 39 40 41 |
# File 'lib/alf-adapter/alf/adapter.rb', line 35 def autodetect(conn_spec) name, clazz = registered.find{|nc| nc.last.recognizes?(conn_spec) } unless clazz raise ArgumentError, "No adapter for `#{conn_spec.inspect}`" end clazz end |
.factor(conn_spec) ⇒ Adapter
Builds an adapter instance through the autodetection adapter mechanism.
48 49 50 51 |
# File 'lib/alf-adapter/alf/adapter.rb', line 48 def factor(conn_spec) return conn_spec if conn_spec.is_a?(Adapter) autodetect(conn_spec).new(conn_spec) end |
.recognizes?(args) ⇒ Boolean
Returns true if args can be used for get an adapter instance, false otherwise.
When returning true, an immediate invocation of new(*args) should succeed. While runtime exception are admitted (no such connection, for example), argument errors should not occur (missing argument, wrong typing, etc.).
Please be specific in the implementation of this extension point, as registered adapters for a chain and each of them should have a chance of being selected.
65 66 67 |
# File 'lib/alf-adapter/alf/adapter.rb', line 65 def recognizes?(args) false end |
.register(name, clazz) ⇒ Object
Register an adapter class under a specific name.
Registered class must implement a recognizes? method that takes an array of arguments; it must returns true if an adapter instance can be built using those arguments, false otherwise.
Example:
Adapter.register(:sqlite, MySQLiteAdapterClass)
Adapter.sqlite(...) # MySQLiteAdapterClass.new(...)
Adapter.autodetect(...) # => MySQLiteAdapterClass.new(...)
22 23 24 |
# File 'lib/alf-adapter/alf/adapter.rb', line 22 def register(name, clazz) super([name, clazz], Adapter) end |
Instance Method Details
#connect ⇒ Object
Yields the block with a connection and closes it afterwards
86 87 88 89 90 91 |
# File 'lib/alf-adapter/alf/adapter.rb', line 86 def connect c = connection yield(c) ensure c.close if c end |
#connection ⇒ Object
Returns a low-level connection on this adapter
81 82 83 |
# File 'lib/alf-adapter/alf/adapter.rb', line 81 def connection Connection.new(conn_spec) end |