Module: InputReader
- Defined in:
- lib/input_reader.rb,
lib/input_reader/builder.rb,
lib/input_reader/version.rb
Defined Under Namespace
Classes: Builder
Constant Summary collapse
- VERSION =
"0.2.0"
Class Method Summary collapse
- .boolean_false_values ⇒ Object
- .boolean_true_values ⇒ Object
- .confirmation_required(messages = '') ⇒ Object
- .get_and_parse_array(parsers, options = {}) ⇒ Object
- .get_and_parse_input(parsers, options = {}) ⇒ Object
- .get_array(options = {}) ⇒ Object
- .get_array_of_dates(options = {}) ⇒ Object
- .get_array_of_datetimes(options = {}) ⇒ Object
- .get_array_of_ints(options = {}) ⇒ Object
- .get_boolean(options = {}) ⇒ Object
- .get_date(options = {}) ⇒ Object
- .get_datetime(options = {}) ⇒ Object
- .get_input(options = {}) ⇒ Object
- .get_input_with_exception_handling(options = {}) ⇒ Object
- .get_int(options = {}) ⇒ Object
- .get_string(options = {}) ⇒ Object
- .prompt_choices(items, selection_attribute = nil) ⇒ Object
- .select_item(items, options = {}) ⇒ Object
- .select_items(items, options = {}) ⇒ Object
Class Method Details
.boolean_false_values ⇒ Object
11 12 13 |
# File 'lib/input_reader.rb', line 11 def boolean_false_values %w{n no f false 0} end |
.boolean_true_values ⇒ Object
7 8 9 |
# File 'lib/input_reader.rb', line 7 def boolean_true_values %w{y yes t true 1} end |
.confirmation_required(messages = '') ⇒ Object
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 |
# File 'lib/input_reader.rb', line 139 def confirmation_required( = '') puts '-' * 110 puts '[Messages]' puts puts '-' * 110 print 'Are you sure? (Y/N): ' case STDIN.gets.chomp.upcase when 'Y' puts yield puts '----------------------' puts 'Operation completed!!' puts '----------------------' when 'N' puts 'Operation aborted on user prompt' else puts 'Please enter a valid response. Operation aborted.' end end |
.get_and_parse_array(parsers, options = {}) ⇒ Object
71 72 73 74 |
# File 'lib/input_reader.rb', line 71 def get_and_parse_array(parsers, = {}) [:parsers] = Array([:parsers]) + Array(parsers) get_array() end |
.get_and_parse_input(parsers, options = {}) ⇒ Object
76 77 78 79 |
# File 'lib/input_reader.rb', line 76 def get_and_parse_input(parsers, = {}) [:parsers] = Array([:parsers]) + Array(parsers) self.get_input_with_exception_handling() end |
.get_array(options = {}) ⇒ Object
50 51 52 53 54 55 56 57 |
# File 'lib/input_reader.rb', line 50 def get_array( = {}) array = [] = .merge(:allow_blank => true) while input = self.get_input_with_exception_handling() array << input end array end |
.get_array_of_dates(options = {}) ⇒ Object
63 64 65 |
# File 'lib/input_reader.rb', line 63 def get_array_of_dates( = {}) self.get_and_parse_array(lambda { |d| Date.parse(d) }, ) end |
.get_array_of_datetimes(options = {}) ⇒ Object
67 68 69 |
# File 'lib/input_reader.rb', line 67 def get_array_of_datetimes( = {}) self.get_and_parse_array(lambda { |dt| DateTime.parse(dt) }, ) end |
.get_array_of_ints(options = {}) ⇒ Object
59 60 61 |
# File 'lib/input_reader.rb', line 59 def get_array_of_ints( = {}) self.get_and_parse_array(:to_i, ) end |
.get_boolean(options = {}) ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/input_reader.rb', line 23 def get_boolean( = {}) all_values = boolean_true_values + boolean_false_values [:validators] = [{:message => "Invalid input given. Valid values are #{all_values.join(', ')}", :validator => lambda { |input| all_values.include?(input.to_s.downcase) }}] [:prompt] ||= '(Y/N)?' input = self.get_input() if boolean_true_values.include?(input.to_s.downcase) true elsif boolean_false_values.include?(input.to_s.downcase) false else nil end end |
.get_date(options = {}) ⇒ Object
42 43 44 |
# File 'lib/input_reader.rb', line 42 def get_date( = {}) self.get_and_parse_input(lambda { |d| Date.parse(d) }, ) end |
.get_datetime(options = {}) ⇒ Object
46 47 48 |
# File 'lib/input_reader.rb', line 46 def get_datetime( = {}) self.get_and_parse_input(lambda { |dt| DateTime.parse(dt) }, ) end |
.get_input(options = {}) ⇒ Object
15 16 17 |
# File 'lib/input_reader.rb', line 15 def get_input( = {}) InputReader::Builder.new().get_input end |
.get_input_with_exception_handling(options = {}) ⇒ Object
81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/input_reader.rb', line 81 def get_input_with_exception_handling( = {}) valid_input = false while !valid_input begin input = self.get_input() valid_input = true rescue Interrupt raise rescue Exception => e puts e. end end input end |
.get_int(options = {}) ⇒ Object
38 39 40 |
# File 'lib/input_reader.rb', line 38 def get_int( = {}) self.get_and_parse_input(:to_i, ) end |
.get_string(options = {}) ⇒ Object
19 20 21 |
# File 'lib/input_reader.rb', line 19 def get_string( = {}) self.get_input_with_exception_handling() end |
.prompt_choices(items, selection_attribute = nil) ⇒ Object
96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/input_reader.rb', line 96 def prompt_choices(items, selection_attribute = nil) items.each.with_index do |item, i| option = if selection_attribute.is_a?(String) || selection_attribute.is_a?(Symbol) item.send(selection_attribute) elsif selection_attribute.is_a?(Proc) selection_attribute.call(item) else item end puts "#{i + 1}. #{option}" end end |
.select_item(items, options = {}) ⇒ Object
109 110 111 112 113 114 115 116 117 118 |
# File 'lib/input_reader.rb', line 109 def select_item(items, = {}) prompt_choices(items, [:selection_attribute]) input = get_int({ :valid_values => (1..items.size).to_a, :allow_blank => [:allow_blank], :prompt => [:prompt] || 'Choice: ' }) return nil if input.nil? items[input - 1] end |
.select_items(items, options = {}) ⇒ Object
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 |
# File 'lib/input_reader.rb', line 120 def select_items(items, = {}) prompt_choices(items, [:selection_attribute]) puts "#{items.size + 1}. All" input = get_input({ :parsers => [lambda { |input| choices = input.strip.gsub(/\s*,\s*/, ',').split(',').map(&:to_i) if choices.include?(items.size + 1) choices = (1..items.size).to_a end choices }], :validators => [lambda { |input| input.all? { |i| i > 0 && i <= items.size } }], :allow_blank => [:allow_blank], :prompt => [:prompt] || 'Choices (separate with comma): ' }) return [] if input.nil? input.map { |c| items[c - 1] } end |