input_reader 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.gitignore +17 -0
 - data/Gemfile +4 -0
 - data/LICENSE +22 -0
 - data/README.md +29 -0
 - data/Rakefile +2 -0
 - data/input_reader.gemspec +17 -0
 - data/lib/input_reader.rb +159 -0
 - data/lib/input_reader/input_reader.rb +140 -0
 - data/lib/input_reader/version.rb +3 -0
 - metadata +54 -0
 
    
        data/.gitignore
    ADDED
    
    
    
        data/Gemfile
    ADDED
    
    
    
        data/LICENSE
    ADDED
    
    | 
         @@ -0,0 +1,22 @@ 
     | 
|
| 
      
 1 
     | 
    
         
            +
            Copyright (c) 2012 Michael Noack
         
     | 
| 
      
 2 
     | 
    
         
            +
             
     | 
| 
      
 3 
     | 
    
         
            +
            MIT License
         
     | 
| 
      
 4 
     | 
    
         
            +
             
     | 
| 
      
 5 
     | 
    
         
            +
            Permission is hereby granted, free of charge, to any person obtaining
         
     | 
| 
      
 6 
     | 
    
         
            +
            a copy of this software and associated documentation files (the
         
     | 
| 
      
 7 
     | 
    
         
            +
            "Software"), to deal in the Software without restriction, including
         
     | 
| 
      
 8 
     | 
    
         
            +
            without limitation the rights to use, copy, modify, merge, publish,
         
     | 
| 
      
 9 
     | 
    
         
            +
            distribute, sublicense, and/or sell copies of the Software, and to
         
     | 
| 
      
 10 
     | 
    
         
            +
            permit persons to whom the Software is furnished to do so, subject to
         
     | 
| 
      
 11 
     | 
    
         
            +
            the following conditions:
         
     | 
| 
      
 12 
     | 
    
         
            +
             
     | 
| 
      
 13 
     | 
    
         
            +
            The above copyright notice and this permission notice shall be
         
     | 
| 
      
 14 
     | 
    
         
            +
            included in all copies or substantial portions of the Software.
         
     | 
| 
      
 15 
     | 
    
         
            +
             
     | 
| 
      
 16 
     | 
    
         
            +
            THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
         
     | 
| 
      
 17 
     | 
    
         
            +
            EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
         
     | 
| 
      
 18 
     | 
    
         
            +
            MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
         
     | 
| 
      
 19 
     | 
    
         
            +
            NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
         
     | 
| 
      
 20 
     | 
    
         
            +
            LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
         
     | 
| 
      
 21 
     | 
    
         
            +
            OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
         
     | 
| 
      
 22 
     | 
    
         
            +
            WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
         
     | 
    
        data/README.md
    ADDED
    
    | 
         @@ -0,0 +1,29 @@ 
     | 
|
| 
      
 1 
     | 
    
         
            +
            # InputReader
         
     | 
| 
      
 2 
     | 
    
         
            +
             
     | 
| 
      
 3 
     | 
    
         
            +
            TODO: Write a gem description
         
     | 
| 
      
 4 
     | 
    
         
            +
             
     | 
| 
      
 5 
     | 
    
         
            +
            ## Installation
         
     | 
| 
      
 6 
     | 
    
         
            +
             
     | 
| 
      
 7 
     | 
    
         
            +
            Add this line to your application's Gemfile:
         
     | 
| 
      
 8 
     | 
    
         
            +
             
     | 
| 
      
 9 
     | 
    
         
            +
                gem 'input_reader'
         
     | 
| 
      
 10 
     | 
    
         
            +
             
     | 
| 
      
 11 
     | 
    
         
            +
            And then execute:
         
     | 
| 
      
 12 
     | 
    
         
            +
             
     | 
| 
      
 13 
     | 
    
         
            +
                $ bundle
         
     | 
| 
      
 14 
     | 
    
         
            +
             
     | 
| 
      
 15 
     | 
    
         
            +
            Or install it yourself as:
         
     | 
| 
      
 16 
     | 
    
         
            +
             
     | 
| 
      
 17 
     | 
    
         
            +
                $ gem install input_reader
         
     | 
| 
      
 18 
     | 
    
         
            +
             
     | 
| 
      
 19 
     | 
    
         
            +
            ## Usage
         
     | 
| 
      
 20 
     | 
    
         
            +
             
     | 
| 
      
 21 
     | 
    
         
            +
            TODO: Write usage instructions here
         
     | 
| 
      
 22 
     | 
    
         
            +
             
     | 
| 
      
 23 
     | 
    
         
            +
            ## Contributing
         
     | 
| 
      
 24 
     | 
    
         
            +
             
     | 
| 
      
 25 
     | 
    
         
            +
            1. Fork it
         
     | 
| 
      
 26 
     | 
    
         
            +
            2. Create your feature branch (`git checkout -b my-new-feature`)
         
     | 
