userinput 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +5 -0
- data/.rspec +2 -0
- data/.rubocop.yml +2 -0
- data/.travis.yml +13 -0
- data/Gemfile +4 -0
- data/LICENSE +22 -0
- data/README.md +22 -0
- data/Rakefile +14 -0
- data/lib/userinput.rb +96 -0
- data/spec/spec_helper.rb +10 -0
- data/spec/userinput_spec.rb +66 -0
- data/userinput.gemspec +21 -0
- metadata +127 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 7931090fde842e07e7bb83ca88dcef175c348f0c
|
4
|
+
data.tar.gz: ce4cb1f5fc13933887acac8b53d300042bef9b05
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: f69f1f71afd3d61a7ec05578867d5c6ef731639a6b58c5a978cc2b68073c10c6781b740641c4414ccdfd80066a0691bd7b3327c120f353fc37f094696b6085fc
|
7
|
+
data.tar.gz: e519c52e115d34fa9711377911da156d2f5cc14570f3be3b17b7dba4946340bc95b038ee89e59bba5a3d1b8b57f0e8e3f306c5aaa455c44e3f2115f44687e5dc
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.rubocop.yml
ADDED
data/.travis.yml
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
language: ruby
|
2
|
+
cache: bundler
|
3
|
+
rvm:
|
4
|
+
- 2.1.0
|
5
|
+
- 2.0.0
|
6
|
+
- 1.9.3
|
7
|
+
notifications:
|
8
|
+
email: false
|
9
|
+
irc:
|
10
|
+
template:
|
11
|
+
- '%{repository}/%{branch}/%{build_number}: %{message} -- %{build_url}'
|
12
|
+
channels:
|
13
|
+
secure: OkVku3yPBdazEJyXtmrAZCNiR2ceuvFGwz1ap6vQDA/In/d5W4Nt58zxrSp8mCP2rDXjju4N+toZNt6nPA4RU7OiKlo6o4Wo5ARqNPDBPRui2cafBWGldW68gI6yAArcyMCt2iRiSy+GCGk5GwCDxKhQ1b3vR/MLopQUUYc3c9U=
|
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2014 Les Aker
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
22
|
+
|
data/README.md
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
userinput
|
2
|
+
=========
|
3
|
+
|
4
|
+
[![Gem Version](https://img.shields.io/gem/v/userinput.svg)](https://rubygems.org/gems/userinput)
|
5
|
+
[![Dependency Status](https://img.shields.io/gemnasium/akerl/userinput.svg)](https://gemnasium.com/akerl/userinput)
|
6
|
+
[![Code Climate](https://img.shields.io/codeclimate/github/akerl/userinput.svg)](https://codeclimate.com/github/akerl/userinput)
|
7
|
+
[![Coverage Status](https://img.shields.io/coveralls/akerl/userinput.svg)](https://coveralls.io/r/akerl/userinput)
|
8
|
+
[![Build Status](https://img.shields.io/travis/akerl/userinput.svg)](https://travis-ci.org/akerl/userinput)
|
9
|
+
[![MIT Licensed](https://img.shields.io/badge/license-MIT-green.svg)](https://tldrlegal.com/license/mit-license)
|
10
|
+
|
11
|
+
SHORT_DESCRIPTION
|
12
|
+
|
13
|
+
## Usage
|
14
|
+
|
15
|
+
## Installation
|
16
|
+
|
17
|
+
gem install userinput
|
18
|
+
|
19
|
+
## License
|
20
|
+
|
21
|
+
userinput is released under the MIT License. See the bundled LICENSE file for details.
|
22
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
require 'bundler/gem_tasks'
|
2
|
+
require 'rspec/core/rake_task'
|
3
|
+
require 'rubocop/rake_task'
|
4
|
+
|
5
|
+
desc 'Run tests'
|
6
|
+
RSpec::Core::RakeTask.new(:spec)
|
7
|
+
|
8
|
+
desc 'Run Rubocop on the gem'
|
9
|
+
Rubocop::RakeTask.new(:rubocop) do |task|
|
10
|
+
task.patterns = ['lib/**/*.rb', 'spec/**/*.rb']
|
11
|
+
task.fail_on_error = true
|
12
|
+
end
|
13
|
+
|
14
|
+
task default: [:spec, :rubocop, :build, :install]
|
data/lib/userinput.rb
ADDED
@@ -0,0 +1,96 @@
|
|
1
|
+
##
|
2
|
+
# User input library
|
3
|
+
module UserInput
|
4
|
+
class << self
|
5
|
+
##
|
6
|
+
# Insert a helper .new() method for creating a new Prompt object
|
7
|
+
|
8
|
+
def new(*args, &block)
|
9
|
+
self::Prompt.new(*args, &block)
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
##
|
14
|
+
# Prompt object
|
15
|
+
class Prompt
|
16
|
+
##
|
17
|
+
# Build new prompt object and set defaults
|
18
|
+
def initialize(params = {}, &block)
|
19
|
+
@attempts = params[:attempts]
|
20
|
+
@message = params[:message] || ''
|
21
|
+
@default = params[:default]
|
22
|
+
@secret = params[:secret] || false
|
23
|
+
@validation = block || params[:validation]
|
24
|
+
end
|
25
|
+
|
26
|
+
##
|
27
|
+
# Request user input
|
28
|
+
def ask
|
29
|
+
print "#{@message}? #{@default.nil? ? '' : "[#{@default}] "}"
|
30
|
+
disable_echo if @secret
|
31
|
+
|
32
|
+
input = _ask
|
33
|
+
return input if valid(input)
|
34
|
+
|
35
|
+
check_counter
|
36
|
+
ask
|
37
|
+
ensure
|
38
|
+
enable_echo if @secret
|
39
|
+
end
|
40
|
+
|
41
|
+
##
|
42
|
+
# Validate user input
|
43
|
+
def valid(input)
|
44
|
+
case @validation
|
45
|
+
when Proc
|
46
|
+
return @validation.call input
|
47
|
+
when Regexp
|
48
|
+
return @validation.match input
|
49
|
+
when NilClass
|
50
|
+
return true
|
51
|
+
else
|
52
|
+
fail "Supported validation type not provided #{@validation.class}"
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
private
|
57
|
+
|
58
|
+
##
|
59
|
+
# Parse user input
|
60
|
+
def _ask
|
61
|
+
input = STDIN.gets.chomp
|
62
|
+
input = @default if input.empty? && !@default.nil?
|
63
|
+
input
|
64
|
+
end
|
65
|
+
|
66
|
+
##
|
67
|
+
# Track attempt counter
|
68
|
+
def check_counter
|
69
|
+
unless @attempts.nil?
|
70
|
+
@attempts -= 1
|
71
|
+
fail ArgumentError, 'No valid input provided' if @attempts == 0
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
##
|
76
|
+
# Disable terminal display of user input
|
77
|
+
def disable_echo
|
78
|
+
toggle_echo false
|
79
|
+
end
|
80
|
+
|
81
|
+
##
|
82
|
+
# Enable terminal display of user input
|
83
|
+
def enable_echo
|
84
|
+
toggle_echo true
|
85
|
+
end
|
86
|
+
|
87
|
+
##
|
88
|
+
# Toggle terminal display of user input
|
89
|
+
def toggle_echo(state)
|
90
|
+
setting = state ? '' : '-'
|
91
|
+
`stty #{setting}echo`
|
92
|
+
rescue
|
93
|
+
nil
|
94
|
+
end
|
95
|
+
end
|
96
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,66 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe UserInput do
|
4
|
+
describe '#new' do
|
5
|
+
it 'creates Prompt objects' do
|
6
|
+
expect(UserInput.new).to be_an_instance_of UserInput::Prompt
|
7
|
+
end
|
8
|
+
end
|
9
|
+
|
10
|
+
describe UserInput::Prompt do
|
11
|
+
let(:subject) do
|
12
|
+
UserInput::Prompt.new(
|
13
|
+
attempts: 2,
|
14
|
+
message: '_msg',
|
15
|
+
default: '_default'
|
16
|
+
)
|
17
|
+
end
|
18
|
+
|
19
|
+
describe '#new' do
|
20
|
+
it 'makes a new Prompt object' do
|
21
|
+
expect(subject).to be_an_instance_of UserInput::Prompt
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
describe '#ask' do
|
26
|
+
it 'prompts for user input' do
|
27
|
+
allow(STDIN).to receive(:gets) { "_answer\n" }
|
28
|
+
expect(subject).to receive(:print).with('_msg? [_default] ')
|
29
|
+
expect(subject.ask).to eql '_answer'
|
30
|
+
end
|
31
|
+
|
32
|
+
it 'returns the default if available' do
|
33
|
+
allow(STDIN).to receive(:gets) { "\n" }
|
34
|
+
expect(subject).to receive(:print).with('_msg? [_default] ')
|
35
|
+
expect(subject.ask).to eql '_default'
|
36
|
+
end
|
37
|
+
|
38
|
+
context 'when provided a Regexp' do
|
39
|
+
it 'validates input' do
|
40
|
+
prompt = UserInput::Prompt.new(
|
41
|
+
message: '_msg',
|
42
|
+
validation: /[0-9]+/
|
43
|
+
)
|
44
|
+
allow(STDIN).to receive(:gets).and_return("_str\n", "29\n")
|
45
|
+
expect(prompt).to receive(:print).with('_msg? ').twice
|
46
|
+
expect(prompt.ask).to eql '29'
|
47
|
+
end
|
48
|
+
end
|
49
|
+
context 'when provided a code block' do
|
50
|
+
it 'validates input' do
|
51
|
+
prompt = UserInput::Prompt.new { |x| x == 'correct' }
|
52
|
+
allow(STDIN).to receive(:gets).and_return("_str\n", "correct\n")
|
53
|
+
expect(prompt).to receive(:print).with('? ').twice
|
54
|
+
expect(prompt.ask).to eql 'correct'
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
it 'raises an error if max attempts is reached' do
|
59
|
+
prompt = UserInput::Prompt.new(attempts: 2) { |x| false }
|
60
|
+
allow(STDIN).to receive(:gets).and_return("_str\n", "_foo\n")
|
61
|
+
expect(prompt).to receive(:print).with('? ').twice
|
62
|
+
expect { prompt.ask }.to raise_error ArgumentError
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
data/userinput.gemspec
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
Gem::Specification.new do |s|
|
2
|
+
s.name = 'userinput'
|
3
|
+
s.version = '0.0.1'
|
4
|
+
s.date = Time.now.strftime("%Y-%m-%d")
|
5
|
+
|
6
|
+
s.summary = ''
|
7
|
+
s.description = ""
|
8
|
+
s.authors = ['Les Aker']
|
9
|
+
s.email = 'me@lesaker.org'
|
10
|
+
s.homepage = 'https://github.com/akerl/userinput'
|
11
|
+
s.license = 'MIT'
|
12
|
+
|
13
|
+
s.files = `git ls-files`.split
|
14
|
+
s.test_files = `git ls-files spec/*`.split
|
15
|
+
|
16
|
+
s.add_development_dependency 'rubocop', '~> 0.19.0'
|
17
|
+
s.add_development_dependency 'rake', '~> 10.2.1'
|
18
|
+
s.add_development_dependency 'coveralls', '~> 0.7.0'
|
19
|
+
s.add_development_dependency 'rspec', '~> 2.14.1'
|
20
|
+
s.add_development_dependency 'fuubar', '~> 1.3.2'
|
21
|
+
end
|
metadata
ADDED
@@ -0,0 +1,127 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: userinput
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Les Aker
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-03-29 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rubocop
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ~>
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 0.19.0
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ~>
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 0.19.0
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ~>
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: 10.2.1
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ~>
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: 10.2.1
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: coveralls
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ~>
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: 0.7.0
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ~>
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: 0.7.0
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rspec
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ~>
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: 2.14.1
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ~>
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: 2.14.1
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: fuubar
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ~>
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: 1.3.2
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ~>
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: 1.3.2
|
83
|
+
description: ''
|
84
|
+
email: me@lesaker.org
|
85
|
+
executables: []
|
86
|
+
extensions: []
|
87
|
+
extra_rdoc_files: []
|
88
|
+
files:
|
89
|
+
- .gitignore
|
90
|
+
- .rspec
|
91
|
+
- .rubocop.yml
|
92
|
+
- .travis.yml
|
93
|
+
- Gemfile
|
94
|
+
- LICENSE
|
95
|
+
- README.md
|
96
|
+
- Rakefile
|
97
|
+
- lib/userinput.rb
|
98
|
+
- spec/spec_helper.rb
|
99
|
+
- spec/userinput_spec.rb
|
100
|
+
- userinput.gemspec
|
101
|
+
homepage: https://github.com/akerl/userinput
|
102
|
+
licenses:
|
103
|
+
- MIT
|
104
|
+
metadata: {}
|
105
|
+
post_install_message:
|
106
|
+
rdoc_options: []
|
107
|
+
require_paths:
|
108
|
+
- lib
|
109
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
110
|
+
requirements:
|
111
|
+
- - '>='
|
112
|
+
- !ruby/object:Gem::Version
|
113
|
+
version: '0'
|
114
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
115
|
+
requirements:
|
116
|
+
- - '>='
|
117
|
+
- !ruby/object:Gem::Version
|
118
|
+
version: '0'
|
119
|
+
requirements: []
|
120
|
+
rubyforge_project:
|
121
|
+
rubygems_version: 2.0.14
|
122
|
+
signing_key:
|
123
|
+
specification_version: 4
|
124
|
+
summary: ''
|
125
|
+
test_files:
|
126
|
+
- spec/spec_helper.rb
|
127
|
+
- spec/userinput_spec.rb
|