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

- old
+ new

@@ -6,41 +6,34 @@ module Configuration # Contains coercers and other functions that allow # module instance methods returning an SQL result set # to be extended with additional result coercion and # formatting. The normal way to access this functionality - # is via {ConfigurableMethod#as}, + # is via {MethodConfiguration#as}, # which is available when defining a statement method # or prepared method: # # def_statement(:a, 'SELECT a, b, c FROM foo').as(:csv) # def_prepared(:b, 'SELECT d, e, f FROM bar').as(:csv) module As - # For extend_method - Configuration = DbMod::Statements::Configuration - # List of available result coercion methods. # Only keys defined here are allowed as arguments # to {DbMod::Statements::Configuration::ConfigurableMethod#as}. COERCERS = { csv: As::Csv, json: As::Json } - # Extend a method so that the SQL result set it - # returns will be coerced to the given type. - # See {COERCERS} for a list of defined coercion - # methods. + # 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] type to which result set should be coerced - 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[:as] + 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