eac_ruby_utils 0.49.0 → 0.52.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: 79201cf9333f8dd3efce24ef1a00fe30c90a8c5c96c549c77db9d6f875009786
4
- data.tar.gz: e55dd71229d8047e6be0d1ad8b08b0bc92bb97dff785371a92803ccbe13568ba
3
+ metadata.gz: d1cd6f8891c93e7a6a866a11fa705c3ce0609b5b0d6b5e5468d73a46ed135121
4
+ data.tar.gz: 49a544fac4a944b2c58ad8cbd0f06eeec6a3e9e5e1df4a66872bdd34bf79c1ef
5
5
  SHA512:
6
- metadata.gz: 8e6761d989f6f31c684403237f53066a59d7990b5a6b081084bb7e82740cc94a259e605250b45190759afd1ae9e6963e1b3c200f65439062b678e8249f8e1a21
7
- data.tar.gz: 6c70b64908695c5b273c32a2b2413ee5884173b1c81c02aa7e5951951664afe223b8cd09f6e17f335ea284660c4f3800444b9c44914579aea18e7aa9c32e5763
6
+ metadata.gz: 00e8db1cb8da1ed4b54aee6e704820b8962b22e5f9bdb0321354db5ed044cd1799024775c8704765a25d302d2cce776de2511e511665b5d55d00150c3e897271
7
+ data.tar.gz: 8da830a3af85dc78d88b919645522cdec79e52c61086517b35bb9a64a14e5f9d4bc2af067f9cef31153268e1b1522b7adbad3e0507d04a62fcd2255e9f090d2f
@@ -0,0 +1,59 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'eac_ruby_utils/patches/module/common_concern'
4
+
5
+ module EacRubyUtils
6
+ # Support to abstract methods.
7
+ #
8
+ # Usage:
9
+ #
10
+ # require 'eac_ruby_utils/abstract_methods'
11
+ #
12
+ # class BaseClass
13
+ # include EacRubyUtils::AbstractMethods
14
+ #
15
+ # abstract_methods :mymethod
16
+ # end
17
+ #
18
+ # BaseClass.new.mymethod # raise "Abstract method: mymethod"
19
+ #
20
+ # class SubClass
21
+ # def mymethod
22
+ # "Implemented"
23
+ # end
24
+ # end
25
+ #
26
+ # SubClass.new.mymethod # return "Implemented"
27
+ module AbstractMethods
28
+ common_concern
29
+
30
+ module ClassMethods
31
+ def abstract_methods(*methods_names)
32
+ @abstract_methods ||= Set.new
33
+ @abstract_methods += methods_names.map(&:to_sym)
34
+
35
+ @abstract_methods.to_enum
36
+ end
37
+ end
38
+
39
+ module InstanceMethods
40
+ def respond_to_missing?(method_name, include_private = false)
41
+ super || abstract_method?(method_name)
42
+ end
43
+
44
+ def method_missing(method_name, *arguments, &block)
45
+ raise_abstract_method(method_name) if abstract_method?(method_name)
46
+
47
+ super
48
+ end
49
+
50
+ def abstract_method?(method_name)
51
+ self.class.abstract_methods.include?(method_name.to_sym)
52
+ end
53
+
54
+ def raise_abstract_method(method_name)
55
+ raise ::NoMethodError, "Abstract method \"#{method_name}\" hit in \"#{self}\""
56
+ end
57
+ end
58
+ end
59
+ end
@@ -1,31 +1,20 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'eac_ruby_utils/common_constructor'
4
3
  require 'eac_ruby_utils/configs'
5
- require 'eac_ruby_utils/console/speaker'
6
- require 'eac_ruby_utils/options_consumer'
7
- require 'eac_ruby_utils/patches/object/asserts'
8
- require 'eac_ruby_utils/simple_cache'
4
+ require 'eac_ruby_utils/core_ext'
9
5
 
10
6
  module EacRubyUtils
11
7
  module Console
12
8
  class Configs
13
- include ::EacRubyUtils::Console::Speaker
9
+ require_sub __FILE__
10
+ enable_console_speaker
14
11
 
15
12
  class << self
16
13
  def entry_key_to_envvar_name(entry_key)
17
- path = if entry_key.is_a?(::Array)
18
- entry_key
19
- else
20
- ::EacRubyUtils::PathsHash.parse_entry_key(entry_key)
21
- end
22
- path.join('_').gsub(/[^a-z0-9_]/i, '').gsub(/\A_+/, '').gsub(/_+\z/, '')
23
- .gsub(/_{2,}/, '_').upcase
14
+ ::EacRubyUtils::Console::Configs::EntryReader.entry_key_to_envvar_name(entry_key)
24
15
  end
25
16
  end
26
17
 
