class Query

Constants

ATTRIBUTE_AND_VALUE_DIVIDERS
Group
RepeatedFilter
VALUE_TO_LIST_DIVIDERS

Attributes

argv[R]

Public Class Methods

new(argv) click to toggle source
# File lib/query.rb, line 16
def initialize(argv)
  @argv = argv
end

Public Instance Methods

build() click to toggle source
# File lib/query.rb, line 20
def build
  @build ||= argv.map.with_object([]) do |argument, query_options|
    query_element = query_element(argument)

    if query_options.find { |el| el[:attribute] == query_element[:attribute] }
      repeated_filter(query_element[:attribute])
    end

    query_options << query_element
  end
end

Private Instance Methods

parse_value(value) click to toggle source
# File lib/query.rb, line 47
def parse_value(value)
  return value unless value&.match?(VALUE_TO_LIST_DIVIDERS)

  value.split(VALUE_TO_LIST_DIVIDERS)
end
query_element(argument) click to toggle source
# File lib/query.rb, line 36
def query_element(argument)
  return { attribute: argument, value: Group } unless argument.match?(ATTRIBUTE_AND_VALUE_DIVIDERS)

  attribute, value = argument.split(ATTRIBUTE_AND_VALUE_DIVIDERS)

  {
    attribute: sanitize(attribute),
    value: sanitize(parse_value(value))
  }
end
repeated_filter(attribute) click to toggle source
# File lib/query.rb, line 63
def repeated_filter(attribute)
  message = "#{attribute}: Every filter can be passed only once"
  raise RepeatedFilter, message
end
sanitize(query_part) click to toggle source
# File lib/query.rb, line 53
def sanitize(query_part)
  return unless query_part

  if query_part.is_a?(String)
    query_part.gsub(/~=/, '=').gsub(/~&/, '&')
  else
    query_part.map { |part| sanitize(part) }
  end
end