Module: DbMod::Statements

Defined in:
lib/db_mod/statements.rb,
lib/db_mod/statements/prepared.rb,
lib/db_mod/statements/statement.rb,
lib/db_mod/statements/parameters.rb,
lib/db_mod/statements/configurable_method.rb

Overview

Functions allowing DbMod modules to declare SQL statements that can be called later via automatically declared instance methods.

See Statement for details on def_statement and Prepared for details on def_prepared.

Defined Under Namespace

Modules: Parameters, Prepared, Statement Classes: ConfigurableMethod

Class Method Summary collapse

Class Method Details

.configurable_method(mod, name, definition) ⇒ DbMod::Statements::ConfigurableMethod

Used by submodules to when defining a method as declared by def_statement or def_prepared. Wraps the defined method so that it may be extended with additional argument and result processing.

Parameters:

  • mod (Module)

    the module where the method has been declared

  • name (Symbol)

    the name of the module that has been defined

  • definition (Proc)

    method definition

Returns:



30
31
32
33
34
# File 'lib/db_mod/statements.rb', line 30

def self.configurable_method(mod, name, definition)
  mod.instance_eval { define_method(name, definition) }

  ConfigurableMethod.new(mod, name)
end

.extend_method(mod, name, wrapper) ⇒ Object

Used by ConfigurableMethod (and associated code) to wrap a defined statement method or prepared method with additional parameter or result processing. A wrapper method definition should be provided, which will be called in place of the original method. It will be called with the original method proc as a first argument followed by the original method arguments (before DbMod has made any attempt to validate them!). It is expected to yield to the original proc at some point, although it is allowed to do whatever it wants with the results before returning them.

Parameters:

  • mod (Module)

    the module where the method has been defined

  • name (Symbol)

    the method name

  • wrapper (Proc)

    a function which will be used to wrap the original method definition



49
50
51
52
53
54
55
# File 'lib/db_mod/statements.rb', line 49

def self.extend_method(mod, name, wrapper)
  mod.instance_eval do
    wrapped = instance_method(name)

    define_method name, ->(*args) { wrapper.call wrapped.bind(self), *args }
  end
end

.setup(mod) ⇒ Object

Called when a module includes DbMod, defines module-level def_statement and def_prepared dsl methods.



15
16
17
18
# File 'lib/db_mod/statements.rb', line 15

def self.setup(mod)
  DbMod::Statements::Prepared.setup(mod)
  DbMod::Statements::Statement.setup(mod)
end