module XMigra::Console

Public Class Methods

output_section(title=nil, opts={}) { || ... } click to toggle source
# File lib/xmigra/console.rb, line 69
def output_section(title=nil, opts={})
  trailing_newlines = opts[:trailing_newlines] || 3
  
  if title
    puts " #{title} ".center(40, '=')
    puts
  end
  
  (yield).tap do
    trailing_newlines.times {puts}
  end
end
validated_input(prompt) { |input_value| ... } click to toggle source
# File lib/xmigra/console.rb, line 82
def validated_input(prompt)
  loop do
    print prompt + ": "
    input_value = $stdin.gets.strip
    
    result = begin
      yield input_value
    rescue InvalidInput => e
      XMigra.log_error(e)
      puts e.message if e.explicit_message?
      next
    end
    
    return result unless result.nil?
  end
end
yes_no(prompt, default_value) click to toggle source
# File lib/xmigra/console.rb, line 99
def yes_no(prompt, default_value)
  input_options = ""
  input_options << (default_value == :yes ? "Y" : "y")
  input_options << (default_value == :no ? "N" : "n")
  
  validated_input("#{prompt} [#{input_options}]") do |input_value|
    case input_value
    when /^y(es)?$/io
      true
    when /^n(o)?$/io
      false
    when ''
      {:yes => true, :no => false}[default_value]
    end
  end
end