27
- STORE_PASSWORDS_KEY = 'core.store_passwords'
28
-
29
18
  attr_reader :configs
30
19
 
31
20
  def initialize(configs_key, options = {})
@@ -34,104 +23,15 @@ module EacRubyUtils
34
23
  end
35
24
 
36
25
  def read_password(entry_key, options = {})
37
- options = options.merge(noecho: true)
38
- if store_passwords?
39
- read_entry(entry_key, options)
40
- else
41
- looped_entry_value_from_input(entry_key, options)
42
- end
26
+ ::EacRubyUtils::Console::Configs::PasswordEntryReader.new(self, entry_key, options).read
43
27
  end
44
28
 
45
29
  def read_entry(entry_key, options = {})
46
- options = ReadEntryOptions.new(options)
47
- unless options[:noenv]
48
- envvar_value = envvar_read_entry(entry_key)
49
- return envvar_value if envvar_value.present?
50
- end
51
- stored_value = configs.read_entry(entry_key)
52
- return stored_value if stored_value
53
- return read_entry_from_console(entry_key, options) unless options[:noinput]
54
-
55
- raise "No value found for entry \"#{entry_key}\"" if options[:required]
30
+ ::EacRubyUtils::Console::Configs::EntryReader.new(self, entry_key, options).read
56
31
  end
57
32
 
58
33
  def store_passwords?
59
- read_entry(
60
- STORE_PASSWORDS_KEY,
61
- before_input: -> { store_password_banner },
62
- validator: ->(entry_value) { %w[yes no].include?(entry_value) }
63
- ) == 'yes'
64
- end
65
-
66
- protected
67
-
68
- def envvar_read_entry(entry_key)
69
- env_entry_key = self.class.entry_key_to_envvar_name(entry_key)
70
- return nil unless ENV.key?(env_entry_key)
71
-
72
- ENV.fetch(env_entry_key).if_present(::EacRubyUtils::BlankNotBlank.instance)
73
- end
74
-
75
- def read_entry_from_console(entry_key, options)
76
- options[:before_input].call if options[:before_input].present?
77
- entry_value = looped_entry_value_from_input(entry_key, options)
78
- configs.write_entry(entry_key, entry_value)
79
- entry_value
80
- end
81
-
82
- def store_password_banner
83
- infom 'Do you wanna to store passwords?'
84
- infom "Warning: the passwords will be store in clear text in \"#{configs.storage_path}\""
85
- infom 'Enter "yes" or "no"'
86
- end
87
-
88
- def looped_entry_value_from_input(entry_key, options)
89
- loop do
90
- entry_value = entry_value_from_input(entry_key, options)
91
- next if entry_value.blank?
92
- next if options[:validator] && !options[:validator].call(entry_value)
93
-
94
- return entry_value
95
- end
96
- end
97
-
98
- def entry_value_from_input(entry_key, options)
99
- entry_value = request_input("Value for entry \"#{entry_key}\"",
100
- options.request_input_options)
101
- warn('Entered value is blank') if entry_value.blank?
102
- entry_value
103
- end
104
-
105
- class ReadEntryOptions
106
- include ::EacRubyUtils::SimpleCache
107
- ::EacRubyUtils::CommonConstructor.new(:options).setup_class(self)
108
-
109
- DEFAULT_VALUES = {
110
- before_input: nil, bool: false, list: false, noecho: false, noenv: false, noinput: false,
111
- required: true, validator: nil
112
- }.freeze
113
-
114
- def [](key)
115
- values.fetch(key.to_sym)
116
- end
117
-
118
- def request_input_options
119
- values.slice(:bool, :list, :noecho)
120
- end
121
-
122
- private
123
-
124
- def values_uncached
125
- consumer = ::EacRubyUtils::OptionsConsumer.new(options)
126
- r = {}
127
- DEFAULT_VALUES.each do |key, default_value|
128
- value = consumer.consume(key)
129
- value = default_value if value.nil?
130
- r[key] = value
131
- end
132
- consumer.validate
133
- r
134
- end
34
+ ::EacRubyUtils::Console::Configs::StorePasswordsEntryReader.new(self) == 'yes'
135
35
  end
136
36
  end
137
37
  end
