Module: Inputs
- Defined in:
- lib/inputs.rb,
lib/inputs/version.rb
Constant Summary collapse
- VERSION =
"0.2.0"
Class Method Summary collapse
- ._input_evaluator ⇒ Object
- ._input_evaluator=(evaluator) ⇒ Object
- ._multi_line_input_evaluator ⇒ Object
- ._multi_line_input_evaluator=(evaluator) ⇒ Object
-
.name(question) ⇒ Object
Outputs a question and returns an input string.
-
.name!(question) ⇒ Object
Outputs a question and returns an input string.
- .names(question) ⇒ Object
- .output(txt) ⇒ Object
- .pick(options, question: "Please choose:", option_output_eval: ->(key, option) { " Press #{key} for \"#{option}\"" }) ⇒ Object
-
.text(question) ⇒ Object
Multiline input, exit via CTRL+D.
- .yn(question) ⇒ Object
Class Method Details
._input_evaluator ⇒ Object
85 86 87 |
# File 'lib/inputs.rb', line 85 def self._input_evaluator @_input_evaluator ||= ->{ STDIN.gets } end |
._input_evaluator=(evaluator) ⇒ Object
81 82 83 |
# File 'lib/inputs.rb', line 81 def self._input_evaluator=(evaluator) @_input_evaluator = evaluator end |
._multi_line_input_evaluator ⇒ Object
93 94 95 |
# File 'lib/inputs.rb', line 93 def self._multi_line_input_evaluator @_multi_line_input_evaluator ||= ->(){ txt = ''; STDIN.each_line {|l| txt = txt + l }; txt } end |
._multi_line_input_evaluator=(evaluator) ⇒ Object
89 90 91 |
# File 'lib/inputs.rb', line 89 def self._multi_line_input_evaluator=(evaluator) @_multi_line_input_evaluator = evaluator end |
.name(question) ⇒ Object
Outputs a question and returns an input string. If no string is typed it returns empty string (“”)
21 22 23 24 25 26 27 |
# File 'lib/inputs.rb', line 21 def self.name(question) output question name = _input_evaluator.call name = name.chomp output name.green name end |
.name!(question) ⇒ Object
Outputs a question and returns an input string. If no string is typed it returns nil
30 31 32 33 34 35 36 37 38 |
# File 'lib/inputs.rb', line 30 def self.name!(question) output question name = _input_evaluator.call name = name.chomp if name != '' output name.green name end end |
.names(question) ⇒ Object
48 49 50 51 52 53 54 |
# File 'lib/inputs.rb', line 48 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
77 78 79 |
# File 'lib/inputs.rb', line 77 def self.output(txt) puts txt.green end |
.pick(options, question: "Please choose:", option_output_eval: ->(key, option) { " Press #{key} for \"#{option}\"" }) ⇒ Object
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/inputs.rb', line 56 def self.pick(, question: "Please choose:", option_output_eval: ->(key, option) { " Press #{key} for \"#{option}\"" }) option_map = .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 |
.text(question) ⇒ Object
Multiline input, exit via CTRL+D
41 42 43 44 45 46 |
# File 'lib/inputs.rb', line 41 def self.text(question) output question txt = _multi_line_input_evaluator.call output txt.green txt 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 |