lib/db_mod/statements/configuration/single.rb in db_mod-0.0.4 vs lib/db_mod/statements/configuration/single.rb in db_mod-0.0.5

- old
+ new

@@ -8,55 +8,49 @@ module Statements module Configuration # 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 + # 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 value > $min').single(:column) - # def_prepared(:c, 'SELECT * FROM c WHERE id = $id').single(:row) + # 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 # => "foo" - # b # => ['1','2','3',...] - # c # => Hash + # 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 # {DbMod::Exceptions::NoResults} failure # instead of returning +nil+, use +.single(:row!)+ or # +.single(:value!)+. module Single - # For process_method_results - Configuration = DbMod::Statements::Configuration - - # List of allowed parameters for {#single}, + # List of allowed parameters for {MethodConfiguration#single}, # and the methods used to process them. COERCERS = { value: Single::Value, value!: Single::RequiredValue, row: Single::Row, row!: Single::RequiredRow, column: Single::Column } - # Extend a method so that only some singular part of - # the SQL result set is returned. - # See above for more details. + # Extend the given method definition with additional + # result coercion. # - # @param mod [Module] module where the method has been defined - # @param name [Symbol] method name - # @param type [Symbol] one of {SINGLE_TYPES} - def self.extend_method(mod, name, type) - unless COERCERS.key? type - fail ArgumentError, "#{type} not in #{COERCERS.keys.join ', '}" - end + # @param definition [Proc] base method definition + # @param config [MethodConfiguration] method configuration + def self.extend(definition, config) + type = config[:single] + return definition if type.nil? - Configuration.process_method_results(mod, name, COERCERS[type]) + Configuration.attach_result_processor definition, COERCERS[type] end end end end end