Module: XMigra::Console

Defined in:
lib/xmigra/console.rb

Defined Under Namespace

Classes: InvalidInput, Menu

Class Method Summary collapse

Class Method Details

.output_section(title = nil, opts = {}) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
# 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) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# 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
      puts e.message if e.explicit_message?
      next
    end
    
    return result unless result.nil?
  end
end

.yes_no(prompt, default_value) ⇒ Object



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/xmigra/console.rb', line 98

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