@@ -0,0 +1,81 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'eac_ruby_utils/console/configs/read_entry_options'
4
+ require 'eac_ruby_utils/core_ext'
5
+
6
+ module EacRubyUtils
7
+ module Console
8
+ class Configs
9
+ class EntryReader
10
+ enable_console_speaker
11
+
12
+ class << self
13
+ def entry_key_to_envvar_name(entry_key)
14
+ path = if entry_key.is_a?(::Array)
15
+ entry_key
16
+ else
17
+ ::EacRubyUtils::PathsHash.parse_entry_key(entry_key)
18
+ end
19
+ path.join('_').gsub(/[^a-z0-9_]/i, '').gsub(/\A_+/, '').gsub(/_+\z/, '')
20
+ .gsub(/_{2,}/, '_').upcase
21
+ end
22
+ end
23
+
24
+ common_constructor :console_configs, :entry_key, :options do
25
+ self.options = ::EacRubyUtils::Console::Configs::ReadEntryOptions.new(options)
26
+ end
27
+
28
+ def read
29
+ %w[envvars storage console].each do |suffix|
30
+ value = send("read_from_#{suffix}")
31
+ return value if value.present?
32
+ end
33
+ return nil unless options[:required]
34
+
35
+ raise "No value found for entry \"#{entry_key}\""
36
+ end
37
+
38
+ def read_from_storage
39
+ console_configs.configs.read_entry(entry_key)
40
+ end
41
+
42
+ def read_from_envvars
43
+ return if options[:noenv]
44
+
45
+ env_entry_key = self.class.entry_key_to_envvar_name(entry_key)
46
+ return unless ENV.key?(env_entry_key)
47
+
48
+ ENV.fetch(env_entry_key).if_present(::EacRubyUtils::BlankNotBlank.instance)
49
+ end
50
+
51
+ def read_from_console
52
+ return if options[:noinput]
53
+
54
+ options[:before_input].if_present(&:call)
55
+ entry_value = looped_entry_value_from_input
56
+ console_configs.configs.write_entry(entry_key, entry_value) if options[:store]
57
+ entry_value
58
+ end
59
+
60
+ private
61
+
62
+ def looped_entry_value_from_input
63
+ loop do
64
+ entry_value = entry_value_from_input(entry_key, options)
65
+ next if entry_value.blank?
66
+ next if options[:validator] && !options[:validator].call(entry_value)
67
+
68
+ return entry_value
69
+ end
70
+ end
71
+
72
+ def entry_value_from_input(entry_key, options)
73
+ entry_value = request_input("Value for entry \"#{entry_key}\"",
74
+ options.request_input_options)
75
+ warn('Entered value is blank') if entry_value.blank?
76
+ entry_value
77
+ end
78
+ end
79
+ end
80
+ end
81
+ end
@@ -0,0 +1,18 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'eac_ruby_utils/console/configs/entry_reader'
4
+
5
+ module EacRubyUtils
6
+ module Console
7
+ class Configs
8
+ class PasswordEntryReader < ::EacRubyUtils::Console::Configs::EntryReader
9
+ ENTRY_KEY = 'core.store_passwords'
10
+
11
+ def initialize(console_configs, entry_key, options = {})
12
+ super(console_configs, entry_key, options.merge(noecho: true,
13
+ store: console_configs.store_passwords?))
14
+ end
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,46 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'eac_ruby_utils/core_ext'
4
+
5
+ module EacRubyUtils
6
+ module Console
7
+ class Configs
8
+ class ReadEntryOptions
9
+ enable_simple_cache
10
+ common_constructor :options do
11
+ self.options = options.to_h.symbolize_keys
12
+ .assert_valid_keys(DEFAULT_VALUES.keys).freeze
13
+ end
14
+
15
+ DEFAULT_VALUES = {
16
+ before_input: nil, bool: false, list: false, noecho: false, noenv: false, noinput: false,
17
+ required: true, store: true, validator: nil
18
+ }.freeze
19
+
20
+ delegate :to_h, to: :options
21
+
22
+ def [](key)
23
+ values.fetch(key.to_sym)
24
+ end
25
+
26
+ def request_input_options
27
+ values.slice(:bool, :list, :noecho)
28
+ end
29
+
30
+ private
31
+
32
+ def values_uncached
33
+ consumer = options.to_options_consumer
34
+ r = {}
35
+ DEFAULT_VALUES.each do |key, default_value|
36
+ value = consumer.consume(key)
37
+ value = default_value if value.nil?
38
+ r[key] = value
39
+ end
40
+ consumer.validate
41
+ r
42
+ end
43
+ end
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,27 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'eac_ruby_utils/console/configs/entry_reader'
4
+
5
+ module EacRubyUtils
6
+ module Console
7
+ class Configs
8
+ class StorePasswordsEntryReader < ::EacRubyUtils::Console::Configs::EntryReader
9
+ ENTRY_KEY = 'core.store_passwords'
10
+
11
+ def initialize(console_configs)
12
+ super(console_configs, ENTRY_KEY,
13
+ before_input: -> { banner },
14
+ validator: ->(entry_value) { %w[yes no].include?(entry_value) }
15
+ )
16
+ end
17
+
18
+ def banner
19
+ infom 'Do you wanna to store passwords?'
20
+ infom 'Warning: the passwords will be store in clear text in ' \
21
+ "\"#{console_configs.configs.storage_path}\""
22
+ infom 'Enter "yes" or "no"'
23
+ end
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,4 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'eac_ruby_utils/require_sub'
4
+ ::EacRubyUtils.require_sub __FILE__
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Enumerator
4
+ def current(default_value = nil)
5
+ peek
6
+ rescue ::StopIteration
7
+ default_value
8
+ end
9
+ end
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Enumerator
4
+ def ongoing?
5
+ !stopped?
6
+ end
7
+
8
+ def stopped?
9
+ peek
10
+ false
11
+ rescue ::StopIteration
12
+ true
13
+ end
14
+ end
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Object
4
+ def print_debug
5
+ STDERR.write(to_debug + "\n")
6
+
7
+ self
8
+ end
9
+
10
+ def to_debug
11
+ "|#{self.class}|#{self}|"
12
+ end
13
+
14
+ def raise_debug
15
+ raise to_debug
16
+ end
17
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module EacRubyUtils
4
- VERSION = '0.49.0'
4
+ VERSION = '0.52.0'
5
5
  end
