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

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



61
62
63
# File 'lib/input_reader.rb', line 61

def get_array_of_dates(options = nil)
  self.get_and_parse_array(:to_date, options)
end

.get_array_of_datetimes(options = nil) ⇒ Object



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

def get_array_of_datetimes(options = nil)
  self.get_and_parse_array(:to_datetime, options)
end

.get_array_of_ints(options = nil) ⇒ Object



56
57
58
# File 'lib/input_reader.rb', line 56

def get_array_of_ints(options = nil)
  self.get_and_parse_array(:to_i, options)
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(options = nil)
  options ||= {}
  true_values = %w{y t true 1}
  false_values = %w{n f false 0}
  all_values = true_values + 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)
  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(options = nil)
  self.get_and_parse_input(:to_date, options)
end

.get_datetime(options = nil) ⇒ Object



40
41
42
# File 'lib/input_reader.rb', line 40

def get_datetime(options = nil)
  self.get_and_parse_input(:to_datetime, options)
end

.get_input(options = nil) ⇒ Object



7
8
9
# File 'lib/input_reader.rb', line 7

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

.get_int(options = nil) ⇒ Object



30
31
32
# File 'lib/input_reader.rb', line 30

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

.get_string(options = nil) ⇒ Object



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

def get_string(options = nil)
  self.get_input_with_exception_handling(options)
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, options = nil)
  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: "
  })
  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, options = nil)
  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): "
  })
  input && input.map { |c| items[c - 1] }
end