Module: DbMod::Statements::Configuration

Defined in:
lib/db_mod/statements/configuration.rb,
lib/db_mod/statements/configuration/as.rb,
lib/db_mod/statements/configuration/as/csv.rb,
lib/db_mod/statements/configuration/single.rb,
lib/db_mod/statements/configuration/as/json.rb,
lib/db_mod/statements/configuration/defaults.rb,
lib/db_mod/statements/configuration/single/row.rb,
lib/db_mod/statements/configuration/single/value.rb,
lib/db_mod/statements/configuration/single/column.rb,
lib/db_mod/statements/configuration/single/required_row.rb,
lib/db_mod/statements/configuration/method_configuration.rb,
lib/db_mod/statements/configuration/single/required_value.rb

Overview

Provides additional functionality to statement and prepared methods, allowing additional processing of arguments and results using the dsl extensions exposed via MethodConfiguration.

Defined Under Namespace

Modules: As, Defaults, Single Classes: MethodConfiguration

Class Method Summary collapse

Class Method Details

.attach_param_processor(definition, params, config) ⇒ Proc (private)

Attaches any required parameter processing and validation to the method definition by wrapping it in a further proc as required.

Parameters:

Returns:

  • (Proc)

    a new wrapper for definition



41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/db_mod/statements/configuration.rb', line 41

def self.attach_param_processor(definition, params, config)
  wrapped =
    if params.is_a?(Array) && !params.empty?
      define_named_args_method(definition, params)

    elsif params.is_a?(Fixnum) && params > 0
      define_fixed_args_method(definition, params)
    else
      ->() { instance_exec(&definition) }
    end

  return wrapped unless config
  Defaults.extend(wrapped, params, config[:defaults])
end

.attach_result_processor(definition, processor) ⇒ Object (private)

Attach a processor to the chain of result processors for a method. The pattern here is something similar to rack’s middleware. A result processor is constructed with a method definition, and then acts as a replacement for the method, responding to #call. Subclasses must implement a process method, which should accept an SQL result set (or possibly, the result of other upstream processing), perform some transform on it and return the result.

Parameters:

  • definition (Proc)

    base method definition

  • processor (#call)

    result processor



112
113
114
# File 'lib/db_mod/statements/configuration.rb', line 112

def self.attach_result_processor(definition, processor)
  ->(*args) { processor.call instance_exec(*args, &definition) }
end

.attach_result_processors(definition, config) ⇒ Object (private)

Attaches any required result processing to the method definition, as may have been defined in a block passed to either of def_statement or def_prepared. This method is called before attach_param_processor, so that the method definition can be wrapped by the parameter processor. In this way processors attached here are assured access to method parameters after any initial processing and validation has taken place.

Parameters:

  • definition (Proc)

    base method definition

  • config (MethodConfiguration)

    configuration declared at method definition time



95
96
97
98
99
100
# File 'lib/db_mod/statements/configuration.rb', line 95

def self.attach_result_processors(definition, config)
  definition = Single.extend(definition, config)
  definition = As.extend(definition, config)

  definition
end

.def_configurable(mod, name, definition, params = 0) { ... } ⇒ Object

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, the base function which will perform database interaction and return an SQL result object

  • params (Array<Symbol>, Fixnum) (defaults to: 0)

    declares the parameters that the method will accept. Can either be an array of named parameters or a integer giving the arity of the function. [] may also be given to denote a no-argument method.

Yields:



23
24
25
26
27
28
29
30
# File 'lib/db_mod/statements/configuration.rb', line 23

def self.def_configurable(mod, name, definition, params = 0, &block)
  config = MethodConfiguration.new(&block).to_hash if block_given?

  definition = attach_result_processors(definition, config) if config
  definition = attach_param_processor(definition, params, config)

  mod.instance_eval { define_method(name, definition) }
end

.define_fixed_args_method(definition, arity) ⇒ Proc (private)

Wrap the given definition in a procedure that will validate that the correct number of arguments has been passed, before passing them on to the original method definition.

Parameters:

  • definition (Proc)

    base method definition

  • arity (Fixnum)

    expected number of arguments

Returns:

  • (Proc)

    new method definition



77
78
79
80
81
82
# File 'lib/db_mod/statements/configuration.rb', line 77

def self.define_fixed_args_method(definition, arity)
  lambda do |*args|
    Parameters.valid_fixed_args!(arity, args)
    instance_exec(*args, &definition)
  end
end

.define_named_args_method(definition, params) ⇒ Proc (private)

Wrap the given definition in a procedure that will validate any passed arguments, and transform them into an array that can be passed directly to PGconn.exec_params or PGconn.exec_prepared.

Parameters:

  • definition (Proc)

    base method definition

  • params (Array<Symbol>)

    list of method parameter names

Returns:

  • (Proc)

    new method definition



63
64
65
66
67
68
# File 'lib/db_mod/statements/configuration.rb', line 63

def self.define_named_args_method(definition, params)
  lambda do |*args|
    args = Parameters.valid_named_args! params, args
    instance_exec(*args, &definition)
  end
end