Sha256: ecd4e4793e4667af1f04a4f7136f995aec29e75944ee21b5c37f413da1aaa703

Contents?: true

Size: 969 Bytes

Versions: 4

Compression:

Stored size: 969 Bytes

Contents

# frozen_string_literal: true

module Checkpoint
  module DB
    # A helper for building placeholder variable names from items in a list and
    # providing a corresponding hash of values. A prefix with some mnemonic
    # corresponding to the column is recommended. For example, if the column is
    # `agent_token`, using the prefix `at` will yield `$at_0`, `$at_1`, etc. for
    # an IN clause.
    class Params
      attr_reader :items, :prefix

      def initialize(items, prefix)
        @items  = [items].flatten
        @prefix = prefix
      end

      def placeholders
        0.upto(items.size - 1).map do |i|
          :"$#{prefix}_#{i}"
        end
      end

      def values
        items.map.with_index do |item, i|
          value = if item.respond_to?(:sql_value)
                    item.sql_value
                  else
                    item.to_s
                  end
          [:"#{prefix}_#{i}", value]
        end
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
checkpoint-1.1.3 lib/checkpoint/db/params.rb
checkpoint-1.1.2 lib/checkpoint/db/params.rb
checkpoint-1.1.1 lib/checkpoint/db/params.rb
checkpoint-1.1.0 lib/checkpoint/db/params.rb