Class: Reading::Stats::Filter

Inherits:
Object
  • Object
show all
Defined in:
lib/reading/stats/filter.rb

Overview

The parts of a query that filter the data being queried, e.g. “genre=history”.

Class Method Summary collapse

Class Method Details

.filter(input, items) ⇒ Object

Determines which filters are contained in the given input, and then runs them to get the remaining Items. For the filters and their actions, see the constants below.

Parameters:

  • input (String)

    the query string.

  • items (Array<Item>)

    the Items on which to run the operation.

Returns:

  • (Object)

    the return value of the action.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/reading/stats/filter.rb', line 11

def self.filter(input, items)
  filtered_items = items

  split_input = input.split(INPUT_SPLIT)

  split_input[1..].each do |filter_input|
    match_found = false

    REGEXES.each do |key, regex|
      match = filter_input.match(regex)

      if match
        match_found = true

        begin
          filtered_items = filter_single(
            key,
            match[:predicate],
            match[:operator],
            filtered_items,
          )
        rescue InputError => e
          raise InputError, "#{e.message} in \"#{input}\""
        end
      end
    end

    unless match_found
      raise InputError, "Invalid filter \"#{filter_input}\" in \"#{input}\""
    end
  end

  filtered_items
end