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/returning.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, Returning, 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



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/db_mod/statements/configuration.rb', line 48

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) ⇒ Proc (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 (Proc, #call)

    result processor

Returns:

  • (Proc)

    wrapped method definition



123
124
125
126
127
128
129
130
131
# File 'lib/db_mod/statements/configuration.rb', line 123

def self.attach_result_processor(definition, processor)
  if processor.is_a? Proc
    lambda do |*args|
      instance_exec(instance_exec(*args, &definition), &processor)
    end
  else
    ->(*args) { processor.call instance_exec(*args, &definition) }
  end
end

.attach_result_processors(definition, config) ⇒ Proc (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

Returns:

  • (Proc)

    extended method definition



104
105
106
107
108
109
110
# File 'lib/db_mod/statements/configuration.rb', line 104

def self.attach_result_processors(definition, config)
  definition = Single.extend(definition, config)
  definition = As.extend(definition, config)
  definition = Returning.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
31
32
33
34
35
36
37
# File 'lib/db_mod/statements/configuration.rb', line 23

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

  config &&= config.to_hash

  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



85
86
87
88
89
90
# File 'lib/db_mod/statements/configuration.rb', line 85

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



71
72
73
74
75
76
# File 'lib/db_mod/statements/configuration.rb', line 71

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