eac_cli 0.40.1 → 0.41.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
  SHA256:
3
- metadata.gz: c774c31dedbbb2c69f387f8b31ae0f38e90687b077a32efdabf5a4a657a12353
4
- data.tar.gz: 87217fc7028a27c12a13045c87a5bd12c13f3acb183cf707b4c780dde8171ec4
3
+ metadata.gz: e47dbd4933f71d029f4fd1f19d84db730ecd3040c866c89725c6d02b81571fb3
4
+ data.tar.gz: 1b5093e85452bb32a228b94d4a71bc35fc0fd9c2f036d23d2c9f52e76b05047a
5
5
  SHA512:
6
- metadata.gz: 5feea87d16a816c2a4e886c34ff756e68d611df4637e4bb7deea35b95f7e800c08964d7369c40832368f1cfb583ab80a1f49c626b52ac81eb5807ee5a13ceff5
7
- data.tar.gz: 62153058635b53cd3fdda5406cf0d9e5c337fe8089c08232b1ee22672ceae3fa279e4ce95d370133f9bbc9d4d3bb5fa197c0232dad5ad701d362e2eb41c2d0d5
6
+ metadata.gz: 5eb9b2c2faa3b1e6beb70f09e16caaedf0061fbbd565c72d18228061896e0996d18f0375d3f569ca1ef3dc0ec5d333ea9157f44192c10a8981fa6e2ffaf8e086
7
+ data.tar.gz: b0120fe12a7dd9599a00f8288e7be2a6adde51796c4012f0a12b3df5f294c4bccd9bfb9e81becb8d562882b821e291906c9bbe010a076ee7d56fee57cf2ccb92
@@ -23,7 +23,13 @@ module EacCli
23
23
  # @param message [String]
24
24
  # @return [EacCli::RunnerWith::Confirmation::InputResult]
25
25
  def by_message(message)
26
- new(input(message, list: INPUT_LIST))
26
+ new(input_value_by_speaker(message))
27
+ end
28
+
29
+ # @param message [String]
30
+ # @return [String]
31
+ def input_value_by_speaker(message)
32
+ input(message, list: INPUT_LIST, ignore_case: false)
27
33
  end
28
34
  end
29
35
 
@@ -18,14 +18,14 @@ module EacCli
18
18
  end
19
19
  end
20
20
 
21
+ # @param message [String, nil]
22
+ # @return [Boolean]
21
23
  def confirm?(message = nil)
22
24
  return for_all_answers.fetch(message) if for_all_answers.key?(message)
23
25
  return false if parsed.no?
24
26
  return true if parsed.yes?
25
27
 
26
- r = confirm_input(message)
27
- for_all_answers[message] = r.for_all?
28
- r.confirm?
28
+ confirm_input_and_register(message)
29
29
  rescue ::EacCli::Speaker::InputRequested => e
30
30
  fatal_error e.message
31
31
  end
@@ -48,6 +48,14 @@ module EacCli
48
48
  )
49
49
  end
50
50
 
51
+ # @param message [String, nil]
52
+ # @return [Boolean]
53
+ def confirm_input_and_register(message)
54
+ r = confirm_input(message)
55
+ for_all_answers[message] = r.confirm? if r.for_all?
56
+ r.confirm?
57
+ end
58
+
51
59
  # @return [Hash<String, Boolean>]
52
60
  def for_all_answers_uncached
53
61
  {}
@@ -6,10 +6,14 @@ require 'ostruct'
6
6
  module EacCli
7
7
  class Speaker
8
8
  class List
9
+ VALUE_STRUCT = ::Struct.new(:key, :label, :value)
10
+
9
11
  class << self
10
- def build(list)
11
- return List.new(hash_to_values(list)) if list.is_a?(::Hash)
12
- return List.new(array_to_values(list)) if list.is_a?(::Array)
12
+ # @param list [Array, Hash]
13
+ # @param options [Hash]
14
+ def build(list, options = {})
15
+ return List.new(hash_to_values(list), options) if list.is_a?(::Hash)
16
+ return List.new(array_to_values(list), options) if list.is_a?(::Array)
13
17
 
14
18
  raise "Invalid list: #{list} (#{list.class})"
15
19
  end
@@ -17,22 +21,38 @@ module EacCli
17
21
  private
18
22
 
19
23
  def hash_to_values(list)
20
- list.map { |key, value| ::OpenStruct.new(key: key, label: key, value: value) } # rubocop:disable Style/OpenStructUse
24
+ list.map { |key, value| VALUE_STRUCT.new(key, key, value) }
21
25
  end
22
26
 
23
27
  def array_to_values(list)
24
- list.map { |value| ::OpenStruct.new(key: value, label: value, value: value) } # rubocop:disable Style/OpenStructUse
28
+ list.map { |value| VALUE_STRUCT.new(value, value, value) }
25
29
  end
26
30
  end
27
31
 
28
- attr_reader :values
32
+ DEFAULT_IGNORE_CASE = true
33
+
34
+ enable_listable
35
+ lists.add_symbol :option, :ignore_case
29
36
 
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) # rubocop:disable Style/OpenStructUse
37
+ # @!attribute [r] values
38
+ # @return [Array<VALUE_STRUCT>]
39
+ # # @!attribute [r] options
40
+ # @return [Hash]
41
+ # @!method initialize(values)
42
+ # @param values [Array<VALUE_STRUCT>]
43
+ # @param options [Hash]
44
+ common_constructor :values, :options, default: [{}] do
45
+ self.options = self.class.lists.option.hash_keys_validate!(options)
46
+ self.values = values.map do |v|
47
+ VALUE_STRUCT.new(to_key(v.key), to_label(v.label), v.value)
33
48
  end
