Class: DbMod::Statements::Configuration::MethodConfiguration
- Inherits:
-
Object
- Object
- DbMod::Statements::Configuration::MethodConfiguration
- Defined in:
- lib/db_mod/statements/configuration/method_configuration.rb
Overview
Instance Method Summary collapse
-
#as(type) ⇒ self
Extend the method by converting results into a given format, using one of the coercion methods defined under As.
-
#defaults(*defaults) ⇒ Object
Declares default values for method parameters.
-
#initialize { ... } ⇒ MethodConfiguration
constructor
Creates a new configuration object to be used as the scope for blocks passed to
def_statement
anddef_prepared
declarations. -
#one_of!(value, allowed) ⇒ Object
private
Guard method which asserts that a configuration setting is one of the allowed values in the given hash.
-
#set_once!(setting, value) ⇒ Object
private
Guard method which asserts that a configuration method may not be called more than once, or else raises Exceptions::BadMethodConfiguration.
-
#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.
-
#to_hash ⇒ Hash
Return all given settings in a hash.
Constructor Details
#initialize { ... } ⇒ MethodConfiguration
Creates a new configuration object to be used as the scope for blocks passed to def_statement
and def_prepared
declarations.
18 19 20 21 |
# File 'lib/db_mod/statements/configuration/method_configuration.rb', line 18 def initialize(&block) @settings = {} instance_exec(&block) if block_given? 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.
29 30 31 32 33 34 |
# File 'lib/db_mod/statements/configuration/method_configuration.rb', line 29 def as(type) one_of! type, Configuration::As::COERCERS set_once! :as, type self end |
#defaults(*defaults) ⇒ Object
Declares default values for method parameters. For methods with named parameters, a hash of argument names and default values should be provided. For methods with indexed parameters, an array of 1..n default values should be provided, where n is the method’s arity. In this case default values will be applied to the right-hand side of the argument list, as with normal parameter default rules.
62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/db_mod/statements/configuration/method_configuration.rb', line 62 def defaults(*defaults) if defaults.size == 1 && defaults.first.is_a?(Hash) defaults = defaults.first elsif defaults.last.is_a? Hash fail ArgumentError, 'mixed default declaration not allowed' end set_once! :defaults, defaults self end |
#one_of!(value, allowed) ⇒ Object (private)
Guard method which asserts that a configuration setting is one of the allowed values in the given hash.
101 102 103 104 105 |
# File 'lib/db_mod/statements/configuration/method_configuration.rb', line 101 def one_of!(value, allowed) return if allowed.key? value fail ArgumentError, "#{value} not in #{allowed.keys.join ', '}" end |
#set_once!(setting, value) ⇒ Object (private)
Guard method which asserts that a configuration method may not be called more than once, or else raises Exceptions::BadMethodConfiguration.
88 89 90 91 92 93 94 |
# File 'lib/db_mod/statements/configuration/method_configuration.rb', line 88 def set_once!(setting, value) if @settings.key? setting fail Exceptions::BadMethodConfiguration, "#{setting} already called" end @settings[setting] = value 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.
44 45 46 47 48 49 |
# File 'lib/db_mod/statements/configuration/method_configuration.rb', line 44 def single(type) one_of! type, Configuration::Single::COERCERS set_once! :single, type self end |
#to_hash ⇒ Hash
Return all given settings in a hash.
76 77 78 |
# File 'lib/db_mod/statements/configuration/method_configuration.rb', line 76 def to_hash @settings end |