eac_ruby_utils 0.10.1 → 0.11.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
  SHA256:
3
- metadata.gz: 745395714bcf0d3d8679fb92457c99b22583c42aa3e1808bad1b109f3a8934dc
4
- data.tar.gz: 0a77a27b857495a4af57620b94ee65044bed25f642b2f3056410ef44466bbe07
3
+ metadata.gz: 36ad6529aeb2cd18a50fdd7ee4af8be6e3cd7157d41ff48352e5bc82b32fefc9
4
+ data.tar.gz: 32503898f9f75eebcd106a990ac11560b32ef0a6f6b2511e9a62e05bc0ce42e9
5
5
  SHA512:
6
- metadata.gz: 051cd01d18d22646d4c8d1688f755c2a9a3360deb1d650a770aca52daab3e6c84045f6f3d8378602db1781a28aab27545f3b332dba812856fc04848d891f11b6
7
- data.tar.gz: 01cc5a7fc5fbf3d0de63e24a7cc066c30ffa7b3bb0027f9469ecec8619196642e4809e7e531972e64cb3c6100ea494ddf2e223d0ad856ce1e59f9716de21cd2e
6
+ metadata.gz: b6e09038246c4300dc4c087cfbd8501f18e528bc988be179fd11ec7675a5dfa8ec0f04fb9e588265d46fa96fb76f5639b74eec4bb5e49f1f1b133213dfdd6841
7
+ data.tar.gz: cde052825e985ffb6be03b9aadae9344edf612536a248cb1cb5a57df8dc1226fee5df2c20e497d3913ed3ad33e39ff907155799a0b7303b5edc4e6cec500117e
@@ -0,0 +1,62 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'active_support/hash_with_indifferent_access'
4
+ require 'ostruct'
5
+
6
+ module EacRubyUtils
7
+ module Console
8
+ module Speaker
9
+ class List
10
+ class << self
11
+ def build(list)
12
+ return List.new(hash_to_values(list)) if list.is_a?(::Hash)
13
+ return List.new(array_to_values(list)) if list.is_a?(::Array)
14
+ raise "Invalid list: #{list} (#{list.class})"
15
+ end
16
+
17
+ private
18
+
19
+ def hash_to_values(list)
20
+ list.map { |key, value| ::OpenStruct.new(key: key, label: key, value: value) }
21
+ end
22
+
23
+ def array_to_values(list)
24
+ list.map { |value| ::OpenStruct.new(key: value, label: value, value: value) }
25
+ end
26
+ end
27
+
28
+ attr_reader :values
29
+
30
+ def initialize(values)
31
+ @values = values.map do |v|
32
+ ::OpenStruct.new(key: to_key(v.key), label: to_label(v.label), value: v.value)
33
+ end
34
+ end
35
+
36
+ def valid_labels
37
+ values.map(&:label)
38
+ end
39
+
40
+ def valid_value?(value)
41
+ values.any? { |v| v.key == to_key(value) }
42
+ end
43
+
44
+ def to_key(value)
45
+ to_label(value).downcase
46
+ end
47
+
48
+ def to_label(value)
49
+ value.to_s.strip
50
+ end
51
+
52
+ def build_value(value)
53
+ key = to_key(value)
54
+ values.each do |v|
55
+ return v.value if v.key == key
56
+ end
57
+ raise "Value not found: \"#{value}\" (#{values})"
58
+ end
59
+ end
60
+ end
61
+ end
62
+ end
@@ -2,6 +2,8 @@
2
2
 
3
3
  require 'colorize'
4
4
  require 'io/console'
5
+ require 'eac_ruby_utils/options_consumer'
6
+ require 'eac_ruby_utils/console/speaker/list'
5
7
 
6
8
  module EacRubyUtils
7
9
  module Console
@@ -41,13 +43,15 @@ module EacRubyUtils
41
43
  end
42
44
 
43
45
  def request_input(question, options = {})
44
- STDERR.write "#{question}: ".to_s.yellow
45
- if options[:noecho]
46
- r = STDIN.noecho(&:gets).chomp.strip
47
- STDERR.write("\n")
48
- r
46
+ options = ::EacRubyUtils::OptionsConsumer.new(options)
47
+ bool, list, noecho = options.consume_all(:bool, :list, :noecho)
48
+ options.validate
49
+ if list
50
+ request_from_list(question, list, noecho)
51
+ elsif bool
52
+ request_from_bool(question, noecho)
49
53
  else
50
- STDIN.gets.chomp.strip
54
+ request_string(question, noecho)
51
55
  end
52
56
  end
53
57
 
@@ -66,6 +70,46 @@ module EacRubyUtils
66
70
  def success(string)
67
71
  puts string.to_s.green
68
72
  end
