Class: Alf::Adapter

Inherits:
Object
  • Object
show all
Extended by:
Support::Registry
Defined in:
lib/alf/adapter.rb,
lib/alf/adapter/memory.rb,
lib/alf/adapter/folder.rb,
lib/alf/adapter/connection.rb,
lib/alf/adapter/folder/connection.rb,
lib/alf/adapter/memory/connection.rb,
lib/alf/adapter/connection/schema_cached.rb

Direct Known Subclasses

Folder, Memory

Defined Under Namespace

Classes: Connection, Folder, Memory

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Support::Registry

each, listen, listeners, register, registered

Constructor Details

#initialize(conn_spec) ⇒ Adapter

Creates an adapter instance.

Parameters:

  • conn_spec (Object)

    a connection specification.


76
77
78
# File 'lib/alf/adapter.rb', line 76

def initialize(conn_spec)
  @conn_spec = conn_spec
end

Instance Attribute Details

#conn_specObject (readonly)

The connection specification


71
72
73
# File 'lib/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.

Parameters:

  • conn_spec (Object)

    a connection specification

Returns:

  • (Class)

    the first registered class that recognizes `conn_spec`

Raises:

  • (ArgumentError)

    when no registered class recognizes the arguments


35
36
37
38
39
40
41
# File 'lib/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.

Parameters:

  • conn_spec (Hash)

    a connection specification

  • schema (Module)

    a module for scope definition

Returns:

  • (Adapter)

    an adapter instance


48
49
50
51
# File 'lib/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.

Parameters:

  • args (Array)

    arguments for the Adapter constructor

Returns:

  • (Boolean)

    true if an adapter may be built using `args`, false otherwise.


65
66
67
# File 'lib/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(...)

Parameters:

  • name (Symbol)

    name of the connection kind

  • clazz (Class)

    class that implemented the connection

See Also:

  • autodetect and recognizes?

22
23
24
# File 'lib/alf/adapter.rb', line 22

def register(name, clazz)
  super([name, clazz], Adapter)
end

Instance Method Details

#connectObject

Yields the block with a connection and closes it afterwards


86
87
88
89
90
91
# File 'lib/alf/adapter.rb', line 86

def connect
  c = connection
  yield(c)
ensure
  c.close if c
end

#connectionObject

Returns a low-level connection on this adapter


81
82
83
# File 'lib/alf/adapter.rb', line 81

def connection
  Connection.new(conn_spec)
end