Class: DbMod::Statements::Configuration::ConfigurableMethod

Inherits:
Object
  • Object
show all
Defined in:
lib/db_mod/statements/configuration/configurable_method.rb

Overview

Encapsulates a method that has just been defined via the dsl exposed in DbMod::Statements so that it can be extended with additional processing such as result coercion.

The pattern here is something similar to rack’s middleware. Calling any of the extension methods below will replace the original method defined by def_prepared or def_statement with a wrapper function that may perform processing on given arguments, pass them to the original function, then perform additional processing on the result.

Instance Method Summary collapse

Constructor Details

#initialize(mod, name) ⇒ ConfigurableMethod

Encapsulate a method that has been newly defined by a DbMod dsl function, for additional configuration.

Parameters:

  • mod (Module)

    the DbMod enabled module where the method was defined

  • name (Symbol)

    the method name



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

def initialize(mod, name)
  @mod = mod
  @name = name
  @already_called = {}
end

Instance Method Details

#as(type) ⇒ self

Extend the method by converting results into a given format, using one of the coercion methods defined under As.

Parameters:

  • type (:csv, :json)

    output format for the method

Returns:

  • (self)


37
38
39
40
41
42
43
# File 'lib/db_mod/statements/configuration/configurable_method.rb', line 37

def as(type)
  called! :as

  Configuration::As.extend_method(@mod, @name, type)

  self
end

#called!(method) ⇒ Object (private)

Guard method which asserts that a configuration method may not be called more than once, or else raises Exceptions::BadMethodConfiguration.

Parameters:

  • method (Symbol)

    method being called



68
69
70
71
72
73
74
# File 'lib/db_mod/statements/configuration/configurable_method.rb', line 68

def called!(method)
  if @already_called[method]
    fail Exceptions::BadMethodConfiguration, "#{method} already called"
  end

  @already_called[method] = true
end

#single(type) ⇒ self

Extend the method by extracting a singular part of the result set, for queries expected to only return one row, one column, or one row with a single value. See Single for more details.

Parameters:

  • type (Symbol)

    see SINGLE_TYPES

Returns:

  • (self)


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

def single(type)
  called! :single

  Configuration::Single.extend_method(@mod, @name, type)

  self
end