module CompositePrimaryKeys

Constants

ESCAPE_CHAR
ID_SEP
ID_SET_SEP

Public Class Methods

normalize(ids, cpk_size) click to toggle source

Convert mixed representation of CPKs (by strings or arrays) to normalized representation (just by arrays).

‘ids` is Array that may contain:

  1. A CPK represented by an array or a string.

  2. An array of CPKs represented by arrays or strings.

There is an issue. Let ‘ids` contain an array with serveral strings. We can’t distinguish case 1 from case 2 there in general. E.g. the item can be an array containing appropriate number of strings, and each string can contain appropriate number of commas. We consider case 2 to win there.

# File lib/composite_primary_keys/composite_arrays.rb, line 22
def self.normalize(ids, cpk_size)
  ids.map do |id|
    if Utils.cpk_as_array?(id, cpk_size) && id.any? { |item| !Utils.cpk_as_string?(item, cpk_size) }
      # CPK as an array - case 1
      id
    elsif id.is_a?(Array)
      # An array of CPKs - case 2
      normalize(id, cpk_size)
    elsif id.is_a?(String)
      # CPK as a string - case 1
      CompositeKeys.parse(id)
    else
      id
    end
  end
end