eac_ruby_utils 0.14.0 → 0.15.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: cbc288c5822f1c0981eb007a0ac8398e0d06e8355b39a1f2aa7a5dbf12fb9e2b
4
- data.tar.gz: 7aa562293120d2e8da628bf6d471f7ec23416c9b49f03b8a2205881c6fd166c1
3
+ metadata.gz: b0ddfde05215993f63e4489e1b54cb4434d0dce6f9c7d401d985c229461ca106
4
+ data.tar.gz: 7f06d22a45ed253237e0033da8e07f90a327858352bc1cd99ac8396127a094af
5
5
  SHA512:
6
- metadata.gz: a30eed73757ef9d54ee35f35f1b9753c43b543959760c2fe1320dd8fb81b0f30a912b991b0066d8b6cfc9d062a0d65a7bc31325d46067c066ac876a6cf8863b5
7
- data.tar.gz: 48d825385999066c7b1b366e5f47d934069e5b8f7fb272e4a5805645bbf4a2c88c4506be462666e204a993b276423c1867f8e9e30b23f710d80c9c5d7d6fe3f8
6
+ metadata.gz: d28f2608835e09ce813974309bbb87742118f71d294e907699575d99ed422a33d87d0af0528c8bdc8f200141fee74db6a9b54799fa55dd52c21c37981995d103
7
+ data.tar.gz: 507b28c0c90e84105ab30b62038857ea01b4f8b46de4d012a41e58b8fb31f63dce03f22ce3cb0a7387b77b7c4bd7c74f96f8262df6185e7d13d8284d45e966e0
@@ -0,0 +1,48 @@
1
+ # frozen_string_literal: true
2
+
3
+ module EacRubyUtils
4
+ class CommonConstructor
5
+ attr_reader :args, :options
6
+
7
+ def initialize(*args)
8
+ @args = args
9
+ end
10
+
11
+ def setup_class(klass)
12
+ setup_class_attr_readers(klass)
13
+ setup_class_attr_writers(klass)
14
+ setup_class_initialize(klass)
15
+ end
16
+
17
+ def setup_class_attr_readers(klass)
18
+ klass.send(:attr_reader, *args)
19
+ klass.send(:public, *args)
20
+ end
21
+
22
+ def setup_class_attr_writers(klass)
23
+ klass.send(:attr_writer, *args)
24
+ klass.send(:private, *args.map { |a| "#{a}=" })
25
+ end
26
+
27
+ def setup_class_initialize(klass)
28
+ klass.class_eval initialize_method_code, __FILE__, __LINE__
29
+ end
30
+
31
+ def initialize_method_code
32
+ b = "def initialize(#{initialize_method_args_code})\n"
33
+ initialize_method_args.each do |arg|
34
+ b += " self.#{arg} = #{arg}\n"
35
+ end
36
+ b += "end\n"
37
+ b
38
+ end
39
+
40
+ def initialize_method_args_code
41
+ initialize_method_args.join(', ')
42
+ end
43
+
44
+ def initialize_method_args
45
+ args
46
+ end
47
+ end
48
+ end
@@ -17,6 +17,7 @@ module EacRubyUtils
17
17
 
18
18
  def pop
19
19
  return nodes_stack.pop if nodes_stack.count > 1
20
+
20
21
  raise "Cannot remove first node (nodes_stack.count: #{nodes_stack.count})"
21
22
  end
22
23
 
@@ -2,7 +2,7 @@
2
2
 
3
3
  require 'colorize'
4
4
  require 'io/console'
5
- require 'eac_ruby_utils/options_consumer'
5
+ require 'eac_ruby_utils/patches/hash/options_consumer'
6
6
  require 'eac_ruby_utils/console/speaker/_class_methods'
7
7
  require 'eac_ruby_utils/console/speaker/list'
8
8
  require 'eac_ruby_utils/console/speaker/node'
@@ -44,10 +44,12 @@ module EacRubyUtils
44
44
  puts string.to_s.yellow
45
45
  end
46
46
 
47
+ # Options:
48
+ # +bool+ ([Boolean], default: +false+): requires a answer "yes" or "no".
49
+ # +list+ ([Hash] or [Array], default: +nil+): requires a answer from a list.
50
+ # +noecho+ ([Boolean], default: +false+): does not output answer.
47
51
  def request_input(question, options = {})
48
- options = ::EacRubyUtils::OptionsConsumer.new(options)
49
- bool, list, noecho = options.consume_all(:bool, :list, :noecho)
50
- options.validate
52
+ bool, list, noecho = options.to_options_consumer.consume_all(:bool, :list, :noecho)
51
53
  if list
