Class: EasyPrompt

Inherits:
Object
  • Object
show all
Defined in:
lib/easyprompt.rb

Defined Under Namespace

Classes: MockCommandLineUser, MockableStdin, MockableStdout

Constant Summary collapse

Version =
'0.1.3'

Instance Method Summary collapse

Constructor Details

#initializeEasyPrompt

Returns a new instance of EasyPrompt.



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

def initialize
	@stdout = MockableStdout.get_mockable_stdout
	@stdin = MockableStdin.get_mockable_stdin
end

Instance Method Details

#ask(msg, default = nil, response_class = :string) ⇒ Object

Asks the user for input.

msg

The prompt that tells the user what information to enter next.

default

The default value that will be returned if the user enters a newline (usually by pressing “Enter”) without typing any information. The default value will be displayed in square brackets after msg.

response_class

The sort of value that EasyPrompt#ask should return. Valid response classes are:

:string

This is the default.

:array

Takes an arbitrary number of lines entered by the user, and returns them as an array. The entry stops when the user enters a blank line.

:boolean

Values will be turned into true or false depending on whether the user enters “y” or “n”.

:regexp

Values will be read as a regular expression source. If the user enters an invalid regexp source, EasyPrompt will re-prompt until a valid regexp source is entered.



42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/easyprompt.rb', line 42

def ask( msg, default = nil, response_class = :string )
	@success = false
	@last_error = nil
	until @success
		@success = true
		full_msg = msg
		full_msg = @last_error + ' ' + full_msg if @last_error
		@stdout.write( prompt( full_msg, default, response_class ) + ' ' )
		response = try_parse_response( default, response_class )
	end
	response
end

#prompt(msg, default = nil, response_class = :string) ⇒ Object

:nodoc:



55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/easyprompt.rb', line 55

def prompt( msg, default = nil, response_class = :string ) #:nodoc:
	prompt = msg
	unless default.nil?
		if response_class == :boolean
			default_str = default ? 'y' : 'n'
		elsif response_class == :regexp
			default_str = default.source
		else
			default_str = default.to_s
		end
		prompt += " [#{ default_str }]"
	end
	prompt
end

#try_parse_array_response(response) ⇒ Object

:nodoc:



70
71
72
73
74
75
76
# File 'lib/easyprompt.rb', line 70

def try_parse_array_response( response ) #:nodoc:
	response = [ response ]
	while ( ( next_line = @stdin.gets ) != "\n" )
		response << next_line.chomp
	end
	response
end

#try_parse_regexp_response(response) ⇒ Object

:nodoc:



78
79
80
81
82
83
84
85
86
# File 'lib/easyprompt.rb', line 78

def try_parse_regexp_response( response ) #:nodoc:
	begin
		Regexp.new response
	rescue RegexpError
		@success = false
		@last_error = "I'm sorry, but that was not a valid regular expression."
		nil
	end
end

#try_parse_response(default, response_class) ⇒ Object

:nodoc:



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/easyprompt.rb', line 88

def try_parse_response( default, response_class ) #:nodoc:
	response = @stdin.gets
	response.chomp!
	if response == ''
		default
	else
		if response_class == :boolean
			response =~ /^y/i 
		elsif response_class == :regexp
			try_parse_regexp_response response
		elsif response_class == :array
			try_parse_array_response response
		end
	end
end