| 
      
 27 
     | 
    
         
            +
            3. Commit your changes (`git commit -am 'Added some feature'`)
         
     | 
| 
      
 28 
     | 
    
         
            +
            4. Push to the branch (`git push origin my-new-feature`)
         
     | 
| 
      
 29 
     | 
    
         
            +
            5. Create new Pull Request
         
     | 
    
        data/Rakefile
    ADDED
    
    
| 
         @@ -0,0 +1,17 @@ 
     | 
|
| 
      
 1 
     | 
    
         
            +
            # -*- encoding: utf-8 -*-
         
     | 
| 
      
 2 
     | 
    
         
            +
            require File.expand_path('../lib/input_reader/version', __FILE__)
         
     | 
| 
      
 3 
     | 
    
         
            +
             
     | 
| 
      
 4 
     | 
    
         
            +
            Gem::Specification.new do |gem|
         
     | 
| 
      
 5 
     | 
    
         
            +
              gem.authors       = ["Alessandro Berardi, Michael Noack"]
         
     | 
| 
      
 6 
     | 
    
         
            +
              gem.email         = ["support@travellink.com.au"]
         
     | 
| 
      
 7 
     | 
    
         
            +
              gem.description   = %q{Command line helpers to read input of various types, confirm, etc.}
         
     | 
| 
      
 8 
     | 
    
         
            +
              gem.summary       = %q{Command line helpers to read input, etc.}
         
     | 
| 
      
 9 
     | 
    
         
            +
              gem.homepage      = ""
         
     | 
| 
      
 10 
     | 
    
         
            +
             
     | 
| 
      
 11 
     | 
    
         
            +
              gem.files         = `git ls-files`.split($\)
         
     | 
| 
      
 12 
     | 
    
         
            +
              gem.executables   = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
         
     | 
| 
      
 13 
     | 
    
         
            +
              gem.test_files    = gem.files.grep(%r{^(test|spec|features)/})
         
     | 
| 
      
 14 
     | 
    
         
            +
              gem.name          = "input_reader"
         
     | 
| 
      
 15 
     | 
    
         
            +
              gem.require_paths = ["lib"]
         
     | 
| 
      
 16 
     | 
    
         
            +
              gem.version       = InputReader::VERSION
         
     | 
| 
      
 17 
     | 
    
         
            +
            end
         
     | 
    
        data/lib/input_reader.rb
    ADDED
    
    | 
         @@ -0,0 +1,159 @@ 
     | 
|
| 
      
 1 
     | 
    
         
            +
            require "input_reader/version"
         
     | 
| 
      
 2 
     | 
    
         
            +
             
     | 
| 
      
 3 
     | 
    
         
            +
            module InputReader
         
     | 
| 
      
 4 
     | 
    
         
            +
              require 'input_reader/input_reader'
         
     | 
| 
      
 5 
     | 
    
         
            +
             
     | 
| 
      
 6 
     | 
    
         
            +
              class << self
         
     | 
| 
      
 7 
     | 
    
         
            +
                def get_input(options = nil)
         
     | 
| 
      
 8 
     | 
    
         
            +
                  InputReader::Builder.new(options).get_input
         
     | 
| 
      
 9 
     | 
    
         
            +
                end
         
     | 
| 
      
 10 
     | 
    
         
            +
             
     | 
| 
      
 11 
     | 
    
         
            +
             
     | 
| 
      
 12 
     | 
    
         
            +
                def get_string(options = nil)
         
     | 
| 
      
 13 
     | 
    
         
            +
                  self.get_input_with_exception_handling(options)
         
     | 
| 
      
 14 
     | 
    
         
            +
                end
         
     | 
| 
      
 15 
     | 
    
         
            +
             
     | 
| 
      
 16 
     | 
    
         
            +
             
     | 
| 
      
 17 
     | 
    
         
            +
                def get_boolean(options = nil)
         
     | 
| 
      
 18 
     | 
    
         
            +
                  options ||= {}
         
     | 
| 
      
 19 
     | 
    
         
            +
                  true_values = %w{y t true 1}
         
     | 
| 
      
 20 
     | 
    
         
            +
                  false_values = %w{n f false 0}
         
     | 
| 
      
 21 
     | 
    
         
            +
                  all_values = true_values + false_values
         
     | 
| 
      
 22 
     | 
    
         
            +
                  options[:validators] = [{:message => "Invalid input given. Valid values are #{all_values.join(', ')}",
         
     | 
| 
      
 23 
     | 
    
         
            +
                      :validator => lambda { |input| all_values.include?(input.to_s.downcase) } }]
         
     | 
| 
      
 24 
     | 
    
         
            +
                  options[:prompt] ||= "(Y/N)?"
         
     | 
| 
      
 25 
     | 
    
         
            +
                  input = self.get_input(options)
         
     | 
| 
      
 26 
     | 
    
         
            +
                  true_values.include?(input.to_s.downcase)
         
     | 
