module Utils

Public Instance Methods

clean_filters(filters) click to toggle source
# File lib/chewie/utils.rb, line 36
def clean_filters(filters)
  filters.reject do |_, value|
    value.nil?
  end.with_indifferent_access
end
combine_values(keys, filters) click to toggle source
# File lib/chewie/utils.rb, line 42
def combine_values(keys, filters)
  keys.map { |key| filters[key] }
end
context(key, **options) { || ... } click to toggle source
# File lib/chewie/utils.rb, line 28
def context(key, **options)
  options.merge(key => yield)
end
expose_or_return_value(value, with, should_format) click to toggle source
# File lib/chewie/utils.rb, line 52
def expose_or_return_value(value, with, should_format)
  is_term = with == :term
  is_term && should_format ? value.pop : value
end
format_values(values, combine, format) click to toggle source
# File lib/chewie/utils.rb, line 46
def format_values(values, combine, format)
  [values].flatten.map do |value|
    combine.any? ? format.call(value, combine) : format.call(value)
  end
end
reduce_handlers(data: {}, context: :query) click to toggle source
# File lib/chewie/utils.rb, line 10
def reduce_handlers(data: {}, context: :query)
  filters = data[:filters]
  context_handlers = handlers[context]

  return unless context_handlers.present?

  grouped_handlers = context_handlers.group_by {|h| h[:query] }

  context_handlers.each.with_object({}) do |handler, hsh|
    next if handler.empty?

    handler = Chewie::Handler::Reduced.
      new(context: context, handler: handler, filters: filters)

    handler.reduce_with(grouped_handlers, hsh)
  end
end
set_handler(context: :query, handler: {}) click to toggle source
# File lib/chewie/utils.rb, line 2
def set_handler(context: :query, handler: {})
  if handlers[context].present?
    handlers[context].push(handler)
  else
    handlers[context] = [handler]
  end
end
set_query_data(query, filters) click to toggle source
# File lib/chewie/utils.rb, line 32
def set_query_data(query, filters)
  { filters: clean_filters(filters).merge(query: query) }
end