Module: Inputs
- Defined in:
- lib/inputs.rb,
lib/inputs/version.rb
Constant Summary collapse
- VERSION =
"0.2.1"
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
- .password(prompt = 'Please Enter your password: ') ⇒ 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
90 91 92 |
# File 'lib/inputs.rb', line 90 def self._input_evaluator @_input_evaluator ||= ->{ STDIN.gets } end |
._input_evaluator=(evaluator) ⇒ Object
86 87 88 |
# File 'lib/inputs.rb', line 86 def self._input_evaluator=(evaluator) @_input_evaluator = evaluator end |
._multi_line_input_evaluator ⇒ Object
98 99 100 |
# File 'lib/inputs.rb', line 98 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
94 95 96 |
# File 'lib/inputs.rb', line 94 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 (“”)
22 23 24 25 26 27 28 |
# File 'lib/inputs.rb', line 22 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
31 32 33 34 35 36 37 38 39 |
# File 'lib/inputs.rb', line 31 def self.name!(question) output question name = _input_evaluator.call name = name.chomp if name != '' output name.green name end end |
.names(question) ⇒ Object
53 54 55 56 57 58 59 |
# File 'lib/inputs.rb', line 53 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
82 83 84 |
# File 'lib/inputs.rb', line 82 def self.output(txt) puts txt.green end |
.password(prompt = 'Please Enter your password: ') ⇒ Object
49 50 51 |
# File 'lib/inputs.rb', line 49 def self.password(prompt = 'Please Enter your password: ') IO::console.getpass(prompt) end |
.pick(options, question: "Please choose:", option_output_eval: ->(key, option) { " Press #{key} for \"#{option}\"" }) ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/inputs.rb', line 61 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
42 43 44 45 46 47 |
# File 'lib/inputs.rb', line 42 def self.text(question) output question txt = _multi_line_input_evaluator.call output txt.green txt end |
.yn(question) ⇒ Object
6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
# File 'lib/inputs.rb', line 6 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 |