| 
      
 27 
     | 
    
         
            +
                end
         
     | 
| 
      
 28 
     | 
    
         
            +
             
     | 
| 
      
 29 
     | 
    
         
            +
             
     | 
| 
      
 30 
     | 
    
         
            +
                def get_int(options = nil)
         
     | 
| 
      
 31 
     | 
    
         
            +
                  self.get_and_parse_input(:to_i, options)
         
     | 
| 
      
 32 
     | 
    
         
            +
                end
         
     | 
| 
      
 33 
     | 
    
         
            +
             
     | 
| 
      
 34 
     | 
    
         
            +
             
     | 
| 
      
 35 
     | 
    
         
            +
                def get_date(options = nil)
         
     | 
| 
      
 36 
     | 
    
         
            +
                  self.get_and_parse_input(:to_date, options)
         
     | 
| 
      
 37 
     | 
    
         
            +
                end
         
     | 
| 
      
 38 
     | 
    
         
            +
             
     | 
| 
      
 39 
     | 
    
         
            +
             
     | 
| 
      
 40 
     | 
    
         
            +
                def get_datetime(options = nil)
         
     | 
| 
      
 41 
     | 
    
         
            +
                  self.get_and_parse_input(:to_datetime, options)
         
     | 
| 
      
 42 
     | 
    
         
            +
                end
         
     | 
| 
      
 43 
     | 
    
         
            +
             
     | 
| 
      
 44 
     | 
    
         
            +
             
     | 
| 
      
 45 
     | 
    
         
            +
                def get_array(options = nil)
         
     | 
| 
      
 46 
     | 
    
         
            +
                  options ||= {}
         
     | 
| 
      
 47 
     | 
    
         
            +
                  array = []
         
     | 
| 
      
 48 
     | 
    
         
            +
                  input_options = options.merge(:allow_blank => true)
         
     | 
| 
      
 49 
     | 
    
         
            +
                  while input = self.get_input_with_exception_handling(input_options)
         
     | 
| 
      
 50 
     | 
    
         
            +
                    array << input
         
     | 
| 
      
 51 
     | 
    
         
            +
                  end
         
     | 
| 
      
 52 
     | 
    
         
            +
                  array
         
     | 
| 
      
 53 
     | 
    
         
            +
                end
         
     | 
| 
      
 54 
     | 
    
         
            +
             
     | 
| 
      
 55 
     | 
    
         
            +
             
     | 
| 
      
 56 
     | 
    
         
            +
                def get_array_of_ints(options = nil)
         
     | 
| 
      
 57 
     | 
    
         
            +
                  self.get_and_parse_array(:to_i, options)
         
     | 
| 
      
 58 
     | 
    
         
            +
                end
         
     | 
| 
      
 59 
     | 
    
         
            +
             
     | 
| 
      
 60 
     | 
    
         
            +
             
     | 
| 
      
 61 
     | 
    
         
            +
                def get_array_of_dates(options = nil)
         
     | 
| 
      
 62 
     | 
    
         
            +
                  self.get_and_parse_array(:to_date, options)
         
     | 
| 
      
 63 
     | 
    
         
            +
                end
         
     | 
| 
      
 64 
     | 
    
         
            +
             
     | 
| 
      
 65 
     | 
    
         
            +
             
     | 
| 
      
 66 
     | 
    
         
            +
                def get_array_of_datetimes(options = nil)
         
     | 
| 
      
 67 
     | 
    
         
            +
                  self.get_and_parse_array(:to_datetime, options)
         
     | 
| 
      
 68 
     | 
    
         
            +
                end
         
     | 
| 
      
 69 
     | 
    
         
            +
             
     | 
| 
      
 70 
     | 
    
         
            +
             
     | 
| 
      
 71 
     | 
    
         
            +
                def get_and_parse_array(parsers, options = nil)
         
     | 
| 
      
 72 
     | 
    
         
            +
                  options ||= {}
         
     | 
| 
      
 73 
     | 
    
         
            +
                  options[:parsers] = Array.wrap(options[:parsers]) + Array.wrap(parsers)
         
     | 
| 
      
 74 
     | 
    
         
            +
                  get_array(options)
         
     | 
| 
      
 75 
     | 
    
         
            +
                end
         
     | 
| 
      
 76 
     | 
    
         
            +
             
     | 
| 
      
 77 
     | 
    
         
            +
             
     | 
| 
      
 78 
     | 
    
         
            +
                def get_and_parse_input(parsers, options = nil)
         
     | 
| 
      
 79 
     | 
    
         
            +
                  options ||= {}
         
     | 
| 
      
 80 
     | 
    
         
            +
                  options[:parsers] = Array.wrap(options[:parsers]) + Array.wrap(parsers)
         
     | 
| 
      
 81 
     | 
    
         
            +
                  self.get_input_with_exception_handling(options)
         
     | 
