Module: Queries::Helpers

Instance Method Summary collapse

Instance Method Details

#boolean_param(params, attribute) ⇒ Boolean?

Returns:

  • (Boolean, nil)


6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/queries/helpers.rb', line 6

def boolean_param(params, attribute)
  return nil if attribute.nil? || params[attribute].nil?
  case params[attribute].class.name
  when 'TrueClass', 'FalseClass'
    params[attribute]
  when 'String'
    params[attribute].downcase == 'true' ? true : false
  when 'Symbol'
    params[attribute].to_s.downcase == 'true' ? true : false
  else
    puts Rainbow(params[attribute].class.name.to_s).purple
    raise
  end
end

#integer_param(params, attribute) ⇒ Object

Returns params.

Returns:

  • params



24
25
26
27
28
29
30
31
32
33
34
# File 'lib/queries/helpers.rb', line 24

def integer_param(params, attribute)
  return nil if attribute.nil? || params[attribute].nil?

  [params[attribute]].flatten.each do |v|
    next if v.kind_of?(Integer)
    next if Utilities::Strings.only_integer(v) # This rabbit hole feels a little janky
    raise TaxonWorks::Error::API, "values of #{attribute} must be integers (provided: #{params[attribute]})"
  end

  params[attribute]
end

#split_pairs(pairs) ⇒ Object



36
37
38
39
40
41
42
43
# File 'lib/queries/helpers.rb', line 36

def split_pairs(pairs)
  h = {}
  pairs.each do |p|
    k, v = p.split(':', 2)
    h[k] = v
  end
  h
end

#split_repeated_pairs(pairs) ⇒ Object



45
46
47
48
49
50
51
# File 'lib/queries/helpers.rb', line 45

def split_repeated_pairs(pairs)
  a = []
  pairs.each do |p|
    a << p.split(':', 2)
  end
  a
end