Module: InputReader
- Defined in:
- lib/input_reader.rb,
lib/input_reader/version.rb,
lib/input_reader/input_reader.rb
Defined Under Namespace
Classes: Builder
Constant Summary collapse
- VERSION =
"0.0.1"
Class Method Summary collapse
- .confirmation_required(messages = '') ⇒ Object
- .get_and_parse_array(parsers, options = nil) ⇒ Object
- .get_and_parse_input(parsers, options = nil) ⇒ Object
- .get_array(options = nil) ⇒ Object
- .get_array_of_dates(options = nil) ⇒ Object
- .get_array_of_datetimes(options = nil) ⇒ Object
- .get_array_of_ints(options = nil) ⇒ Object
- .get_boolean(options = nil) ⇒ Object
- .get_date(options = nil) ⇒ Object
- .get_datetime(options = nil) ⇒ Object
- .get_input(options = nil) ⇒ Object
- .get_input_with_exception_handling(options = nil) ⇒ Object
- .get_int(options = nil) ⇒ Object
- .get_string(options = nil) ⇒ Object
- .select_item(items, options = nil) ⇒ Object
- .select_items(items, options = nil) ⇒ Object
Class Method Details
.confirmation_required(messages = '') ⇒ Object
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 |
# File 'lib/input_reader.rb', line 133 def confirmation_required( = '') puts "" puts "-" * 110 puts "[Messages]" puts puts "-" * 110 puts "" print "Are you sure you want to do this? (Y/N): " user_answer = STDIN.gets.chomp.upcase if user_answer != 'Y' && user_answer != 'N' puts "Please enter a valid response. Operation aborted. #{user_answer}" else if user_answer == 'Y' puts yield puts "----------------------" puts "Operation completed!!" puts "----------------------" else puts "Operation aborted on user prompt" end end end |
.get_and_parse_array(parsers, options = nil) ⇒ Object
71 72 73 74 75 |
# File 'lib/input_reader.rb', line 71 def get_and_parse_array(parsers, = nil) ||= {} [:parsers] = Array.wrap([:parsers]) + Array.wrap(parsers) get_array() end |
.get_and_parse_input(parsers, options = nil) ⇒ Object
78 79 80 81 82 |
# File 'lib/input_reader.rb', line 78 def get_and_parse_input(parsers, = nil) ||= {} [:parsers] = Array.wrap([:parsers]) + Array.wrap(parsers) self.get_input_with_exception_handling() end |
.get_array(options = nil) ⇒ Object
45 46 47 48 49 50 51 52 53 |
# File 'lib/input_reader.rb', line 45 def get_array( = nil) ||= {} array = [] = .merge(:allow_blank => true) while input = self.get_input_with_exception_handling() array << input end array end |
.get_array_of_dates(options = nil) ⇒ Object
61 62 63 |
# File 'lib/input_reader.rb', line 61 def get_array_of_dates( = nil) self.get_and_parse_array(:to_date, ) end |
.get_array_of_datetimes(options = nil) ⇒ Object
66 67 68 |
# File 'lib/input_reader.rb', line 66 def get_array_of_datetimes( = nil) self.get_and_parse_array(:to_datetime, ) end |
.get_array_of_ints(options = nil) ⇒ Object
56 57 58 |
# File 'lib/input_reader.rb', line 56 def get_array_of_ints( = nil) self.get_and_parse_array(:to_i, ) end |
.get_boolean(options = nil) ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/input_reader.rb', line 17 def get_boolean( = nil) ||= {} true_values = %w{y t true 1} false_values = %w{n f false 0} all_values = true_values + 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() true_values.include?(input.to_s.downcase) end |
.get_date(options = nil) ⇒ Object
35 36 37 |
# File 'lib/input_reader.rb', line 35 def get_date( = nil) self.get_and_parse_input(:to_date, ) end |
.get_datetime(options = nil) ⇒ Object
40 41 42 |
# File 'lib/input_reader.rb', line 40 def get_datetime( = nil) self.get_and_parse_input(:to_datetime, ) end |
.get_input(options = nil) ⇒ Object
7 8 9 |
# File 'lib/input_reader.rb', line 7 def get_input( = nil) InputReader::Builder.new().get_input end |
.get_input_with_exception_handling(options = nil) ⇒ Object
85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/input_reader.rb', line 85 def get_input_with_exception_handling( = nil) ||= {} valid_input = false while !valid_input begin input = self.get_input() valid_input = true rescue Exception => e raise e if e.is_a?(Interrupt) puts e. end end input end |
.get_int(options = nil) ⇒ Object
30 31 32 |
# File 'lib/input_reader.rb', line 30 def get_int( = nil) self.get_and_parse_input(:to_i, ) end |
.get_string(options = nil) ⇒ Object
12 13 14 |
# File 'lib/input_reader.rb', line 12 def get_string( = nil) self.get_input_with_exception_handling() end |
.select_item(items, options = nil) ⇒ Object
101 102 103 104 105 106 107 108 109 110 |
# File 'lib/input_reader.rb', line 101 def select_item(items, = nil) ||= {} prompt_choices(items, [:selection_attribute]) input = get_int({ :valid_values => (1..items.size).to_a, :allow_blank => [:allow_blank], :prompt => [:prompt] || "Choice: " }) input && items[input - 1] end |
.select_items(items, options = nil) ⇒ Object
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 |
# File 'lib/input_reader.rb', line 113 def select_items(items, = nil) ||= {} 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): " }) input && input.map { |c| items[c - 1] } end |