| 
      
 82 
     | 
    
         
            +
                end
         
     | 
| 
      
 83 
     | 
    
         
            +
             
     | 
| 
      
 84 
     | 
    
         
            +
             
     | 
| 
      
 85 
     | 
    
         
            +
                def get_input_with_exception_handling(options = nil)
         
     | 
| 
      
 86 
     | 
    
         
            +
                  options ||= {}
         
     | 
| 
      
 87 
     | 
    
         
            +
                  valid_input = false
         
     | 
| 
      
 88 
     | 
    
         
            +
                  while !valid_input
         
     | 
| 
      
 89 
     | 
    
         
            +
                    begin
         
     | 
| 
      
 90 
     | 
    
         
            +
                      input = self.get_input(options)
         
     | 
| 
      
 91 
     | 
    
         
            +
                      valid_input = true
         
     | 
| 
      
 92 
     | 
    
         
            +
                    rescue Exception => e
         
     | 
| 
      
 93 
     | 
    
         
            +
                      raise e if e.is_a?(Interrupt)
         
     | 
| 
      
 94 
     | 
    
         
            +
                      puts e.message
         
     | 
| 
      
 95 
     | 
    
         
            +
                    end
         
     | 
| 
      
 96 
     | 
    
         
            +
                  end
         
     | 
| 
      
 97 
     | 
    
         
            +
                  input
         
     | 
| 
      
 98 
     | 
    
         
            +
                end
         
     | 
| 
      
 99 
     | 
    
         
            +
             
     | 
| 
      
 100 
     | 
    
         
            +
             
     | 
| 
      
 101 
     | 
    
         
            +
                def select_item(items, options = nil)
         
     | 
| 
      
 102 
     | 
    
         
            +
                  options ||= {}
         
     | 
| 
      
 103 
     | 
    
         
            +
                  prompt_choices(items, options[:selection_attribute])
         
     | 
| 
      
 104 
     | 
    
         
            +
                  input = get_int({
         
     | 
| 
      
 105 
     | 
    
         
            +
                    :valid_values => (1..items.size).to_a,
         
     | 
| 
      
 106 
     | 
    
         
            +
                    :allow_blank => options[:allow_blank],
         
     | 
| 
      
 107 
     | 
    
         
            +
                    :prompt => options[:prompt] || "Choice: "
         
     | 
| 
      
 108 
     | 
    
         
            +
                  })
         
     | 
| 
      
 109 
     | 
    
         
            +
                  input && items[input - 1]
         
     | 
| 
      
 110 
     | 
    
         
            +
                end
         
     | 
| 
      
 111 
     | 
    
         
            +
             
     | 
| 
      
 112 
     | 
    
         
            +
             
     | 
| 
      
 113 
     | 
    
         
            +
                def select_items(items, options = nil)
         
     | 
| 
      
 114 
     | 
    
         
            +
                  options ||= {}
         
     | 
| 
      
 115 
     | 
    
         
            +
                  prompt_choices(items, options[:selection_attribute])
         
     | 
| 
      
 116 
     | 
    
         
            +
                  puts "#{items.size + 1}. All"
         
     | 
| 
      
 117 
     | 
    
         
            +
                  input = get_input({
         
     | 
| 
      
 118 
     | 
    
         
            +
                    :parsers => [lambda { |input|
         
     | 
| 
      
 119 
     | 
    
         
            +
                        choices = input.strip.gsub(/\s*,\s*/, ",").split(',').map(&:to_i)
         
     | 
| 
      
 120 
     | 
    
         
            +
                        if choices.include?(items.size + 1)
         
     | 
| 
      
 121 
     | 
    
         
            +
                          choices = (1..items.size).to_a
         
     | 
| 
      
 122 
     | 
    
         
            +
                        end
         
     | 
| 
      
 123 
     | 
    
         
            +
                        choices
         
     | 
| 
      
 124 
     | 
    
         
            +
                      }],
         
     | 
| 
      
 125 
     | 
    
         
            +
                    :validators => [lambda { |input| input.all? { |i| i > 0 && i <= items.size} }],
         
     | 
| 
      
 126 
     | 
    
         
            +
                    :allow_blank => options[:allow_blank],
         
     | 
| 
      
 127 
     | 
    
         
            +
                    :prompt => options[:prompt] || "Choices (separate with comma): "
         
     | 
| 
      
 128 
     | 
    
         
            +
                  })
         
     | 
| 
      
 129 
     | 
    
         
            +
                  input && input.map { |c| items[c - 1] }
         
     | 
| 
      
 130 
     | 
    
         
            +
                end
         
     | 
| 
      
 131 
     | 
    
         
            +
             
     | 
| 
      
 132 
     | 
    
         
            +
             
     | 
| 
      
 133 
     | 
    
         
            +
                def confirmation_required(messages = '')
         
     | 
| 
      
 134 
     | 
    
         
            +
                  puts ""
         
     | 