@@ -1,13 +1,14 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require 'date'
3
4
  require 'yaml'
4
5
 
5
6
  module EacRubyUtils
6
7
  # A safe YAML loader/dumper with common types included.
7
8
  class Yaml
8
9
  class << self
9
- DEFAULT_PERMITTED_CLASSES = [::Array, ::Date, ::FalseClass, ::Hash, ::NilClass, ::Numeric,
10
- ::String, ::Symbol, ::Time, ::TrueClass].freeze
10
+ DEFAULT_PERMITTED_CLASSES = [::Array, ::Date, ::DateTime, ::FalseClass, ::Hash, ::NilClass,
11
+ ::Numeric, ::String, ::Symbol, ::Time, ::TrueClass].freeze
11
12
 
12
13
  def dump(object)
13
14
  ::YAML.dump(sanitize(object))
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.49.0
4
+ version: 0.52.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: 2020-10-25 00:00:00.000000000 Z
11
+ date: 2020-11-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -108,6 +108,7 @@ extra_rdoc_files: []
108
108
  files:
109
109
  - README.rdoc
110
110
  - lib/eac_ruby_utils.rb
111
+ - lib/eac_ruby_utils/abstract_methods.rb
111
112
  - lib/eac_ruby_utils/arguments_consumer.rb
112
113
  - lib/eac_ruby_utils/blank_not_blank.rb
113
114
  - lib/eac_ruby_utils/boolean.rb
@@ -119,6 +120,10 @@ files:
119
120
  - lib/eac_ruby_utils/configs/file.rb
120
121
  - lib/eac_ruby_utils/console.rb
121
122
  - lib/eac_ruby_utils/console/configs.rb
123
+ - lib/eac_ruby_utils/console/configs/entry_reader.rb
124
+ - lib/eac_ruby_utils/console/configs/password_entry_reader.rb
125
+ - lib/eac_ruby_utils/console/configs/read_entry_options.rb
126
+ - lib/eac_ruby_utils/console/configs/store_passwords_entry_reader.rb
122
127
  - lib/eac_ruby_utils/console/docopt_runner.rb
123
128
  - lib/eac_ruby_utils/console/docopt_runner/_class_methods.rb
124
129
  - lib/eac_ruby_utils/console/docopt_runner/_doc.rb
@@ -182,6 +187,9 @@ files:
182
187
  - lib/eac_ruby_utils/patches/class/common_constructor.rb
183
188
  - lib/eac_ruby_utils/patches/enumerable.rb
184
189
  - lib/eac_ruby_utils/patches/enumerable/boolean_combinations.rb
190
+ - lib/eac_ruby_utils/patches/enumerator.rb
191
+ - lib/eac_ruby_utils/patches/enumerator/current.rb
192
+ - lib/eac_ruby_utils/patches/enumerator/stopped.rb
185
193
  - lib/eac_ruby_utils/patches/hash.rb
186
194
  - lib/eac_ruby_utils/patches/hash/options_consumer.rb
187
195
  - lib/eac_ruby_utils/patches/hash/sym_keys_hash.rb
@@ -195,6 +203,7 @@ files:
195
203
  - lib/eac_ruby_utils/patches/module/simple_cache.rb
196
204
  - lib/eac_ruby_utils/patches/object.rb
197
205
  - lib/eac_ruby_utils/patches/object/asserts.rb
206
+ - lib/eac_ruby_utils/patches/object/debug.rb
198
207
  - lib/eac_ruby_utils/patches/object/if_present.rb
199
208
  - lib/eac_ruby_utils/patches/object/if_respond.rb
200
209
  - lib/eac_ruby_utils/patches/object/template.rb