interactive 0.2.0 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: f2b4b79a65cc5ac54133c83d492ac1ead6ae2e9d
4
- data.tar.gz: 53c30ac9d8daacd36e4b3e5f294c4e3ad52fc886
3
+ metadata.gz: d9dcea125c70a876fcb818733193c9d9554a4455
4
+ data.tar.gz: f992006a8ae904ccf96f52bd227892edccabef92
5
5
  SHA512:
6
- metadata.gz: ddfd47284226b675a8afb50de9dd5aa6a3a072000227b657e8260efc3ad1ad841a405ea3b3086acf100895ec49ada6c9c4b50238602036b8407d37deb643ed01
7
- data.tar.gz: ddac10c4ef2bb18eb1c50f632ed34578d8f0c54ce476061110219b2079c4aa398b4abc89e0321361877994ac3c3bc959b0b88c7f866423d03f67954174255f31
6
+ metadata.gz: 95ce90bea80d01932067744d6dbd7c7581ba971a4736968b4562361124cce93fa5a2e70f3004ece8bb98d61c94c0b8218f9be6dfe2d1df25e3da78f00ceeaf73
7
+ data.tar.gz: f50ec4bc741764e1a8eb07c4afe6380f9e07575afd5ed9ed921bd300a7365d6a3318cc07d6310ab2ac1954ea0b335430131489e2d995ca90f38049e13d4e8687
data/CHANGELOG.markdown CHANGED
@@ -1,9 +1,7 @@
1
- v0.1.0 -- Mon Mar 16 07:17:53 EDT 2015
1
+ v0.3.0 -- Wed Mar 18 11:28:05 EDT 2015
2
2
  --------------------------------------
3
- - Added support for handling ranges.
4
- - Useful for processing responses of questions like "Which item do you want to
5
- use?", where whole number responses such as "1", "2", etc. makes sense.
6
- - Refactored, DRY'er.
3
+ - Added support for questions without options
4
+ - Aliased `Question#ask_and_wait_for_valid_response` as `Question#ask`
7
5
 
8
6
  v0.2.0 -- Mon Mar 16 21:52:21 EDT 2015
9
7
  --------------------------------------
@@ -13,3 +11,10 @@ v0.2.0 -- Mon Mar 16 21:52:21 EDT 2015
13
11
  corresponding to the index in the array.
14
12
  - Putting in an array will eagerly trigger the full explanation of
15
13
  the shortcut (i.e. show the full explanation right after the question)
14
+
15
+ v0.1.0 -- Mon Mar 16 07:17:53 EDT 2015
16
+ --------------------------------------
17
+ - Added support for handling ranges.
18
+ - Useful for processing responses of questions like "Which item do you want to
19
+ use?", where whole number responses such as "1", "2", etc. makes sense.
20
+ - Refactored, DRY'er.
data/README.md CHANGED
@@ -23,13 +23,55 @@ Or install it yourself as:
23
23
 
24
24
  ## Usage
25
25
 
26
+ ### Questions Without any Options
27
+ If you want to ask a user a question accepting all answers:
28
+
29
+ ```ruby
30
+ require 'interactive'
31
+ include 'interactive'
32
+
33
+ question = Question.new do |q|
34
+ q.question = "What is your api token password?"
35
+ end
36
+
37
+ question.ask do |response|
38
+ # response is an object that responds to string methods
39
+ puts response.split
40
+ end
41
+ ```
42
+
43
+ This will ask:
44
+
45
+ ```ruby
46
+ => What are your project ids?
47
+ ```
48
+
49
+ You can respond like so, for example:
50
+
51
+ ```ruby
52
+ $ 123someid hello4545 12992hhoo
53
+ ```
54
+
55
+ That will give us the following:
56
+
57
+ ```ruby
58
+ => 123someid
59
+ hello4545
60
+ 12992hhoo
61
+ ```
62
+
26
63
  ### Questions With Lazy Shortcut Explanations
