eac_cli 0.40.1 → 0.42.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: c774c31dedbbb2c69f387f8b31ae0f38e90687b077a32efdabf5a4a657a12353
4
- data.tar.gz: 87217fc7028a27c12a13045c87a5bd12c13f3acb183cf707b4c780dde8171ec4
3
+ metadata.gz: 37dc288478a862aec6a0c885cdc8a36a61b25c706ee3da6b448985a3bcbf1286
4
+ data.tar.gz: 66c5be70d7e1ef6ee34c35e308f5d7356b0551289301a506675e61f1048bf279
5
5
  SHA512:
6
- metadata.gz: 5feea87d16a816c2a4e886c34ff756e68d611df4637e4bb7deea35b95f7e800c08964d7369c40832368f1cfb583ab80a1f49c626b52ac81eb5807ee5a13ceff5
7
- data.tar.gz: 62153058635b53cd3fdda5406cf0d9e5c337fe8089c08232b1ee22672ceae3fa279e4ce95d370133f9bbc9d4d3bb5fa197c0232dad5ad701d362e2eb41c2d0d5
6
+ metadata.gz: 347342aa9fe0242a27aaf02fc209709e924a62fea1dcb452ddaae22ed186c01f59c0ec7b4bc0d9ad112b95618d563e5e4b636b4460385716ec8100ae3e72fd72
7
+ data.tar.gz: 5290b28584f6e1d139a89a5be84180877cf3356b0726593bf7e858192840ae494370a3b8fde58a7ca99a4984dec5df85564072a409fb2f1d84177b1a8d36f2b2
@@ -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
  {}
@@ -30,6 +30,17 @@ module EacCli
30
30
  def build_rows
31
31
  rows.map { |row| build_row(row) }
32
32
  end
33
+
34
+ # @param row [Object]
35
+ # @param column [String]
36
+ # @return [Object]
37
+ def build_value(row, column)
38
+ return row.send(column) if row.respond_to?(column)
39
+ return row.with_indifferent_access.fetch(column) if row.is_a?(::Hash)
40
+
41
+ raise ::ArgumentError, "Row \"#{row}\" do not respond to column \"#{column}\" neither " \
42
+ "is a Hash (Row: #{row}, Column: #{column})"
43
+ end
33
44
  end
34
45
  end
35
46
  end
@@ -10,7 +10,7 @@ module EacCli
10
10
  class CsvFormatter < ::EacCli::RunnerWith::OutputList::BaseFormatter
11
11
  # @return [Array]
12
12
  def build_row(row)
13
- build_columns.map { |c| row.send(c) }
13
+ build_columns.map { |c| build_value(row, c) }
14
14
  end
15
15
 
16
16
  # @return [String]
@@ -11,7 +11,7 @@ module EacCli
11
11
  # @param row [Object]
12
12
  # @return [Array]
13
13
  def build_row(row)
14
- build_columns.map { |c| row.send(c) }
14
+ build_columns.map { |c| build_value(row, c) }
15
15
  end
16
16
 
17
17
  # @return [String]
@@ -10,7 +10,7 @@ module EacCli
10
10
  class YamlFormatter < ::EacCli::RunnerWith::OutputList::BaseFormatter
11
11
  # @return [Hash<String, String>]
12
12
  def build_row(row)
13
- build_columns.inject({}) { |a, e| a.merge(e => row.send(e).to_s) }
13
+ build_columns.inject({}) { |a, e| a.merge(e => build_value(row, e).to_s) }
14
14
  end
15
15
 
16
16
  # @return [String]
@@ -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.42.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.42.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-06-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: colorize
@@ -56,14 +56,14 @@ dependencies:
56
56
  requirements:
57
57
  - - "~>"
58
58
  - !ruby/object:Gem::Version
59
- version: '0.121'
59
+ version: '0.122'
60
60
  type: :runtime
61
61
  prerelease: false
62
62
  version_requirements: !ruby/object:Gem::Requirement
63
63
  requirements:
64
64
  - - "~>"
65
65
  - !ruby/object:Gem::Version
66
- version: '0.121'
66
+ version: '0.122'
67
67
  - !ruby/object:Gem::Dependency
68
68
  name: tty-table
69
69
  requirement: !ruby/object:Gem::Requirement
@@ -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: []