Class: Appydave::Tools::RandomContext::Randomizer

Inherits:
Object
  • Object
show all
Defined in:
lib/appydave/tools/random_context/randomizer.rb

Overview

Loads query library, runs each query to check result count, filters to entries with “good” result counts, and picks one randomly.

Constant Summary collapse

BUNDLED_CONFIG_PATH =
File.expand_path('../../../../config/random-queries.yml', __dir__)
USER_CONFIG_PATH =
File.expand_path('~/.config/appydave/random-queries.yml')

Instance Method Summary collapse

Constructor Details

#initialize(config_path: USER_CONFIG_PATH, meta: false, executor: nil, bundled_config_path: BUNDLED_CONFIG_PATH, bootstrap: config_path == USER_CONFIG_PATH) ⇒ Randomizer

Returns a new instance of Randomizer.



16
17
18
19
20
21
22
23
24
# File 'lib/appydave/tools/random_context/randomizer.rb', line 16

def initialize(config_path: USER_CONFIG_PATH, meta: false, executor: nil,
               bundled_config_path: BUNDLED_CONFIG_PATH,
               bootstrap: config_path == USER_CONFIG_PATH)
  @config_path = config_path
  @meta = meta
  @executor = executor || method(:shell_execute)
  @bundled_config_path = bundled_config_path
  @bootstrap = bootstrap
end

Instance Method Details

#pickObject

Returns [QueryEntry, results_array] for the picked entry, or nil if no candidates.



27
28
29
30
31
32
33
34
35
36
# File 'lib/appydave/tools/random_context/randomizer.rb', line 27

def pick
  candidates = []

  load_entries.each do |entry|
    results = @executor.call(entry.command)
    candidates << [entry, results] if entry.good_count?(results.size)
  end

  candidates.sample
end

#runObject

Picks a random candidate and prints label + results to stdout.



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/appydave/tools/random_context/randomizer.rb', line 39

def run
  picked = pick
  unless picked
    puts 'No matching queries found'
    return
  end

  entry, results = picked
  puts "Question: \"#{entry.label}\""

  if @meta
    meta_results = @executor.call("#{entry.command} --meta")
    meta_results.each { |line| puts line }
  else
    results.each { |line| puts line }
  end
end