64
+
27
65
  If you want to ask a user a question expecting certain answers:
28
66
 
67
+
29
68
  ```ruby
30
- question = Interactive::Question.new do |ques|
31
- ques.question = "Which item do you want to use?"
32
- ques.options = [1..3, :cancel, :quit]
69
+ require 'interactive'
70
+ include 'interactive'
71
+
72
+ question = Question.new do |q|
73
+ q.question = "Which item do you want to use?"
74
+ q.options = [1..3, :cancel, :quit]
33
75
  end
34
76
  ```
35
77
 
@@ -37,7 +79,7 @@ You can run the loop and wait for a valid response and do query methods on the
37
79
  response:
38
80
 
39
81
  ```ruby
40
- question.ask_and_wait_for_valid_response do |response|
82
+ question.ask do |response|
41
83
  if response.whole_num_1?
42
84
  # do stuff if user responded with "1"
43
85
  elsif response.whole_num?
@@ -82,14 +124,18 @@ Providing an array of options to the options array will trigger the shortcut
82
124
  explanation right after asking the question:
83
125
 
84
126
  ```ruby
127
+ require 'interactive'
128
+ include 'interactive'
129
+
130
+ # ...
85
131
 
86
132
  options_list = ["/some/path", "/some/other/path"]
87
- iq = Interactive::Question.new do |q|
133
+ iq = Question.new do |q|
88
134
  q.question = "Which path do you want to use?"
89
135
  q.options = [options_list, :cancel]
90
136
  end
91
137
 
92
- iq.ask_and_wait_for_valid_response do |response|
138
+ iq.ask do |response|
93
139
  if response.whole_number?
94
140
  # response.to_i will convert the response string to an integer.
95
141
  # useful for getting the index (i.e. options_list[response.to_i])
data/lib/interactive.rb CHANGED
@@ -1,4 +1,5 @@
1
1
  require "interactive/version"
2
+ require "delegate"
2
3
  require "interactive/option"
3
4
  require "interactive/options"
4
5
  require "interactive/question_with_eager_full_explanation"
@@ -1,12 +1,17 @@
1
1
  require 'delegate'
2
+ require 'interactive'
2
3
 
3
4
  module Interactive
4
- class Options < SimpleDelegator
5
- include Interactive
6
- attr_accessor :options
5
+ module_function
6
+ def Options(options=[])
7
+ options.empty? ? EmptyOptions.new([]) : NonEmptyOptions.new(options)
8
+ end
7
9
 
10
+ class NonEmptyOptions < SimpleDelegator
11
+ include Interactive
8
12
  def initialize(options)
9
- flatten_ranges(options)
13
+ @options = options
14
+ flatten_ranges(@options)
10
15
  wrap_each_option
11
16
  super(@options)
12
17
  end
@@ -16,7 +21,7 @@ module Interactive
16
21
  end
17
22
 
18
23
  def shortcuts_meanings
19
- options.inject("") { |accum, opt| "#{accum} #{opt.shortcut_value} -- #{opt.value}\n"}
24
+ @options.inject("") { |accum, opt| "#{accum} #{opt.shortcut_value} -- #{opt.value}\n"}
20
25
  end
21
26
 
22
27
  def has_hash?
@@ -42,11 +47,31 @@ module Interactive
42
47
  end
43
48
 
44
49
  def first_chars
45
- options.inject("") { |accum, opt| "#{accum}#{ opt.shortcut_value}/" }
50
+ @options.inject("") { |accum, opt| "#{accum}#{ opt.shortcut_value}/" }
46
51
  end
47
52
 
48
53
  def first_chars_without_last_slash(first_chars)
49
54
  first_chars[0..first_chars.length-2]
50
55
  end
51
56
  end