34
49
  end
35
50
 
51
+ # @return [Boolean]
52
+ def ignore_case?
53
+ options.if_key(OPTION_IGNORE_CASE, DEFAULT_IGNORE_CASE, &:to_bool)
54
+ end
55
+
36
56
  def valid_labels
37
57
  values.map(&:label)
38
58
  end
@@ -41,8 +61,11 @@ module EacCli
41
61
  values.any? { |v| v.key == to_key(value) }
42
62
  end
43
63
 
64
+ # @param value [Object]
65
+ # @return [String]
44
66
  def to_key(value)
45
- to_label(value).downcase
67
+ r = to_label(value)
68
+ ignore_case? ? r.downcase : r
46
69
  end
47
70
 
48
71
  def to_label(value)
@@ -7,7 +7,8 @@ module EacCli
7
7
  module Options
8
8
  common_concern do
9
9
  enable_listable
10
- lists.add_symbol :option, :out_out, :err_out, :in_in, :parent, :err_line_prefix
10
+ lists.add_symbol :option, :out_out, :err_out, :in_in, :parent, :err_line_prefix,
11
+ :ignore_case
11
12
  end
12
13
 
13
14
  def err_out
@@ -22,6 +23,11 @@ module EacCli
22
23
  option(OPTION_IN_IN, $stdin)
23
24
  end
24
25
 
26
+ # @return [Boolean]
27
+ def ignore_case
28
+ option(OPTION_IGNORE_CASE, nil)
29
+ end
30
+
25
31
  def err_line_prefix
26
32
  option(OPTION_ERR_LINE_PREFIX, '')
27
33
  end
@@ -0,0 +1,43 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'eac_cli/speaker/list'
4
+ require 'eac_ruby_utils/core_ext'
5
+
6
+ module EacCli
7
+ class Speaker
8
+ class RequestFromList
9
+ acts_as_instance_method
10
+ enable_simple_cache
11
+ common_constructor :speaker, :question, :list_values, :noecho,
12
+ :ignore_case
13
+
14
+ # @return [String]
15
+ def result
16
+ loop do
17
+ return list.build_value(input) if list.valid_value?(input)
18
+
19
+ speaker.warn "Invalid input: \"#{input}\" (Valid: #{list.valid_labels.join(', ')})"
20
+ end
21
+ end
22
+
23
+ protected
24
+
25
+ # @return [String]
26
+ def input_uncached
27
+ speaker.send(
28
+ :request_string,
29
+ "#{question} [#{list.valid_labels.join('/')}]",
30
+ noecho
31
+ )
32
+ end
33
+
34
+ # @return [EacCli::Speaker::List]
35
+ def list_uncached
36
+ list_options = {}
37
+ list_options[::EacCli::Speaker::List::OPTION_IGNORE_CASE] = ignore_case unless
38
+ ignore_case.nil?
39
+ ::EacCli::Speaker::List.build(list_values, list_options)
40
+ end
41
+ end
42
+ end
43
+ end
@@ -52,9 +52,10 @@ module EacCli
52
52
  # +list+ ([Hash] or [Array], default: +nil+): requires a answer from a list.
53
53
  # +noecho+ ([Boolean], default: +false+): does not output answer.
54
54
  def input(question, options = {})
55
- bool, list, noecho = options.to_options_consumer.consume_all(:bool, :list, :noecho)
55
+ bool, list, noecho, ignore_case =
56
+ options.to_options_consumer.consume_all(:bool, :list, :noecho, :ignore_case)
56
57
  if list
57
- request_from_list(question, list, noecho)
58
+ request_from_list(question, list, noecho, ignore_case)
58
59
  elsif bool
59
60
  request_from_bool(question, noecho)
60
61
  else
@@ -96,17 +97,7 @@ module EacCli
96
97
  end
97
98
 
98
99
  def request_from_bool(question, noecho)
99
- request_from_list(question, { yes: true, y: true, no: false, n: false }, noecho)
100
- end
101
-
102
- def request_from_list(question, list, noecho)
103
- list = ::EacCli::Speaker::List.build(list)
104
- loop do
105
- input = request_string("#{question} [#{list.valid_labels.join('/')}]", noecho)
106
- return list.build_value(input) if list.valid_value?(input)
107
-
108
- warn "Invalid input: \"#{input}\" (Valid: #{list.valid_labels.join(', ')})"
109
- end
100
+ request_from_list(question, { yes: true, y: true, no: false, n: false }, noecho, true)
110
101
  end
111
102
 
112
103
  def request_string(question, noecho)
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module EacCli
4
- VERSION = '0.40.1'
4
+ VERSION = '0.41.0'
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: eac_cli
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.40.1
4
+ version: 0.41.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: 2024-02-17 00:00:00.000000000 Z
11
+ date: 2024-05-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: colorize
@@ -178,6 +178,7 @@ files:
178
178
  - lib/eac_cli/speaker/input_requested.rb
179
179
  - lib/eac_cli/speaker/list.rb
180
180
  - lib/eac_cli/speaker/options.rb
181
+ - lib/eac_cli/speaker/request_from_list.rb
181
182
  - lib/eac_cli/version.rb
182
183
  homepage:
183
184
  licenses: []