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 ConfigurableMethod#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 value > $min').single(:column)
def_prepared(:c, 'SELECT * FROM c WHERE id = $id').single(:row)

def do_stuff
  a # => "foo"
  b # => ['1','2','3',...]
  c # => 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

Configuration =

For process_method_results

DbMod::Statements::Configuration
COERCERS =

List of allowed parameters for #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_method(mod, name, type) ⇒ Object

Extend a method so that only some singular part of the SQL result set is returned. See above for more details.

Parameters:

  • mod (Module)

    module where the method has been defined

  • name (Symbol)

    method name

  • type (Symbol)

    one of SINGLE_TYPES



53
54
55
56
57
58
59
# File 'lib/db_mod/statements/configuration/single.rb', line 53

def self.extend_method(mod, name, type)
  unless COERCERS.key? type
    fail ArgumentError, "#{type} not in #{COERCERS.keys.join ', '}"
  end

  Configuration.process_method_results(mod, name, COERCERS[type])
end