73
+
74
+ private
75
+
76
+ def list_value(list, input)
77
+ values = list_values(list)
78
+ return input, true unless values
79
+ return input, false unless values.include?(input)
80
+ end
81
+
82
+ def list_values(list)
83
+ if list.is_a?(::Hash)
84
+ list.keys.map(&:to_s)
85
+ elsif list.is_a?(::Array)
86
+ list.map(&:to_s)
87
+ end
88
+ end
89
+
90
+ def request_from_bool(question, noecho)
91
+ request_from_list(question, { yes: true, y: true, no: false, n: false }, noecho)
92
+ end
93
+
94
+ def request_from_list(question, list, noecho)
95
+ list = ::EacRubyUtils::Console::Speaker::List.build(list)
96
+ loop do
97
+ input = request_string("#{question} [#{list.valid_labels.join('/')}]", noecho)
98
+ return list.build_value(input) if list.valid_value?(input)
99
+ warn "Invalid input: \"#{input}\" (Valid: #{list.valid_labels.join(', ')})"
100
+ end
101
+ end
102
+
103
+ def request_string(question, noecho)
104
+ STDERR.write "#{question}: ".to_s.yellow
105
+ if noecho
106
+ r = STDIN.noecho(&:gets).chomp.strip
107
+ STDERR.write("\n")
108
+ r
109
+ else
110
+ STDIN.gets.chomp.strip
111
+ end
112
+ end
69
113
  end
70
114
  end
71
115
  end
@@ -16,6 +16,10 @@ module EacRubyUtils
16
16
  value
17
17
  end
18
18
 
19
+ def consume_all(*keys)
20
+ keys.map { |key| consume(key) }
21
+ end
22
+
19
23
  def validate
20
24
  return if data.empty?
21
25
 
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ module EacRubyUtils
4
+ class << self
5
+ def patch(target, patch)
6
+ return if target.included_modules.include?(patch)
7
+ target.send(:include, patch)
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ module EacRubyUtils
4
+ class << self
5
+ def require_sub(file)
6
+ Dir["#{File.dirname(file)}/#{::File.basename(file, '.*')}/*.rb"].each do |path|
7
+ require path
8
+ end
9
+ end
10
+ end
11
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module EacRubyUtils
4
- VERSION = '0.10.1'
4
+ VERSION = '0.11.0'
5
5
  end
@@ -8,8 +8,10 @@ module EacRubyUtils
8
8
  require 'eac_ruby_utils/envs'
9
9
  require 'eac_ruby_utils/listable'
10
10
  require 'eac_ruby_utils/options_consumer'
11
+ require 'eac_ruby_utils/patch'
11
12
  require 'eac_ruby_utils/patches'
12
13
  require 'eac_ruby_utils/paths_hash'
14
+ require 'eac_ruby_utils/require_sub'
13
15
  require 'eac_ruby_utils/simple_cache'
14
16
  require 'eac_ruby_utils/yaml'
15
17
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: eac_ruby_utils
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.10.1
4
+ version: 0.11.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Esquilo Azul Company
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-08-11 00:00:00.000000000 Z
11
+ date: 2019-08-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -112,6 +112,7 @@ files:
112
112
  - lib/eac_ruby_utils/console/docopt_runner/_settings.rb
113
113
  - lib/eac_ruby_utils/console/docopt_runner/_subcommands.rb
114
114
  - lib/eac_ruby_utils/console/speaker.rb
115
+ - lib/eac_ruby_utils/console/speaker/list.rb
115
116
  - lib/eac_ruby_utils/contextualizable.rb
116
117
  - lib/eac_ruby_utils/envs.rb
117
118
  - lib/eac_ruby_utils/envs/base_env.rb
@@ -129,6 +130,7 @@ files:
129
130
  - lib/eac_ruby_utils/listable/string_list.rb
130
131
  - lib/eac_ruby_utils/listable/value.rb
131
132
  - lib/eac_ruby_utils/options_consumer.rb
133
+ - lib/eac_ruby_utils/patch.rb
132
134
  - lib/eac_ruby_utils/patches.rb
133
135
  - lib/eac_ruby_utils/patches/hash.rb
134
136
  - lib/eac_ruby_utils/patches/hash/options_consumer.rb
@@ -136,6 +138,7 @@ files:
136
138
  - lib/eac_ruby_utils/patches/object.rb
137
139
  - lib/eac_ruby_utils/patches/object/asserts.rb
138
140
  - lib/eac_ruby_utils/paths_hash.rb
141
+ - lib/eac_ruby_utils/require_sub.rb
139
142
  - lib/eac_ruby_utils/simple_cache.rb
140
143
  - lib/eac_ruby_utils/version.rb
141
144
  - lib/eac_ruby_utils/yaml.rb