Module: Utils

Included in:
Chewie, Chewie::Query::Bool, Chewie::Query::FullText, Chewie::Query::TermLevel
Defined in:
lib/chewie/utils.rb

Instance Method Summary collapse

Instance Method Details

#clean_filters(filters) ⇒ Object



36
37
38
39
40
# 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) ⇒ Object



42
43
44
# File 'lib/chewie/utils.rb', line 42

def combine_values(keys, filters)
  keys.map { |key| filters[key] }
end

#context(key, **options) ⇒ Object



28
29
30
# File 'lib/chewie/utils.rb', line 28

def context(key, **options)
  options.merge(key => yield)
end

#expose_or_return_value(value, with, should_format) ⇒ Object



52
53
54
55
# 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) ⇒ Object



46
47
48
49
50
# 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) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# 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: {}) ⇒ Object



2
3
4
5
6
7
8
# 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) ⇒ Object



32
33
34
# File 'lib/chewie/utils.rb', line 32

def set_query_data(query, filters)
  { filters: clean_filters(filters).merge(query: query) }
end