interactive 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: f2c6959fba0126e77b3a9147f7f23435a19064cf
4
- data.tar.gz: 7418c32a98aacddb6df983502f09abdf738f2d5c
3
+ metadata.gz: f2b4b79a65cc5ac54133c83d492ac1ead6ae2e9d
4
+ data.tar.gz: 53c30ac9d8daacd36e4b3e5f294c4e3ad52fc886
5
5
  SHA512:
6
- metadata.gz: 4fe3a14aa9871323c85e1045d9eb0341422ec9489f49484d30756555e053eb1b7ec1c93a431c407b7294f87c9bd302936b7ddac18ef0a6cde98c816def50a0b1
7
- data.tar.gz: a3144758369854cbf2790e558147f2a6bcd2f7f3269920e98c37cc08c67519b496fc3a3ff813cdb0534ee09709684eece520276616662b24cf4a9478f9b74d64
6
+ metadata.gz: ddfd47284226b675a8afb50de9dd5aa6a3a072000227b657e8260efc3ad1ad841a405ea3b3086acf100895ec49ada6c9c4b50238602036b8407d37deb643ed01
7
+ data.tar.gz: ddac10c4ef2bb18eb1c50f632ed34578d8f0c54ce476061110219b2079c4aa398b4abc89e0321361877994ac3c3bc959b0b88c7f866423d03f67954174255f31
data/CHANGELOG.markdown CHANGED
@@ -4,3 +4,12 @@ v0.1.0 -- Mon Mar 16 07:17:53 EDT 2015
4
4
  - Useful for processing responses of questions like "Which item do you want to
5
5
  use?", where whole number responses such as "1", "2", etc. makes sense.
6
6
  - Refactored, DRY'er.
7
+
8
+ v0.2.0 -- Mon Mar 16 21:52:21 EDT 2015
9
+ --------------------------------------
10
+
11
+ - Added support for handling an array.
12
+ - The items of that array gets internally turned into hashes, with keys
13
+ corresponding to the index in the array.
14
+ - Putting in an array will eagerly trigger the full explanation of
15
+ the shortcut (i.e. show the full explanation right after the question)
data/README.md CHANGED
@@ -23,6 +23,7 @@ Or install it yourself as:
23
23
 
24
24
  ## Usage
25
25
 
26
+ ### Questions With Lazy Shortcut Explanations
26
27
  If you want to ask a user a question expecting certain answers:
27
28
 
28
29
  ```ruby
@@ -49,30 +50,62 @@ question.ask_and_wait_for_valid_response do |response|
49
50
  end
50
51
  ```
51
52
 
52
- That will ask the question appended by the shortcuts:
53
+ That will ask the question appended by the shortcuts (without full explanation):
53
54
 
54
- ```ruby
55
- # => "Which item do you want to use? [1/2/3/c/q]"
55
+ ```sh
56
+ Which item do you want to use? [1/2/3/c/q]
56
57
  ```
57
58
 
58
59
  If the response is valid:
59
60
 
60
- ```ruby
61
- $ a
62
- # => response.add? will return true
61
+ ```sh
62
+ $ a # response.add? will return true
63
63
  ```
64
64
 
65
65
  If the response is invalid, it prints out the question and goes into detail as
66
66
  to what the shortcuts stand for:
67
67
 
68
- ```ruby
68
+ ```sh
69
69
  $ bad-response
