Class: Checkpoint::DB::Params

Inherits:
Object
  • Object
show all
Defined in:
lib/checkpoint/db/params.rb

Overview

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.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(items, prefix) ⇒ Params

Returns a new instance of Params.



13
14
15
16
# File 'lib/checkpoint/db/params.rb', line 13

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

Instance Attribute Details

#itemsObject (readonly)

Returns the value of attribute items.



11
12
13
# File 'lib/checkpoint/db/params.rb', line 11

def items
  @items
end

#prefixObject (readonly)

Returns the value of attribute prefix.



11
12
13
# File 'lib/checkpoint/db/params.rb', line 11

def prefix
  @prefix
end

Instance Method Details

#placeholdersObject



18
19
20
21
22
# File 'lib/checkpoint/db/params.rb', line 18

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

#valuesObject



24
25
26
27
28
29
30
31
32
33
# File 'lib/checkpoint/db/params.rb', line 24

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