Method: Magiq::Query#verify!

Defined in:
lib/magiq/query.rb

#verify!Object



320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
# File 'lib/magiq/query.rb', line 320

def verify!
  if !@params
    raise RuntimeError, "verify! was called before extract!"
  end

  builder.constraints.each do |(op, keys, opts)|
    case op
    when :exclusive
      found_keys = keys.select { |k| params.key?(k) }

      next if found_keys.empty? || found_keys.one?

      raise ParamsError, "The following parameters are not permitted " \
      "to be provided together: #{found_keys.join(', ')}"
    when :mutual
      exclusives = opts[:exclusive] && Array(opts[:exclusive]) || []
      found_keys = keys.select { |k| params.key?(k) }
      found_excl = exclusives.select { |k| params.key?(k) }

      next if found_keys.empty?
      next if found_keys.empty? && found_excl.empty?
      next if found_keys == keys && found_excl.empty?

      if found_excl.any?
        raise ParamsError, "The provided " \
        "parameter#{found_keys.one? ? '' : 's'}: " \
        "#{found_keys.map { |k| "`#{k}`" }.join(', ')} " \
        "#{found_keys.one? ? 'is' : 'are'} mutually exclusive to: " \
        "#{found_excl.map { |k| "`#{k}`" }.join ', '}."
      end

      raise ParamsError, "The provided " \
      "parameter#{found_keys.one? ? '' : 's'}: " \
      "#{found_keys.map { |k| "`#{k}`" }.join(', ')} requires: " \
      "#{(keys - found_keys).map { |k| "`#{k}`" }.join(', ')}."
    end
  end
end