On Fri, Jul 25, 2008 at 9:27 AM, Mylka, Antoni
<[EMAIL PROTECTED]> wrote:
> Hello,
>
> I'm trying to wrap my head around the discovery mechanism for the
> ActiveRecord-JDBC drivers. I'm new to Ruby and got stuck on the
> initialization of the JdbcAdapter class
>
> ADAPTER_TYPES = ::JdbcSpec.constants.map{ |c|
>        ::JdbcSpec.const_get c }.select{ |c|
>        c.respond_to? :adapter_selector }.map{|c|
>        c.adapter_selector }.inject({}) { |h,val|
>        h[val[0]] = val[1]; h }
>
>      def initialize(connection, logger, config)
>        super(connection, logger)
>        @config = config
>        dialect = config[:dialect] || config[:driver]
>        for reg, func in ADAPTER_TYPES
>          if reg === dialect.to_s
>            func.call(@config,self)
>          end
>        end
>        connection.adapter = self
>      end
>
> This is somehow connected with two methods defined in the module of each
> JDBC adapter, e.g:
>
>    def self.adapter_selector
>      [/mysql/i, lambda {|cfg,adapt| adapt.extend(::JdbcSpec::MySQL)}]
>    End
>
> Could someone please describe what is going on here? How does the
> JdbcAdapter know which module use, so that the JdbcAdapter methods are
> overridden by those defined in the appropriate adapter module (e.g.
> MySQL).

Basically, the class method adapter_selector is called on each module
defined in the JdbcSpec module, and its results (an array consisting
of a regular expression and a proc) are collected and cached.
When a new connection is made, either the "dialect" or "driver"
attribute of the connection specification is used, and that string is
matched against the regular expression. If it matches, the proc is
called with the adapter instance, which is augmented by extending with
the driver-specific module, as shown above in the MySQL case.

Does that help?

/Nick
_______________________________________________
Jruby-extras-devel mailing list
[email protected]
http://rubyforge.org/mailman/listinfo/jruby-extras-devel

Reply via email to