easyprompt 0.1.1 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. data/lib/easyprompt.rb +6 -5
  2. data/lib/easyprompt.rb~ +38 -12
  3. metadata +5 -5
data/lib/easyprompt.rb CHANGED
@@ -20,10 +20,11 @@
20
20
  #
21
21
  # The Rubyforge project page can be found at http://rubyforge.org/projects/easyprompt/ .
22
22
 
23
- require 'lafcadio'
23
+ require 'contxtlservice'
24
+ require 'observer'
24
25
 
25
26
  class EasyPrompt
26
- Version = '0.1.1'
27
+ Version = '0.1.2'
27
28
 
28
29
  def initialize; @stdout = MockableStdout.get_mockable_stdout; end
29
30
 
@@ -124,7 +125,7 @@ class EasyPrompt
124
125
  def initialize
125
126
  @responses = {}
126
127
  flush
127
- context = Lafcadio::Context.instance
128
+ context = ContextualService::Context.instance
128
129
  @mock_stdin = StringIO.new
129
130
  @mock_stdin.add_observer( self )
130
131
  context.set_resource( MockableStdin, @mock_stdin )
@@ -176,13 +177,13 @@ class EasyPrompt
176
177
  end
177
178
  end
178
179
 
179
- class MockableStdin < Lafcadio::ContextualService #:nodoc:
180
+ class MockableStdin < ContextualService::Service #:nodoc:
180
181
  def method_missing( symbol, *args )
181
182
  $stdin.send( symbol, *args )
182
183
  end
183
184
  end
184
185
 
185
- class MockableStdout < Lafcadio::ContextualService #:nodoc:
186
+ class MockableStdout < ContextualService::Service #:nodoc:
186
187
  def method_missing( symbol, *args )
187
188
  $stdout.send( symbol, *args )
188
189
  end
data/lib/easyprompt.rb~ CHANGED
@@ -1,5 +1,3 @@
1
- require 'lafcadio'
2
-
3
1
  # EasyPrompt is a utility for command-line scripts. It handles prompts and default values, and also provides a testing facility for mocking out the command-line user.
4
2
  #
5
3
  # For example, here's an irb session that illustrates what EasyPrompt does:
@@ -19,8 +17,13 @@ require 'lafcadio'
19
17
  # => true
20
18
  #
21
19
  # In the first example, we ask for the user's first name and get "John" as the response. In the second example, we supply the default of "Doe", which the user chooses by just pressing the "Enter" key. In the third example, we supply the default of +true+, which the user chooses as well. We received the boolean value +true+ as opposed to the string "true" or "y", because we specified <tt>:boolean</tt> as the +response_class+.
20
+ #
21
+ # The Rubyforge project page can be found at http://rubyforge.org/projects/easyprompt/ .
22
+
23
+ require 'lafcadio'
24
+
22
25
  class EasyPrompt
23
- Version = '0.1.0'
26
+ Version = '0.1.1'
24
27
 
25
28
  def initialize; @stdout = MockableStdout.get_mockable_stdout; end
26
29
 
@@ -30,15 +33,32 @@ class EasyPrompt
30
33
  # response_class:: The sort of value that EasyPrompt#ask should return. Valid response classes are:
31
34
  # [:string] This is the default.
32
35
  # [:boolean] Values will be turned into <tt>true</tt> or <tt>false</tt> depending on whether the user enters "y" or "n".
36
+ # [: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.
33
37
  def ask( msg, default = nil, response_class = :string )
34
- @stdout.write( prompt( msg, default, response_class ) + ' ' )
35
- stdin = MockableStdin.get_mockable_stdin
36
- response = stdin.gets
37
- response.chomp!
38
- if response == ''
39
- response = default
40
- else
41
- response = response =~ /^y/i if response_class == :boolean
38
+ success = false
39
+ last_error = nil
40
+ until success
41
+ success = true
42
+ full_msg = msg
43
+ full_msg = last_error + ' ' + full_msg if last_error
44
+ @stdout.write( prompt( full_msg, default, response_class ) + ' ' )
45
+ stdin = MockableStdin.get_mockable_stdin
46
+ response = stdin.gets
47
+ response.chomp!
48
+ if response == ''
49
+ response = default
50
+ else
51
+ if response_class == :boolean
52
+ response = response =~ /^y/i
53
+ elsif response_class == :regexp
54
+ begin
55
+ response = Regexp.new response
56
+ rescue RegexpError
57
+ success = false
58
+ last_error = "I'm sorry, but that was not a valid regular expression."
59
+ end
60
+ end
61
+ end
42
62
  end
43
63
  response
44
64
  end
@@ -48,6 +68,8 @@ class EasyPrompt
48
68
  unless default.nil?
49
69
  if response_class == :boolean
50
70
  default_str = default ? 'y' : 'n'
71
+ elsif response_class == :regexp
72
+ default_str = default.source
51
73
  else
52
74
  default_str = default.to_s
53
75
  end
@@ -101,7 +123,7 @@ class EasyPrompt
101
123
  # is no match, an error will be raised. Use set_response to add a response.
102
124
  def initialize
103
125
  @responses = {}
104
- @match_count = Hash.new( 1 )
126
+ flush
105
127
  context = Lafcadio::Context.instance
106
128
  @mock_stdin = StringIO.new
107
129
  @mock_stdin.add_observer( self )
@@ -109,6 +131,10 @@ class EasyPrompt
109
131
  @mock_stdout = StringIO.new
110
132
  context.set_resource( MockableStdout, @mock_stdout )
111
133
  end
134
+
135
+ # Clears out the running count of how many times each response has been
136
+ # used.
137
+ def flush; @match_count = Hash.new( 1 ); end
112
138
 
113
139
  def match_regexp #:nodoc:
114
140
  arg = @mock_stdout.string
metadata CHANGED
@@ -3,8 +3,8 @@ rubygems_version: 0.8.6
3
3
  specification_version: 1
4
4
  name: easyprompt
5
5
  version: !ruby/object:Gem::Version
6
- version: 0.1.1
7
- date: 2005-04-11
6
+ version: 0.1.2
7
+ date: 2005-08-09
8
8
  summary: EasyPrompt is a utility for command-line scripts.
9
9
  require_paths:
10
10
  - lib
@@ -39,12 +39,12 @@ extensions: []
39
39
  requirements: []
40
40
  dependencies:
41
41
  - !ruby/object:Gem::Dependency
42
- name: lafcadio
42
+ name: contxtlservice
43
43
  version_requirement:
44
44
  version_requirements: !ruby/object:Gem::Version::Requirement
45
45
  requirements:
46
46
  -
47
- - ">="
47
+ - ">"
48
48
  - !ruby/object:Gem::Version
49
- version: 0.6.0
49
+ version: 0.0.0
50
50
  version: