Module: DbMod::Create
- Defined in:
- lib/db_mod/create.rb
Overview
Provides the create
function which is added to all modules which include DbMod. This function creates an object which exposes the functions defined in the module, allowing them to be used without namespace pollution.
The function may be used in two forms. It may be called with an options hash, in which case #db_connect will be used to create a new connection object. Alternatively an existing connection object may be passed, which will be used for all database queries.
Class Method Summary collapse
-
.instantiable_class(mod) ⇒ Class
private
Creates a class which inherits from the given module and can be instantiated with either a connection object or some connection options.
-
.setup(mod) ⇒ Object
Defines a module-specific
create
function for a module that has just had DbMod included.
Class Method Details
.instantiable_class(mod) ⇒ Class (private)
Creates a class which inherits from the given module and can be instantiated with either a connection object or some connection options.
40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/db_mod/create.rb', line 40 def self.instantiable_class(mod) Class.new do include mod define_method(:initialize) do || if .is_a? PGconn self.conn = else db_connect end end end end |
.setup(mod) ⇒ Object
Defines a module-specific create
function for a module that has just had DbMod included.
22 23 24 25 26 27 28 29 30 |
# File 'lib/db_mod/create.rb', line 22 def self.setup(mod) class << mod define_method(:create) do | = {}| @instantiable_class ||= Create.instantiable_class(self) @instantiable_class.new() end end end |