| 
      
 135 
     | 
    
         
            +
                  puts "-" * 110
         
     | 
| 
      
 136 
     | 
    
         
            +
                  puts "[Messages]"
         
     | 
| 
      
 137 
     | 
    
         
            +
                  puts messages
         
     | 
| 
      
 138 
     | 
    
         
            +
                  puts "-" * 110
         
     | 
| 
      
 139 
     | 
    
         
            +
                  puts ""
         
     | 
| 
      
 140 
     | 
    
         
            +
                  print "Are you sure you want to do this? (Y/N): "
         
     | 
| 
      
 141 
     | 
    
         
            +
                  user_answer = STDIN.gets.chomp.upcase
         
     | 
| 
      
 142 
     | 
    
         
            +
             
     | 
| 
      
 143 
     | 
    
         
            +
                  if user_answer != 'Y' && user_answer != 'N'
         
     | 
| 
      
 144 
     | 
    
         
            +
                    puts "Please enter a valid response. Operation aborted. #{user_answer}"
         
     | 
| 
      
 145 
     | 
    
         
            +
                  else
         
     | 
| 
      
 146 
     | 
    
         
            +
                    if user_answer == 'Y'
         
     | 
| 
      
 147 
     | 
    
         
            +
                      puts
         
     | 
| 
      
 148 
     | 
    
         
            +
                      yield
         
     | 
| 
      
 149 
     | 
    
         
            +
             
     | 
| 
      
 150 
     | 
    
         
            +
                      puts "----------------------"
         
     | 
| 
      
 151 
     | 
    
         
            +
                      puts "Operation completed!!"
         
     | 
| 
      
 152 
     | 
    
         
            +
                      puts "----------------------"
         
     | 
| 
      
 153 
     | 
    
         
            +
                    else
         
     | 
| 
      
 154 
     | 
    
         
            +
                      puts "Operation aborted on user prompt"
         
     | 
| 
      
 155 
     | 
    
         
            +
                    end
         
     | 
| 
      
 156 
     | 
    
         
            +
                  end
         
     | 
| 
      
 157 
     | 
    
         
            +
                end
         
     | 
| 
      
 158 
     | 
    
         
            +
              end
         
     | 
| 
      
 159 
     | 
    
         
            +
            end
         
     | 
| 
         @@ -0,0 +1,140 @@ 
     | 
|
| 
      
 1 
     | 
    
         
            +
            module InputReader
         
     | 
| 
      
 2 
     | 
    
         
            +
            class Builder
         
     | 
| 
      
 3 
     | 
    
         
            +
             
     | 
| 
      
 4 
     | 
    
         
            +
              attr_reader :input
         
     | 
| 
      
 5 
     | 
    
         
            +
             
     | 
| 
      
 6 
     | 
    
         
            +
              def initialize(options = nil)
         
     | 
| 
      
 7 
     | 
    
         
            +
                options ||= {}
         
     | 
| 
      
 8 
     | 
    
         
            +
                @prompt = options[:prompt]
         
     | 
| 
      
 9 
     | 
    
         
            +
                @allow_blank = options[:allow_blank]
         
     | 
| 
      
 10 
     | 
    
         
            +
                @default_value = options[:default_value]
         
     | 
| 
      
 11 
     | 
    
         
            +
                @valid_values = Array.wrap(options[:valid_values])
         
     | 
| 
      
 12 
     | 
    
         
            +
                @validators = Array.wrap(options[:validators])
         
     | 
| 
      
 13 
     | 
    
         
            +
                @pre_validators = Array.wrap(options[:pre_validators])
         
     | 
| 
      
 14 
     | 
    
         
            +
                @post_validators = Array.wrap(options[:post_validators])
         
     | 
| 
      
 15 
     | 
    
         
            +
                @parsers = Array.wrap(options[:parsers])
         
     | 
| 
      
 16 
     | 
    
         
            +
              end
         
     | 
| 
      
 17 
     | 
    
         
            +
             
     | 
| 
      
 18 
     | 
    
         
            +
             
     | 
| 
      
 19 
     | 
    
         
            +
              def get_input
         
     | 
| 
      
 20 
     | 
    
         
            +
                begin
         
     | 
| 
      
 21 
     | 
    
         
            +
                  print "#{@prompt} "
         
     | 
| 
      
 22 
     | 
    
         
            +
                  flush
         
     | 
| 
      
 23 
     | 
    
         
            +
                rescue Exception => e
         
     | 
| 
      
 24 
     | 
    
         
            +
                  puts e.message + e.class.inspect
         
     | 
| 
      
 25 
     | 
    
         
            +
                  exit
         
     | 
| 
      
 26 
     | 
    
         
            +
                end while !valid_input?
         
     | 
| 
      
 27 
     | 
    
         
            +
                @input
         
     | 
| 
      
 28 
     | 
    
         
            +
              end
         
     | 
