Module: Inputs

Defined in:
lib/inputs.rb,
lib/inputs/version.rb

Constant Summary collapse

VERSION =
"0.1.0"

Class Method Summary collapse

Class Method Details

._input_evaluatorObject



65
66
67
# File 'lib/inputs.rb', line 65

def self._input_evaluator
  @_input_evaluator ||= ->{ STDIN.gets }
end

._input_evaluator=(evaluator) ⇒ Object



61
62
63
# File 'lib/inputs.rb', line 61

def self._input_evaluator=(evaluator)
  @_input_evaluator = evaluator
end

.name(question) ⇒ Object



20
21
22
23
24
25
26
# File 'lib/inputs.rb', line 20

def self.name(question)
  output question
  name = _input_evaluator.call
  name = name.chomp
  output name.green
  name
end

.names(question) ⇒ Object



28
29
30
31
32
33
34
# File 'lib/inputs.rb', line 28

def self.names(question)
  output question + " (Comma separated)"
  names = _input_evaluator.call
  names = names.chomp.split(',').map(&:strip)
  output names.join(' & ').green
  names
end

.output(txt) ⇒ Object



57
58
59
# File 'lib/inputs.rb', line 57

def self.output(txt)
  puts txt.green
end

.pick(options, question: "Please choose:", option_output_eval: ->(key, option) { " Press #{key} for \"#{option}\"" }) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/inputs.rb', line 36

def self.pick(options, question: "Please choose:", option_output_eval: ->(key, option) { "  Press #{key} for \"#{option}\"" })
  option_map = options
    .map(&:to_s)
    .uniq
    .map
    .with_index { |x, i| [i+1, x] }
    .to_h

  begin
    output question

    option_map.each do |key, option|
      output option_output_eval.call(key, option)
    end
    input = _input_evaluator.call
    input = input.chomp.to_i
  end until option_map.keys.include?(input)

  option_map.fetch(input)
end

.yn(question) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/inputs.rb', line 5

def self.yn(question)
  begin
    output question + " [y/n]"
    input = _input_evaluator.call
    input = input.chomp
  end until %w(y n).include?(input.chomp)

  case input
  when 'y'
    true
  when 'n'
    false
  end
end