eac_ruby_utils 0.62.0 → 0.66.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.
Files changed (49) hide show
  1. checksums.yaml +4 -4
  2. data/lib/eac_ruby_utils/context.rb +31 -0
  3. data/lib/eac_ruby_utils/contextualizable.rb +12 -7
  4. data/lib/eac_ruby_utils/envs/command.rb +10 -5
  5. data/lib/eac_ruby_utils/filesystem_cache.rb +6 -0
  6. data/lib/eac_ruby_utils/patches/module/common_concern.rb +0 -1
  7. data/lib/eac_ruby_utils/patches/module/context.rb +11 -0
  8. data/lib/eac_ruby_utils/patches/regexp/to_parser.rb +10 -0
  9. data/lib/eac_ruby_utils/regexp_parser.rb +34 -0
  10. data/lib/eac_ruby_utils/rspec/conditional.rb +1 -4
  11. data/lib/eac_ruby_utils/simple_cache.rb +18 -5
  12. data/lib/eac_ruby_utils/version.rb +1 -1
  13. metadata +8 -74
  14. data/lib/eac_ruby_utils/configs.rb +0 -74
  15. data/lib/eac_ruby_utils/configs/base.rb +0 -43
  16. data/lib/eac_ruby_utils/configs/file.rb +0 -47
  17. data/lib/eac_ruby_utils/console.rb +0 -5
  18. data/lib/eac_ruby_utils/console/configs.rb +0 -38
  19. data/lib/eac_ruby_utils/console/configs/entry_reader.rb +0 -81
  20. data/lib/eac_ruby_utils/console/configs/password_entry_reader.rb +0 -18
  21. data/lib/eac_ruby_utils/console/configs/read_entry_options.rb +0 -46
  22. data/lib/eac_ruby_utils/console/configs/store_passwords_entry_reader.rb +0 -27
  23. data/lib/eac_ruby_utils/console/docopt_runner.rb +0 -45
  24. data/lib/eac_ruby_utils/console/docopt_runner/_class_methods.rb +0 -18
  25. data/lib/eac_ruby_utils/console/docopt_runner/_doc.rb +0 -25
  26. data/lib/eac_ruby_utils/console/docopt_runner/_settings.rb +0 -19
  27. data/lib/eac_ruby_utils/console/docopt_runner/_subcommands.rb +0 -154
  28. data/lib/eac_ruby_utils/console/speaker.rb +0 -133
  29. data/lib/eac_ruby_utils/console/speaker/_class_methods.rb +0 -39
  30. data/lib/eac_ruby_utils/console/speaker/_constants.rb +0 -14
  31. data/lib/eac_ruby_utils/console/speaker/list.rb +0 -63
  32. data/lib/eac_ruby_utils/console/speaker/node.rb +0 -26
  33. data/lib/eac_ruby_utils/patches/module/console_speaker.rb +0 -10
  34. data/lib/eac_ruby_utils/patches/module/template.rb +0 -10
  35. data/lib/eac_ruby_utils/patches/object/template.rb +0 -9
  36. data/lib/eac_ruby_utils/paths_hash.rb +0 -56
  37. data/lib/eac_ruby_utils/paths_hash/entry_key_error.rb +0 -8
  38. data/lib/eac_ruby_utils/paths_hash/node.rb +0 -67
  39. data/lib/eac_ruby_utils/paths_hash/path_search.rb +0 -39
  40. data/lib/eac_ruby_utils/templates.rb +0 -9
  41. data/lib/eac_ruby_utils/templates/directory.rb +0 -110
  42. data/lib/eac_ruby_utils/templates/file.rb +0 -50
  43. data/lib/eac_ruby_utils/templates/searcher.rb +0 -55
  44. data/lib/eac_ruby_utils/templates/variable_not_found_error.rb +0 -7
  45. data/lib/eac_ruby_utils/templates/variable_providers.rb +0 -25
  46. data/lib/eac_ruby_utils/templates/variable_providers/base.rb +0 -23
  47. data/lib/eac_ruby_utils/templates/variable_providers/entries_reader.rb +0 -25
  48. data/lib/eac_ruby_utils/templates/variable_providers/generic.rb +0 -25
  49. data/lib/eac_ruby_utils/templates/variable_providers/hash.rb +0 -29