| 
      
 29 
     | 
    
         
            +
             
     | 
| 
      
 30 
     | 
    
         
            +
             
     | 
| 
      
 31 
     | 
    
         
            +
              private
         
     | 
| 
      
 32 
     | 
    
         
            +
             
     | 
| 
      
 33 
     | 
    
         
            +
             
     | 
| 
      
 34 
     | 
    
         
            +
              def read
         
     | 
| 
      
 35 
     | 
    
         
            +
                $stdin.gets.chomp
         
     | 
| 
      
 36 
     | 
    
         
            +
              end
         
     | 
| 
      
 37 
     | 
    
         
            +
             
     | 
| 
      
 38 
     | 
    
         
            +
             
     | 
| 
      
 39 
     | 
    
         
            +
              def flush
         
     | 
| 
      
 40 
     | 
    
         
            +
                $stdout.flush
         
     | 
| 
      
 41 
     | 
    
         
            +
              end
         
     | 
| 
      
 42 
     | 
    
         
            +
             
     | 
| 
      
 43 
     | 
    
         
            +
             
     | 
| 
      
 44 
     | 
    
         
            +
              def self.prompt_choices(items, selection_attribute = nil)
         
     | 
| 
      
 45 
     | 
    
         
            +
                items.each.with_index do |item,i|
         
     | 
| 
      
 46 
     | 
    
         
            +
                  option = if selection_attribute.is_a?(String) || selection_attribute.is_a?(Symbol)
         
     | 
| 
      
 47 
     | 
    
         
            +
                    item.send(selection_attribute)
         
     | 
| 
      
 48 
     | 
    
         
            +
                  elsif selection_attribute.is_a?(Proc)
         
     | 
| 
      
 49 
     | 
    
         
            +
                    selection_attribute.call(item)
         
     | 
| 
      
 50 
     | 
    
         
            +
                  else
         
     | 
| 
      
 51 
     | 
    
         
            +
                    item
         
     | 
| 
      
 52 
     | 
    
         
            +
                  end
         
     | 
| 
      
 53 
     | 
    
         
            +
                  puts "#{i + 1}. #{option}"
         
     | 
| 
      
 54 
     | 
    
         
            +
                end
         
     | 
| 
      
 55 
     | 
    
         
            +
              end
         
     | 
| 
      
 56 
     | 
    
         
            +
              
         
     | 
| 
      
 57 
     | 
    
         
            +
              
         
     | 
| 
      
 58 
     | 
    
         
            +
              def valid_input?
         
     | 
| 
      
 59 
     | 
    
         
            +
                @input = read
         
     | 
| 
      
 60 
     | 
    
         
            +
                @input = @default_value if @input.blank?
         
     | 
| 
      
 61 
     | 
    
         
            +
                pre_validate(@input) && (@input.blank? || post_validate(@input = parse(@input)))
         
     | 
| 
      
 62 
     | 
    
         
            +
              end
         
     | 
| 
      
 63 
     | 
    
         
            +
             
     | 
| 
      
 64 
     | 
    
         
            +
             
     | 
| 
      
 65 
     | 
    
         
            +
              def pre_validate(input)
         
     | 
| 
      
 66 
     | 
    
         
            +
                pre_validators = [
         
     | 
| 
      
 67 
     | 
    
         
            +
                  {
         
     | 
| 
      
 68 
     | 
    
         
            +
                    :validator => lambda { |input| @allow_blank || !input.blank? },
         
     | 
| 
      
 69 
     | 
    
         
            +
                    :message => "No input given"
         
     | 
| 
      
 70 
     | 
    
         
            +
                  },
         
     | 
| 
      
 71 
     | 
    
         
            +
                ] + @pre_validators
         
     | 
| 
      
 72 
     | 
    
         
            +
                validate(input, pre_validators)
         
     | 
| 
      
 73 
     | 
    
         
            +
              end
         
     | 
| 
      
 74 
     | 
    
         
            +
             
     | 
| 
      
 75 
     | 
    
         
            +
             
     | 
| 
      
 76 
     | 
    
         
            +
              def post_validate(input)
         
     | 
| 
      
 77 
     | 
    
         
            +
                post_validators = [
         
     | 
| 
      
 78 
     | 
    
         
            +
                  {
         
     | 
| 
      
 79 
     | 
    
         
            +
                    :validator => lambda { |input| @valid_values.blank? || @valid_values.include?(input) },
         
     | 
| 
      
 80 
     | 
    
         
            +
                    :message => "Invalid input given. Valid values are #{@valid_values.join(', ')}"
         
     | 
| 
      
 81 
     | 
    
         
            +
                  }
         
     | 
| 
      
 82 
     | 
    
         
            +
                ] + @post_validators + @validators
         
     | 
| 
      
 83 
     | 
    
         
            +
                validate(input, post_validators)
         
     | 
| 
      
 84 
     | 
    
         
            +
              end
         
     | 
