Class: Reading::Stats::Query

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

Overview

Gives statistics on an array of Items.

Instance Method Summary collapse

Constructor Details

#initialize(input:, items:, result_formatters: {}) ⇒ Query

Returns a new instance of Query.

Parameters:

  • input (String)

    the query string.

  • items (Array<Item>)

    the Items to be queried.

  • result_formatters (Boolean, Hash{Symbol => Proc}) (defaults to: {})

    to alter the appearance of results; keys should be from among the keys of Operation::ACTIONS. Pre-made formatters for terminal output are in terminal_result_formatters.rb.



18
19
20
21
22
23
# File 'lib/reading/stats/query.rb', line 18

def initialize(input:, items:, result_formatters: {})
  @input = input
  @items = items
  @result_formatters = result_formatters
  @pastel = Pastel.new
end

Instance Method Details

#resultObject

Parses the query and returns the result.

Returns:



27
28
29
30
31
32
33
34
# File 'lib/reading/stats/query.rb', line 27

def result
  filtered_items = Stats::Filter.filter(input, items)
  grouped_items = Grouping.group(input, filtered_items)

  Operation.execute(input, grouped_items, result_formatters || {})
rescue Reading::Error => e
  raise e.class, pastel.bright_red(e.message)
end