70
- # => Which item do you want to use? [1/2/3/c/q]
71
- # => 1 -- 1
72
- # => 2 -- 2
73
- # => 3 -- 3
74
- # => c -- cancel
75
- # => q -- quit
70
+
71
+ Which item do you want to use? [1/2/3/c/q]
72
+ 1 -- 1
73
+ 2 -- 2
74
+ 3 -- 3
75
+ c -- cancel
76
+ q -- quit
77
+ ```
78
+
79
+ ### Questions With Eager Shortcut Explanations
80
+
81
+ Providing an array of options to the options array will trigger the shortcut
82
+ explanation right after asking the question:
83
+
84
+ ```ruby
85
+
86
+ options_list = ["/some/path", "/some/other/path"]
87
+ iq = Interactive::Question.new do |q|
88
+ q.question = "Which path do you want to use?"
89
+ q.options = [options_list, :cancel]
90
+ end
91
+
92
+ iq.ask_and_wait_for_valid_response do |response|
93
+ if response.whole_number?
94
+ # response.to_i will convert the response string to an integer.
95
+ # useful for getting the index (i.e. options_list[response.to_i])
96
+ elsif response.cancel?
97
+ # do stuff to cancel...
98
+ end
99
+ end
100
+ ```
101
+
102
+ This will ask the question and show the explanation eagerly:
103
+
104
+ ```sh
105
+ Which path do you want to use? [0/1/c]
106
+ 0 -- /some/path
107
+ 1 -- /some/other/path
108
+ c -- cancel
76
109
  ```
77
110
 
78
111
  ## Development
@@ -4,7 +4,9 @@ module Interactive
4
4
  module_function
5
5
 
6
6
  def Option(option)
7
- if option.to_s.match(/^\d+$/)
7
+ if option.respond_to?(:to_hash)
8
+ @option = HashNumberedOption.new(option)
9
+ elsif option.to_s.match(/^\d+$/)
8
10
  @option = WholeNumberOption.new(option)
9
11
  else
10
12
  @option = WordOption.new(option)
@@ -26,6 +28,10 @@ module Interactive
26
28
  def query_method_name
27
29
  "whole_number_#{shortcut_value}?"
28
30
  end
31
+
32
+ def value
33
+ @option
34
+ end
29
35
  end
30
36
 
31
37
  class WordOption < SimpleDelegator
@@ -41,5 +47,28 @@ module Interactive
41
47
  def query_method_name
42
48
  "#{@option}?"
43
49
  end
50
+
51
+ def value
52
+ @option
53
+ end
54
+ end
55
+
56
+ class HashNumberedOption < SimpleDelegator
57
+ def initialize(option)
58
+ @option = option
59
+ super(@option)
60
+ end
61
+
62
+ def shortcut_value
63
+ @option.keys.first
64
+ end
65
+
66
+ def query_method_name
67
+ "whole_number_#{shortcut_value}?"
68
+ end
69
+
70
+ def value
71
+ @option[shortcut_value]
72
+ end
44
73
  end
45
74
  end
@@ -16,13 +16,25 @@ module Interactive
16
16
  end
17
17
 
18
18
  def shortcuts_meanings
19
- options.inject("") { |accum, opt| "#{accum} #{opt.shortcut_value} -- #{opt}\n"}
19
+ options.inject("") { |accum, opt| "#{accum} #{opt.shortcut_value} -- #{opt.value}\n"}
20
+ end
21
+
22
+ def has_hash?
23
+ @options.any? {|opt| opt.respond_to?(:to_hash) }
20
24
  end
21
25
 
22
26
  private
23
27
 
24
28
  def flatten_ranges(options)
25
- @options = options.inject([]) {|accum, opt| opt.respond_to?(:to_a) ? accum | opt.to_a : accum << opt}
29
+ @options = options.inject([]) do |accum, opt|
30
+ if opt.class == Range
31
+ accum | opt.to_a
32
+ elsif opt.respond_to?(:to_a)
33
+ accum | opt.map.with_index {|item, index| {index.to_s => item} }
34
+ else
35
+ accum << opt
36
+ end
37
+ end
26
38
  end
27
39
 
28
40
  def wrap_each_option
@@ -9,17 +9,12 @@ module Interactive
9
9
 
10
10
  raise ArgumentError, "question cannot be nil nor empty." if question.nil? || question.empty?
11
11
  raise ArgumentError, "options cannot be empty." if options.empty?
12
+
13
+ @question_type = @options.has_hash? ? QuestionWithEagerFullExplanation.new(self) : QuestionWithLazyFullExplanation.new(self)
12
14
  end
13
15
 
14
16
  def ask_and_wait_for_valid_response(&block)
15
- loop do
16
- puts "#{question} #{options.shortcuts_string}"
17
- resp = Interactive::Response.new(options)
18
- puts options.shortcuts_meanings if resp.invalid?
19
-
20
- yield resp
21
- break unless resp.invalid?
22
- end
17
+ @question_type.ask_and_wait_for_valid_response(&block)
23
18
  end
24
19
 
25
20
  private
@@ -0,0 +1,12 @@
1
+ class QuestionWithEagerFullExplanation < SimpleDelegator
2
+ def ask_and_wait_for_valid_response(&block)
3
+ loop do
4
+ puts "#{question} #{options.shortcuts_string}"
5
+ puts options.shortcuts_meanings
6
+ resp = Interactive::Response.new(options)
7
+
8
+ yield resp
9
+ break unless resp.invalid?
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,12 @@
1
+ class QuestionWithLazyFullExplanation < SimpleDelegator
2
+ def ask_and_wait_for_valid_response(&block)
3
+ loop do
4
+ puts "#{question} #{options.shortcuts_string}"
5
+ resp = Interactive::Response.new(options)
6
+ puts options.shortcuts_meanings if resp.invalid?
7
+
8
+ yield resp
9
+ break unless resp.invalid?
10
+ end
11
+ end
12
+ end
@@ -13,6 +13,10 @@ module Interactive
13
13
  define_whole_number
14
14
  end
15
15
 
16
+ def to_i
17
+ @_response.to_i
18
+ end
19
+
16
20
  private
17
21
 
18
22
  def check_validity
@@ -1,3 +1,3 @@
1
1
  module Interactive
2
- VERSION = "0.1.0"
2
+ VERSION = "0.2.0"
3
3
  end
data/lib/interactive.rb CHANGED
@@ -1,6 +1,8 @@
1
1
  require "interactive/version"
2
2
  require "interactive/option"
3
3
  require "interactive/options"
4
+ require "interactive/question_with_eager_full_explanation"
5
+ require "interactive/question_with_lazy_full_explanation"
4
6
  require 'interactive/question'
5
7
  require 'interactive/response'
6
8
 
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.1.0
4
+ version: 0.2.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-16 00:00:00.000000000 Z
11
+ date: 2015-03-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake
@@ -89,6 +89,8 @@ files:
89
89
  - lib/interactive/option.rb
90
90
  - lib/interactive/options.rb
91
91
  - lib/interactive/question.rb
92
+ - lib/interactive/question_with_eager_full_explanation.rb
93
+ - lib/interactive/question_with_lazy_full_explanation.rb
92
94
  - lib/interactive/response.rb
93
95
  - lib/interactive/version.rb
94
96
  homepage: http://github.com/Edderic/interactive