Sha256: 59d0d4e54eb44019c272e3f2eb4fd279fbfd87e5deeb3b6dfb5d7cb0036ac239

Contents?: true

Size: 1.48 KB

Versions: 1

Compression:

Stored size: 1.48 KB

Contents

require_relative 'as/csv'
require_relative 'as/json'

module DbMod
  module Statements
    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 {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
        # 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 the given method definition with additional
        # result coercion, if specified using {MethodConfiguration#as}.
        #
        # @param definition [Proc] base method definition
        # @param config [MethodConfiguration] method configuration
        # @return [Proc] wrapped method definition, or the original
        #   definition if no coercion has been specified
        def self.extend(definition, config)
          type = config[:as]
          return definition if type.nil?

          Configuration.attach_result_processor definition, COERCERS[type]
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
db_mod-0.0.6 lib/db_mod/statements/configuration/as.rb