@@ -1,43 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'eac_ruby_utils/blank_not_blank'
4
- require 'eac_ruby_utils/core_ext'
5
- require 'eac_ruby_utils/paths_hash'
6
-
7
- module EacRubyUtils
8
- class Configs
9
- class Base
10
- enable_simple_cache
11
-
12
- common_constructor :data, default: [{}] do
13
- self.data = ::EacRubyUtils::PathsHash.new(data)
14
- end
15
-
16
- def []=(entry_key, entry_value)
17
- write(entry_key, entry_value)
18
- end
19
-
20
- def [](entry_key)
21
- read(entry_key)
22
- end
23
-
24
- def clear
25
- replace({})
26
- end
27
-
28
- def read(entry_key)
29
- return nil unless data.key?(entry_key)
30
-
31
- data.fetch(entry_key).if_present(::EacRubyUtils::BlankNotBlank.instance)
32
- end
33
-
34
- def replace(new_data)
35
- self.data = ::EacRubyUtils::PathsHash.new(new_data)
36
- end
37
-
38
- def write(entry_key, entry_value)
39
- data[entry_key] = entry_value
40
- end
41
- end
42
- end
43
- end
@@ -1,47 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'eac_ruby_utils/configs/base'
4
- require 'yaml'
5
-
6
- module EacRubyUtils
7
- class Configs
8
- class File < ::EacRubyUtils::Configs::Base
9
- attr_reader :path, :options
10
-
11
- # Valid options: [:autosave]
12
- def initialize(path, options = {})
13
- @path = path
14
- @options = options.to_sym_keys_hash.freeze
15
- super(raw_data_from_file)
16
- end
17
-
18
- def save
19
- ::FileUtils.mkdir_p(::File.dirname(path))
20
- ::File.write(path, data.to_h.to_yaml)
21
- end
22
-
23
- def load
24
- replace(raw_data_from_file)
25
- end
26
-
27
- def write(entry_key, entry_value)
28
- super
29
- save if autosave?
30
- end
31
-
32
- def autosave?
33
- options[:autosave] ? true : false
34
- end
35
-
36
- private
37
-
38
- def raw_data_from_file
39
- if ::File.exist?(path) && ::File.size(path).positive?
40
- ::YAML.load_file(path)
41
- else
42
- {}
43
- end
44
- end
45
- end
46
- end
47
- end
@@ -1,5 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'eac_ruby_utils/console/configs'
4
- require 'eac_ruby_utils/console/docopt_runner'
5
- require 'eac_ruby_utils/console/speaker'
@@ -1,38 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'eac_ruby_utils/configs'
4
- require 'eac_ruby_utils/core_ext'
5
-
6
- module EacRubyUtils
7
- module Console
8
- class Configs
9
- require_sub __FILE__
10
- enable_console_speaker
11
-
12
- class << self
13
- def entry_key_to_envvar_name(entry_key)
14
- ::EacRubyUtils::Console::Configs::EntryReader.entry_key_to_envvar_name(entry_key)
15
- end
16
- end
17
-
18
- attr_reader :configs
19
-
20
- def initialize(configs_key, options = {})
21
- options.assert_argument(::Hash, 'options')
22
- @configs = ::EacRubyUtils::Configs.new(configs_key, options.merge(autosave: true))
23
- end
24
-
25
- def read_password(entry_key, options = {})
26
- ::EacRubyUtils::Console::Configs::PasswordEntryReader.new(self, entry_key, options).read
27
- end
28
-
29
- def read_entry(entry_key, options = {})
30
- ::EacRubyUtils::Console::Configs::EntryReader.new(self, entry_key, options).read
31
- end
32
-
33
- def store_passwords?
34
- ::EacRubyUtils::Console::Configs::StorePasswordsEntryReader.new(self) == 'yes'
35
- end
36
- end
37
- end
38
- end
@@ -1,81 +0,0 @@
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
@@ -1,18 +0,0 @@
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
@@ -1,46 +0,0 @@
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
@@ -1,27 +0,0 @@
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
@@ -1,45 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'active_support/core_ext/hash/indifferent_access'
4
- require 'active_support/core_ext/hash/slice'
5
- require 'docopt'
6
- require 'eac_ruby_utils/contextualizable'
7
- require 'eac_ruby_utils/patches/hash/sym_keys_hash'
8
- Dir["#{__dir__}/#{::File.basename(__FILE__, '.*')}/_*.rb"].sort.each do |partial|
9
- require partial
10
- end
11
-
12
- module EacRubyUtils
13
- module Console
14
- class DocoptRunner
15
- include ::EacRubyUtils::Contextualizable
16
-
17
- class << self
18
- def create(settings = {})
19
- new(settings)
20
- end
21
- end
22
-
23
- attr_reader :settings
24
-
25
- def initialize(settings = {})
26
- @settings = settings.with_indifferent_access.freeze
27
- check_subcommands
28
- end
29
-
30
- def options
31
- @options ||= Docopt.docopt(target_doc, docopt_options)
32
- end
33
-
34
- def parent
35
- settings[:parent]
36
- end
37
-
38
- protected
39
-
40
- def docopt_options
41
- settings.slice(:version, :argv, :help, :options_first).to_sym_keys_hash
42
- end
43
- end
44
- end
45
- end
@@ -1,18 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module EacRubyUtils
4
- module Console
5
- class DocoptRunner
6
- DOCOPT_ERROR_EXIT_CODE = 0xC0
7
-
8
- class << self
9
- def run(options = {})
10
- create(options).send(:run)
11
- rescue Docopt::Exit => e
12
- STDERR.write(e.message + "\n")
13
- ::Kernel.exit(DOCOPT_ERROR_EXIT_CODE) # rubocop:disable Rails/Exit
14
- end
15
- end
16
- end
17
- end
18
- end
@@ -1,25 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module EacRubyUtils
4
- module Console
5
- class DocoptRunner
6
- PROGRAM_MACRO = '__PROGRAM__'
7
-
8
- def source_doc
9
- setting_value(:doc)
10
- end
11
-
12
- def target_doc
13
- source_doc.gsub(PROGRAM_MACRO, target_program_name).strip + "\n"
14
- end
15
-
16
- def source_program_name
17
- setting_value(:program_name, false)
18
- end
19
-
20
- def target_program_name
21
- [source_program_name, ENV['PROGRAM_NAME'], $PROGRAM_NAME].find(&:present?)
22
- end
23
- end
24
- end
25
- end
@@ -1,19 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'eac_ruby_utils/settings_provider'
4
-
5
- module EacRubyUtils
6
- module Console
7
- class DocoptRunner
8
- include ::EacRubyUtils::SettingsProvider
9
-
10
- attr_reader :settings
11
-
12
- private
13
-
14
- def setting_value(key, required = true)
15
- super(key, required: required, order: %w[method settings_object constant])
16
- end
17
- end
18
- end
19
- end
@@ -1,154 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'active_support/core_ext/string/inflections'
4
- require 'shellwords'
5
-
6
- module EacRubyUtils
7
- module Console
8
- class DocoptRunner
9
- SUBCOMMAND_ARG = '<subcommand>'
10
- SUBCOMMAND_ARGS_ARG = '<subcommand-args>'
11
- SUBCOMMANDS_MACRO = '__SUBCOMMANDS__'
12
-
13
- def subcommands?
14
- source_doc.include?(SUBCOMMANDS_MACRO)
15
- end
16
-
17
- def check_subcommands
18
- return unless subcommands?
19
-
20
- singleton_class.include(SubcommandsSupport)
21
- check_subcommands_arg
22
- return if singleton_class.method_defined?(:run)
23
-
24
- singleton_class.send(:alias_method, :run, :run_with_subcommand)
25
- end
26
-
27
- module SubcommandsSupport
28
- EXTRA_AVAILABLE_SUBCOMMANDS_METHOD_NAME = :extra_available_subcommands
29
-
30
- def check_subcommands_arg
31
- if subcommand_arg_as_list?
32
- singleton_class.include(SubcommandsSupport::SubcommandArgAsList)
33
- else
34
- singleton_class.include(SubcommandsSupport::SubcommandArgAsArg)
35
- end
36
- end
37
-
38
- def run_with_subcommand
39
- if subcommand_name
40
- check_valid_subcommand
41
- subcommand_run
42
- else
43
- run_without_subcommand
44
- end
45
- end
46
-
47
- def subcommand
48
- @subcommand ||= subcommand_class_name(subcommand_name).constantize.create(
49
- argv: subcommand_args,
50
- program_name: subcommand_program,
51
- parent: self
52
- )
53
- end
54
-
55
- def subcommand_run
56
- if !subcommand.is_a?(::EacRubyUtils::Console::DocoptRunner) &&
57
- subcommand.respond_to?(:run_run)
58
- subcommand.run_run
59
- else
60
- subcommand.run
61
- end
62
- end
63
-
64
- def target_doc
65
- super.gsub(SUBCOMMANDS_MACRO,
66
- "#{target_doc_subcommand_arg} [#{SUBCOMMAND_ARGS_ARG}...]") +
67
- "\n" + subcommands_target_doc
68
- end
69
-
70
- def docopt_options
71
- super.merge(options_first: true)
72
- end
73
-
74
- def subcommand_class_name(subcommand)
75
- "#{self.class.name}::#{subcommand.underscore.camelize}"
76
- end
77
-
78
- def subcommand_arg_as_list?
79
- setting_value(:subcommand_arg_as_list, false) || false
80
- end
81
-
82
- def subcommand_args
83
- options.fetch(SUBCOMMAND_ARGS_ARG)
84
- end
85
-
86
- def subcommand_program
87
- subcommand_name
88
- end
89
-
90
- def available_subcommands
91
- r = ::Set.new(setting_value(:subcommands, false) || auto_available_subcommands)
92
- if respond_to?(EXTRA_AVAILABLE_SUBCOMMANDS_METHOD_NAME, true)
93
- r += send(EXTRA_AVAILABLE_SUBCOMMANDS_METHOD_NAME)
94
- end
95
- r.sort
96
- end
97
-
98
- def auto_available_subcommands
99
- self.class.constants
100
- .map { |name| self.class.const_get(name) }
101
- .select { |c| c.instance_of? Class }
102
- .select { |c| c < ::EacRubyUtils::Console::DocoptRunner }
103
- .map { |c| c.name.demodulize.underscore.dasherize }
104
- end
105
-
106
- def run_without_subcommand
107
- "Method #{__method__} should be overrided in #{self.class.name}"
108
- end
109
-
110
- protected
111
-
112
- def check_valid_subcommand
113
- return if available_subcommands.include?(subcommand_name)
114
-
115
- raise ::Docopt::Exit, "\"#{subcommand_name}\" is not a valid subcommand" \
116
- " (Valid: #{available_subcommands.join(', ')})"
117
- end
118
-
119
- module SubcommandArgAsArg
120
- def target_doc_subcommand_arg
121
- SUBCOMMAND_ARG
122
- end
123
-
124
- def subcommand_name
125
- options.fetch(SUBCOMMAND_ARG)
126
- end
127
-
128
- def subcommands_target_doc
129
- available_subcommands.inject("Subcommands:\n") do |a, e|
130
- a + " #{e}\n"
131
- end
132
- end
133
- end
134
-
135
- module SubcommandArgAsList
136
- def target_doc_subcommand_arg
137
- '(' + available_subcommands.join('|') + ')'
138
- end
139
-
140
- def subcommand_name
141
- available_subcommands.each do |subcommand|
142
- return subcommand if options.fetch(subcommand)
143
- end
144
- nil
145
- end
146
-
147
- def subcommands_target_doc
148
- "\n"
149
- end
150
- end
151
- end
152
- end
153
- end
154
- end