Sha256: 026e71e31894a569c92931ee8a8659eb92e8b719b8f0ccc82f7577eec7bc2cd9

Contents?: true

Size: 1.23 KB

Versions: 2

Compression:

Stored size: 1.23 KB

Contents

module DbMod
  module Statements
    module Configuration
      module Single
        # Wrapper for a statement or prepared method that returns
        # the first column of the first returned row. Strictly enforces
        # that exactly one row should be returned by the SQL result, and
        # will fail if zero or more than one row is returned.
        #
        #  def_statement(:a, 'SELECT 1').single(:value!)
        #
        #  def do_stuff
        #    a # => '1'
        #  end
        module RequiredValue
          # Enables this module to be passed to
          # {DbMod::Statements::Configuration.attach_result_processor} as the
          # +wrapper+ function, where it will return the first column of the
          # first row of the result set, or fail if anything other than
          # exactly one row is returned.
          #
          # @param results [Object] SQL result set
          # @return [String] the first column of the first returned row
          def self.call(results)
            fail DbMod::Exceptions::NoResults unless results.any?
            fail DbMod::Exceptions::TooManyResults if results.count > 1

            row = results[0]
            row[row.keys.first]
          end
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
db_mod-0.0.6 lib/db_mod/statements/configuration/single/required_value.rb
db_mod-0.0.5 lib/db_mod/statements/configuration/single/required_value.rb