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

Class Method Details

.boolean_false_valuesObject



11
12
13
# File 'lib/input_reader.rb', line 11

def boolean_false_values
  %w{n no f false 0}
end

.boolean_true_valuesObject



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(messages = '')
  puts '-' * 110
  puts '[Messages]'
  puts messages
  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, options = {})
  options[:parsers] = Array(options[:parsers]) + Array(parsers)
  get_array(options)
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, options = {})
  options[:parsers] = Array(options[:parsers]) + Array(parsers)
  self.get_input_with_exception_handling(options)
end

.get_array(options = {}) ⇒ Object



50
51
52
53
54
55
56
57
# File 'lib/input_reader.rb', line 50

def get_array(options = {})
  array         = []
  input_options = options.merge(:allow_blank => true)
  while input = self.get_input_with_exception_handling(input_options)
    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(options = {})
  self.get_and_parse_array(lambda { |d| Date.parse(d) }, options)
end

.get_array_of_datetimes(options = {}) ⇒ Object



67
68
69
# File 'lib/input_reader.rb', line 67

def get_array_of_datetimes(options = {})
  self.get_and_parse_array(lambda { |dt| DateTime.parse(dt) }, options)
end

.get_array_of_ints(options = {}) ⇒ Object



59
60
61
# File 'lib/input_reader.rb', line 59

def get_array_of_ints(options = {})
  self.get_and_parse_array(:to_i, options)
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(options = {})
  all_values           = boolean_true_values + boolean_false_values
  options[:validators] = [{:message   => "Invalid input given. Valid values are #{all_values.join(', ')}",
                           :validator => lambda { |input| all_values.include?(input.to_s.downcase) }}]
  options[:prompt] ||= '(Y/N)?'
  input = self.get_input(options)
  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(options = {})
  self.get_and_parse_input(lambda { |d| Date.parse(d) }, options)
end

.get_datetime(options = {}) ⇒ Object



46
47
48
# File 'lib/input_reader.rb', line 46

def get_datetime(options = {})
  self.get_and_parse_input(lambda { |dt| DateTime.parse(dt) }, options)
end

.get_input(options = {}) ⇒ Object



15
16
17
# File 'lib/input_reader.rb', line 15

def get_input(options = {})
  InputReader::Builder.new(options).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(options = {})
  valid_input = false
  while !valid_input
    begin
      input = self.get_input(options)
      valid_input = true
    rescue Interrupt
      raise
    rescue Exception => e
      puts e.message
    end
  end
  input
end

.get_int(options = {}) ⇒ Object



38
39
40
# File 'lib/input_reader.rb', line 38

def get_int(options = {})
  self.get_and_parse_input(:to_i, options)
end

.get_string(options = {}) ⇒ Object



19
20
21
# File 'lib/input_reader.rb', line 19

def get_string(options = {})
  self.get_input_with_exception_handling(options)
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, options = {})
  prompt_choices(items, options[:selection_attribute])
  input = get_int({
                    :valid_values => (1..items.size).to_a,
                    :allow_blank  => options[:allow_blank],
                    :prompt       => options[: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, options = {})
  prompt_choices(items, options[: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 => options[:allow_blank],
                      :prompt      => options[:prompt] || 'Choices (separate with comma): '
                    })
  return [] if input.nil?
  input.map { |c| items[c - 1] }
end