57
+
58
+
59
+ class EmptyOptions < SimpleDelegator
60
+ def initialize(options)
61
+ @options = options
62
+ super(@options)
63
+ end
64
+
65
+ def shortcuts_string
66
+ ''
67
+ end
68
+
69
+ def shortcuts_meanings
70
+ ''
71
+ end
72
+
73
+ def has_hash?
74
+ false
75
+ end
76
+ end
52
77
  end
@@ -5,12 +5,11 @@ module Interactive
5
5
  def initialize(&block)
6
6
  yield self
7
7
 
8
- @options = Interactive::Options.new(Array(@options))
8
+ @options = Interactive::Options(Array(@options))
9
9
 
10
10
  raise ArgumentError, "question cannot be nil nor empty." if question.nil? || question.empty?
11
- raise ArgumentError, "options cannot be empty." if options.empty?
12
-
13
11
  @question_type = @options.has_hash? ? QuestionWithEagerFullExplanation.new(self) : QuestionWithLazyFullExplanation.new(self)
12
+ alias :ask :ask_and_wait_for_valid_response
14
13
  end
15
14
 
16
15
  def ask_and_wait_for_valid_response(&block)
@@ -3,7 +3,7 @@ class QuestionWithEagerFullExplanation < SimpleDelegator
3
3
  loop do
4
4
  puts "#{question} #{options.shortcuts_string}"
5
5
  puts options.shortcuts_meanings
6
- resp = Interactive::Response.new(options)
6
+ resp = Interactive::Response(options)
7
7
 
8
8
  yield resp
9
9
  break unless resp.invalid?
@@ -2,7 +2,7 @@ class QuestionWithLazyFullExplanation < SimpleDelegator
2
2
  def ask_and_wait_for_valid_response(&block)
3
3
  loop do
4
4
  puts "#{question} #{options.shortcuts_string}"
5
- resp = Interactive::Response.new(options)
5
+ resp = Interactive::Response(options)
6
6
  puts options.shortcuts_meanings if resp.invalid?
7
7
 
8
8
  yield resp
@@ -1,7 +1,29 @@
1
1
  require 'interactive'
2
2
 
3
3
  module Interactive
4
- class Response
4
+ module_function
5
+
6
+ def Response(*args)
7
+ args = Array(args).flatten
8
+ args.empty? ? ResponseWithNoArgs.new(args) : ResponseWithArgs.new(args)
9
+ end
10
+
11
+ class ResponseWithNoArgs < SimpleDelegator
12
+ def initialize(*args)
13
+ @_response = STDIN.gets.chomp
14
+ super(@_response)
15
+ end
16
+
17
+ def valid?
18
+ true
19
+ end
20
+
21
+ def invalid?
22
+ false
23
+ end
24
+ end
25
+
26
+ class ResponseWithArgs < SimpleDelegator
5
27
  def initialize(*args)
6
28
  @args = Array(args).flatten
7
29
  check_validity
@@ -11,10 +33,8 @@ module Interactive
11
33
  define_methods
12
34
  define_invalid
13
35
  define_whole_number
14
- end
15
36
 
16
- def to_i
17
- @_response.to_i
37
+ super(@_response)
18
38
  end
19
39
 
20
40
  private
@@ -23,7 +43,6 @@ module Interactive
23
43
  raise ArgumentError, "may not use :invalid or 'invalid' as an argument. Private method." if @args.map(&:to_s).include?('invalid')
24
44
  raise ArgumentError, "may not use :whole_number or 'whole_number' as an argument. Private method." if @args.map(&:to_s).include?('whole_number')
25
45
  raise ArgumentError, "may not have keyword options that have the same first letter." if first_chars_not_unique
26
- raise ArgumentError, "wrong number of arguments (need at least two arguments)." if @args.length < 2
27
46
  end
28
47
 
29
48
  def define_methods
@@ -1,3 +1,3 @@
1
1
  module Interactive
2
- VERSION = "0.2.0"
2
+ VERSION = "0.3.0"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: interactive
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Edderic Ugaddan
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-03-17 00:00:00.000000000 Z
11
+ date: 2015-03-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake