Class: Checkpoint::Permits::Params

Inherits:
Object
  • Object
show all
Defined in:
lib/checkpoint/permits.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.



110
111
112
113
# File 'lib/checkpoint/permits.rb', line 110

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

Instance Attribute Details

#itemsObject (readonly)

Returns the value of attribute items.



108
109
110
# File 'lib/checkpoint/permits.rb', line 108

def items
  @items
end

#prefixObject (readonly)

Returns the value of attribute prefix.



108
109
110
# File 'lib/checkpoint/permits.rb', line 108

def prefix
  @prefix
end

Instance Method Details

#placeholdersObject



115
116
117
118
119
# File 'lib/checkpoint/permits.rb', line 115

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

#valuesObject



121
122
123
124
125
126
127
128
129
130
# File 'lib/checkpoint/permits.rb', line 121

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