| 
      
 85 
     | 
    
         
            +
             
     | 
| 
      
 86 
     | 
    
         
            +
             
     | 
| 
      
 87 
     | 
    
         
            +
              def validate(input,validators)
         
     | 
| 
      
 88 
     | 
    
         
            +
             
     | 
| 
      
 89 
     | 
    
         
            +
                validators.each do |validator|
         
     | 
| 
      
 90 
     | 
    
         
            +
             
     | 
| 
      
 91 
     | 
    
         
            +
                  validator_proc = if validator.is_a?(Proc)
         
     | 
| 
      
 92 
     | 
    
         
            +
                    validator
         
     | 
| 
      
 93 
     | 
    
         
            +
                  elsif validator.is_a?(Hash)
         
     | 
| 
      
 94 
     | 
    
         
            +
                    validator[:validator]
         
     | 
| 
      
 95 
     | 
    
         
            +
                  end
         
     | 
| 
      
 96 
     | 
    
         
            +
             
     | 
| 
      
 97 
     | 
    
         
            +
                  error_message = if validator.is_a?(Hash) && validator[:message]
         
     | 
| 
      
 98 
     | 
    
         
            +
                    validator[:message]
         
     | 
| 
      
 99 
     | 
    
         
            +
                  else
         
     | 
| 
      
 100 
     | 
    
         
            +
                    "Invalid input"
         
     | 
| 
      
 101 
     | 
    
         
            +
                  end
         
     | 
| 
      
 102 
     | 
    
         
            +
             
     | 
| 
      
 103 
     | 
    
         
            +
                  valid = if validator_proc.is_a?(Proc)
         
     | 
| 
      
 104 
     | 
    
         
            +
                    validator_proc.call(input)
         
     | 
| 
      
 105 
     | 
    
         
            +
                  elsif validator_proc.is_a?(String) || validator_proc.is_a?(Symbol)
         
     | 
| 
      
 106 
     | 
    
         
            +
                    input.send(validator_proc)
         
     | 
| 
      
 107 
     | 
    
         
            +
                  elsif validator_proc.blank?
         
     | 
| 
      
 108 
     | 
    
         
            +
                    true
         
     | 
| 
      
 109 
     | 
    
         
            +
                  else
         
     | 
| 
      
 110 
     | 
    
         
            +
                    false
         
     | 
| 
      
 111 
     | 
    
         
            +
                  end
         
     | 
| 
      
 112 
     | 
    
         
            +
             
     | 
| 
      
 113 
     | 
    
         
            +
                  if !valid
         
     | 
| 
      
 114 
     | 
    
         
            +
                    puts error_message
         
     | 
| 
      
 115 
     | 
    
         
            +
                    return false
         
     | 
| 
      
 116 
     | 
    
         
            +
                  end
         
     | 
| 
      
 117 
     | 
    
         
            +
             
     | 
| 
      
 118 
     | 
    
         
            +
                end
         
     | 
| 
      
 119 
     | 
    
         
            +
             
     | 
| 
      
 120 
     | 
    
         
            +
                true
         
     | 
| 
      
 121 
     | 
    
         
            +
             
     | 
| 
      
 122 
     | 
    
         
            +
              end
         
     | 
| 
      
 123 
     | 
    
         
            +
             
     | 
| 
      
 124 
     | 
    
         
            +
             
     | 
| 
      
 125 
     | 
    
         
            +
              def parse(input)
         
     | 
| 
      
 126 
     | 
    
         
            +
             
     | 
| 
      
 127 
     | 
    
         
            +
                @parsers.each do |parser|
         
     | 
| 
      
 128 
     | 
    
         
            +
                  if parser.is_a?(Proc)
         
     | 
| 
      
 129 
     | 
    
         
            +
                    input = parser.call(input)
         
     | 
| 
      
 130 
     | 
    
         
            +
                  elsif parser.is_a?(String) || parser.is_a?(Symbol)
         
     | 
| 
      
 131 
     | 
    
         
            +
                    input = input.send(parser)
         
     | 
| 
      
 132 
     | 
    
         
            +
                  end
         
     | 
| 
      
 133 
     | 
    
         
            +
                end
         
     | 
| 
      
 134 
     | 
    
         
            +
             
     | 
| 
      
 135 
     | 
    
         
            +
                input
         
     | 
| 
      
 136 
     | 
    
         
            +
             
     | 
| 
      
 137 
     | 
    
         
            +
              end
         
     | 
| 
      
 138 
     | 
    
         
            +
             
     | 
| 
      
 139 
     | 
    
         
            +
            end
         
     | 
| 
      
 140 
     | 
    
         
            +
            end
         
     | 
    
        metadata
    ADDED
    
    | 
         @@ -0,0 +1,54 @@ 
     | 
|
| 
      
 1 
     | 
    
         
            +
            --- !ruby/object:Gem::Specification
         
     | 