52
54
  request_from_list(question, list, noecho)
53
55
  elsif bool
@@ -105,13 +107,17 @@ module EacRubyUtils
105
107
 
106
108
  def request_string(question, noecho)
107
109
  current_node.stderr.write "#{question}: ".to_s.yellow
108
- if noecho
109
- r = current_node.stdin.noecho(&:gets).chomp.strip
110
- current_node.stderr.write("\n")
111
- r
112
- else
113
- current_node.stdin.gets.chomp.strip
114
- end
110
+ noecho ? request_string_noecho : request_string_echo
111
+ end
112
+
113
+ def request_string_noecho
114
+ r = current_node.stdin.noecho(&:gets).chomp.strip
115
+ current_node.stderr.write("\n")
116
+ r
117
+ end
118
+
119
+ def request_string_echo
120
+ current_node.stdin.gets.chomp.strip
115
121
  end
116
122
 
117
123
  def current_node
@@ -1,9 +1,12 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'active_support/core_ext/hash/indifferent_access'
4
+ require 'ostruct'
4
5
 
5
6
  module EacRubyUtils
6
7
  class OptionsConsumer
8
+ DEFAULT_OPTIONS = { validate: true, ostruct: false }.with_indifferent_access.freeze
9
+
7
10
  def initialize(data)
8
11
  @data = data.with_indifferent_access
9
12
  end
@@ -16,8 +19,16 @@ module EacRubyUtils
16
19
  value
17
20
  end
18
21
 
22
+ # If last argument is a Hash it is used a options.
23
+ # Options:
24
+ # * +validate+: validate after consume.
25
+ # * +ostruct+: return a [OpenStruct] instead a [Hash].
26
+ # @return [Hash] (Default) or [OpenStruct].
19
27
  def consume_all(*keys)
20
- keys.map { |key| consume(key) }
28
+ options = consume_all_extract_options(keys)
29
+ result = consume_all_build_result(keys, options.fetch(:ostruct))
30
+ validate if options.fetch(:validate)
31
+ result
21
32
  end
22
33
 
23
34
  def validate
@@ -33,5 +44,19 @@ module EacRubyUtils
33
44
  private
34
45
 
35
46
  attr_reader :data
47
+
48
+ def consume_all_extract_options(keys)
49
+ options = DEFAULT_OPTIONS
50
+ options = options.merge(keys.pop.with_indifferent_access) if keys.last.is_a?(Hash)
51
+ options
52
+ end
53
+
54
+ def consume_all_build_result(keys, ostruct)
55
+ if ostruct
56
+ ::OpenStruct.new(keys.map { |key| [key, consume(key)] }.to_h)
57
+ else
58
+ keys.map { |key| consume(key) }
59
+ end
60
+ end
36
61
  end
37
62
  end
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'eac_ruby_utils/common_constructor'
4
+
5
+ class Class
6
+ def common_constructor(*args)
7
+ ::EacRubyUtils::CommonConstructor.new(*args).setup_class(self)
8
+ end
9
+ end
@@ -0,0 +1,4 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'eac_ruby_utils/require_sub'
4
+ ::EacRubyUtils.require_sub(__FILE__)
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module EacRubyUtils
4
- VERSION = '0.14.0'
4
+ VERSION = '0.15.0'
5
5
  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.14.0
4
+ version: 0.15.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-10-06 00:00:00.000000000 Z
11
+ date: 2019-10-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -117,6 +117,7 @@ files:
117
117
  - README.rdoc
118
118
  - lib/eac_ruby_utils.rb
119
119
  - lib/eac_ruby_utils/arguments_consumer.rb
120
+ - lib/eac_ruby_utils/common_constructor.rb
120
121
  - lib/eac_ruby_utils/configs.rb
121
122
  - lib/eac_ruby_utils/console.rb
122
123
  - lib/eac_ruby_utils/console/configs.rb
@@ -152,6 +153,8 @@ files:
152
153
  - lib/eac_ruby_utils/options_consumer.rb
153
154
  - lib/eac_ruby_utils/patch.rb
154
155
  - lib/eac_ruby_utils/patches.rb
156
+ - lib/eac_ruby_utils/patches/class.rb
157
+ - lib/eac_ruby_utils/patches/class/common_constructor.rb
155
158
  - lib/eac_ruby_utils/patches/hash.rb
156
159
  - lib/eac_ruby_utils/patches/hash/options_consumer.rb
157
160
  - lib/eac_ruby_utils/patches/hash/sym_keys_hash.rb