Module: DbMod::Statements::Configuration::Single

Defined in:
lib/db_mod/statements/configuration/single.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/single/required_value.rb

Overview

Provides convenience extensions for statement and prepared methods that return only a single result, row, or column. The normal way to access this functionality is via MethodConfiguration#single, which is available when defining a statement method or prepared method:

def_statement(:a, 'SELECT name FROM a WHERE id=$1') { single(:value) }
def_prepared(:b, 'SELECT id FROM b WHERE x > $y') { single(:column) }
def_prepared(:c, 'SELECT * FROM c WHERE id = $id') { single(:row) }

def do_stuff
  a(1)    # => "foo"
  b(y: 2) # => ['1','2','3',...]
  c id: 3 # => Hash
end

.single(:row) and .single(:value) will return the first row or the first value of the first row respectively, or nil if no results are found. To generate a Exceptions::NoResults failure instead of returning nil, use .single(:row!) or .single(:value!).

Defined Under Namespace

Modules: Column, RequiredRow, RequiredValue, Row, Value

Constant Summary collapse

COERCERS =

List of allowed parameters for MethodConfiguration#single, and the methods used to process them.

{
  value: Single::Value,
  value!: Single::RequiredValue,
  row: Single::Row,
  row!: Single::RequiredRow,
  column: Single::Column
}

Class Method Summary collapse

Class Method Details

.extend(definition, config) ⇒ Proc

Extend the given method definition with additional result coercion, if specified using {MethodConfiguration#single.

Parameters:

  • definition (Proc)

    base method definition

  • config (MethodConfiguration)

    method configuration

Returns:

  • (Proc)

    wrapped method definition, or the original definition if no coercion has been specified



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

def self.extend(definition, config)
  type = config[:single]
  return definition if type.nil?

  Configuration.attach_result_processor definition, COERCERS[type]
end