| 
      
 2 
     | 
    
         
            +
            name: input_reader
         
     | 
| 
      
 3 
     | 
    
         
            +
            version: !ruby/object:Gem::Version
         
     | 
| 
      
 4 
     | 
    
         
            +
              version: 0.0.1
         
     | 
| 
      
 5 
     | 
    
         
            +
              prerelease: 
         
     | 
| 
      
 6 
     | 
    
         
            +
            platform: ruby
         
     | 
| 
      
 7 
     | 
    
         
            +
            authors:
         
     | 
| 
      
 8 
     | 
    
         
            +
            - Alessandro Berardi, Michael Noack
         
     | 
| 
      
 9 
     | 
    
         
            +
            autorequire: 
         
     | 
| 
      
 10 
     | 
    
         
            +
            bindir: bin
         
     | 
| 
      
 11 
     | 
    
         
            +
            cert_chain: []
         
     | 
| 
      
 12 
     | 
    
         
            +
            date: 2012-07-13 00:00:00.000000000 Z
         
     | 
| 
      
 13 
     | 
    
         
            +
            dependencies: []
         
     | 
| 
      
 14 
     | 
    
         
            +
            description: Command line helpers to read input of various types, confirm, etc.
         
     | 
| 
      
 15 
     | 
    
         
            +
            email:
         
     | 
| 
      
 16 
     | 
    
         
            +
            - support@travellink.com.au
         
     | 
| 
      
 17 
     | 
    
         
            +
            executables: []
         
     | 
| 
      
 18 
     | 
    
         
            +
            extensions: []
         
     | 
| 
      
 19 
     | 
    
         
            +
            extra_rdoc_files: []
         
     | 
| 
      
 20 
     | 
    
         
            +
            files:
         
     | 
| 
      
 21 
     | 
    
         
            +
            - .gitignore
         
     | 
| 
      
 22 
     | 
    
         
            +
            - Gemfile
         
     | 
| 
      
 23 
     | 
    
         
            +
            - LICENSE
         
     | 
| 
      
 24 
     | 
    
         
            +
            - README.md
         
     | 
| 
      
 25 
     | 
    
         
            +
            - Rakefile
         
     | 
| 
      
 26 
     | 
    
         
            +
            - input_reader.gemspec
         
     | 
| 
      
 27 
     | 
    
         
            +
            - lib/input_reader.rb
         
     | 
| 
      
 28 
     | 
    
         
            +
            - lib/input_reader/input_reader.rb
         
     | 
| 
      
 29 
     | 
    
         
            +
            - lib/input_reader/version.rb
         
     | 
| 
      
 30 
     | 
    
         
            +
            homepage: ''
         
     | 
| 
      
 31 
     | 
    
         
            +
            licenses: []
         
     | 
| 
      
 32 
     | 
    
         
            +
            post_install_message: 
         
     | 
| 
      
 33 
     | 
    
         
            +
            rdoc_options: []
         
     | 
| 
      
 34 
     | 
    
         
            +
            require_paths:
         
     | 
| 
      
 35 
     | 
    
         
            +
            - lib
         
     | 
| 
      
 36 
     | 
    
         
            +
            required_ruby_version: !ruby/object:Gem::Requirement
         
     | 
| 
      
 37 
     | 
    
         
            +
              none: false
         
     | 
| 
      
 38 
     | 
    
         
            +
              requirements:
         
     | 
| 
      
 39 
     | 
    
         
            +
              - - ! '>='
         
     | 
| 
      
 40 
     | 
    
         
            +
                - !ruby/object:Gem::Version
         
     | 
| 
      
 41 
     | 
    
         
            +
                  version: '0'
         
     | 
| 
      
 42 
     | 
    
         
            +
            required_rubygems_version: !ruby/object:Gem::Requirement
         
     | 
| 
      
 43 
     | 
    
         
            +
              none: false
         
     | 
| 
      
 44 
     | 
    
         
            +
              requirements:
         
     | 
| 
      
 45 
     | 
    
         
            +
              - - ! '>='
         
     | 
| 
      
 46 
     | 
    
         
            +
                - !ruby/object:Gem::Version
         
     | 
| 
      
 47 
     | 
    
         
            +
                  version: '0'
         
     | 
| 
      
 48 
     | 
    
         
            +
            requirements: []
         
     | 
| 
      
 49 
     | 
    
         
            +
            rubyforge_project: 
         
     | 
| 
      
 50 
     | 
    
         
            +
            rubygems_version: 1.8.24
         
     | 
| 
      
 51 
     | 
    
         
            +
            signing_key: 
         
     | 
| 
      
 52 
     | 
    
         
            +
            specification_version: 3
         
     | 
| 
      
 53 
     | 
    
         
            +
            summary: Command line helpers to read input, etc.
         
     | 
| 
      
 54 
     | 
    
         
            +
            test_files: []
         
     |