easy_app_helper 4.1.2 → 4.2.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: a8ad72833020b17d6a18aa5d80a3ec6b39752f1d
4
- data.tar.gz: 27b76179f874b068bce02b666b9877096f67db44
3
+ metadata.gz: 44b64f48b34bca054b777aa595b25223bf8841e5
4
+ data.tar.gz: e9ee7e8d49bd806b54be25b6a309e3def700be8a
5
5
  SHA512:
6
- metadata.gz: ffee1008b3e25274dc63b75946723f24d28f9a8b0f26c510c04b8741b7c783d4d0093b5c4cecafb8499a1c6a41f8eaaa67cbe3712a414a3e45174df0a97aefb6
7
- data.tar.gz: 011cd2a385b3a3e3548df86174f876c05ff5c1e8b26df392a43e4498fdbddacdb5674c69d6ba8feb28dce601f33b2dde4dbd88fe817bf5912eacae651ae41bb1
6
+ metadata.gz: f1b76ac90c234ad895e7f4b1c4be83ab04e14b88e6e94876a4d9b3a8ee2ae15d5d7cda8f617677f8f34eeaa125a54f88e35ec406b18536d99c64ed845436f93e
7
+ data.tar.gz: d35c30b487b3c0ed747216f20d07b4279f34eff95635e9255a64de4dee9c3951d584d8278dad274f9d76ce77ad7d98153c461dc86b18949d6a2c821122ed21ea
@@ -11,6 +11,7 @@ require 'easy_app_helper/logger/wrapper'
11
11
  require 'easy_app_helper/managed_logger'
12
12
 
13
13
  require 'easy_app_helper/processes'
14
+ require 'easy_app_helper/input'
14
15
 
15
16
 
16
17
  module EasyAppHelper
@@ -0,0 +1,56 @@
1
+ module EasyAppHelper
2
+
3
+ module Input
4
+
5
+ DEFAULT_CONFIRMATION_CHOICES = {
6
+ true => %w(Yes y),
7
+ false => %w(No n)
8
+ }
9
+
10
+ def get_user_confirmation(choices: DEFAULT_CONFIRMATION_CHOICES,
11
+ default_choice: 'No',
12
+ prompt: 'Are you sure ?',
13
+ strict: false)
14
+
15
+ raise 'Invalid choices !' unless choices.is_a? Hash
16
+ values = choices.values.flatten
17
+ raise "Invalid default choice '#{default_choice}' !" unless values.include? default_choice
18
+ return true if EasyAppHelper.config[:auto]
19
+ full_prompt = '%s (%s): ' % [prompt, choices_string(values, default_choice)]
20
+ STDOUT.print full_prompt
21
+ STDOUT.flush
22
+ input = nil
23
+ until values.include? input
24
+ input = STDIN.gets.chomp
25
+ input = default_choice if input.nil? || input.empty?
26
+ unless strict
27
+ input = default_choice unless values.include? input
28
+ end
29
+ end
30
+ choices.each_pair do |res, possible_choices|
31
+ return res if possible_choices.include? input
32
+ end
33
+ raise 'Something wrong happened !'
34
+ end
35
+
36
+
37
+ def get_user_input(prompt, default=nil)
38
+ full_prompt = (default.nil? or default.empty?) ? "#{prompt}: " : "#{prompt} (default: #{default}): "
39
+ STDOUT.print full_prompt
40
+ STDOUT.flush
41
+ STDIN.gets.chomp
42
+ end
43
+
44
+ private
45
+
46
+
47
+ def choices_string(choices, default_choice, highlight= %w([ ]))
48
+ choices
49
+ .map { |choice| choice == default_choice ? "#{highlight.first}#{choice}#{highlight.last}" : choice }
50
+ .join '/'
51
+ end
52
+
53
+
54
+ end
55
+
56
+ end
@@ -1,3 +1,3 @@
1
1
  module EasyAppHelper
2
- VERSION = '4.1.2'
2
+ VERSION = '4.2.0'
3
3
  end
@@ -0,0 +1,55 @@
1
+ require 'spec_helper'
2
+
3
+
4
+ describe EasyAppHelper::Input do
5
+
6
+ subject {
7
+ o = Object.new.extend EasyAppHelper::Input
8
+ o
9
+ }
10
+
11
+ it 'should prompt user for confirmation' do
12
+ described_class::DEFAULT_CONFIRMATION_CHOICES.each_pair do |expected_result, choices|
13
+ choices.each do |choice|
14
+ expect(STDOUT).to receive(:print)
15
+ expect(STDIN).to receive(:gets).and_return(choice)
16
+ expect(subject.get_user_confirmation).to be expected_result
17
+ end
18
+ end
19
+ end
20
+
21
+ context 'when not in strict mode' do
22
+
23
+ it 'should default to default choice if bullshit is entered' do
24
+ expect(STDOUT).to receive(:print)
25
+ expect(STDIN).to receive(:gets).and_return('bullshit')
26
+ expect(subject.get_user_confirmation).to be false
27
+ end
28
+
29
+ end
30
+
31
+ context 'when in strict mode' do
32
+
33
+ it 'should re-ask until bullshit is not entered' do
34
+ expect(STDOUT).to receive(:print)
35
+ expect(STDIN).to receive(:gets).and_return('bullshit')
36
+ expect(STDIN).to receive(:gets).and_return('bullshit')
37
+ expect(STDIN).to receive(:gets).and_return('bullshit')
38
+ expect(STDIN).to receive(:gets).and_return('y')
39
+ expect(subject.get_user_confirmation strict: true).to be true
40
+ end
41
+
42
+ end
43
+
44
+ context 'when user passes --auto to command line' do
45
+
46
+ it 'should bypass the question and return true' do
47
+ expect(EasyAppHelper.config).to receive(:[]).with(:auto).and_return(true)
48
+ expect(subject.get_user_confirmation).to be true
49
+ end
50
+
51
+
52
+ end
53
+
54
+
55
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: easy_app_helper
3
3
  version: !ruby/object:Gem::Version
4
- version: 4.1.2
4
+ version: 4.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - L.Briais
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-09-18 00:00:00.000000000 Z
11
+ date: 2016-09-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -113,6 +113,7 @@ files:
113
113
  - lib/easy_app_helper/config.rb
114
114
  - lib/easy_app_helper/config/compatibility.rb
115
115
  - lib/easy_app_helper/config/initializer.rb
116
+ - lib/easy_app_helper/input.rb
116
117
  - lib/easy_app_helper/logger/initializer.rb
117
118
  - lib/easy_app_helper/logger/wrapper.rb
118
119
  - lib/easy_app_helper/managed_logger.rb
@@ -136,6 +137,7 @@ files:
136
137
  - lib/tasks/template_manager.rb
137
138
  - spec/config_spec.rb
138
139
  - spec/easy_app_helper_spec.rb
140
+ - spec/input_spec.rb
139
141
  - spec/logger_spec.rb
140
142
  - spec/processes/base_spec.rb
141
143
  - spec/spec_helper.rb
@@ -175,6 +177,7 @@ summary: Provides cool helpers to your application, including configuration and
175
177
  test_files:
176
178
  - spec/config_spec.rb
177
179
  - spec/easy_app_helper_spec.rb
180
+ - spec/input_spec.rb
178
181
  - spec/logger_spec.rb
179
182
  - spec/processes/base_spec.rb
180
183
  - spec/spec_helper.rb