ehbrs-tools 0.3.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 +7 -0
- data/Gemfile +13 -0
- data/exe/ehbrs +6 -0
- data/lib/ehbrs.rb +7 -0
- data/lib/ehbrs/executables.rb +28 -0
- data/lib/ehbrs/tools.rb +9 -0
- data/lib/ehbrs/tools/runner.rb +35 -0
- data/lib/ehbrs/tools/runner/vg.rb +26 -0
- data/lib/ehbrs/tools/runner/vg/ips.rb +90 -0
- data/lib/ehbrs/tools/runner/videos.rb +25 -0
- data/lib/ehbrs/tools/runner/videos/unsupported.rb +65 -0
- data/lib/ehbrs/tools/version.rb +7 -0
- data/lib/ehbrs/videos.rb +9 -0
- data/lib/ehbrs/videos/convert_job.rb +99 -0
- data/lib/ehbrs/videos/file.rb +70 -0
- data/lib/ehbrs/videos/profiles/base.rb +35 -0
- data/lib/ehbrs/videos/profiles/same_quality.rb +21 -0
- data/lib/ehbrs/videos/track.rb +27 -0
- data/lib/ehbrs/videos/unsupported/check_result.rb +24 -0
- data/lib/ehbrs/videos/unsupported/check_set.rb +43 -0
- data/lib/ehbrs/videos/unsupported/check_support.rb +68 -0
- data/lib/ehbrs/videos/unsupported/checks.rb +13 -0
- data/lib/ehbrs/videos/unsupported/checks/codec_extra_unlisted.rb +27 -0
- data/lib/ehbrs/videos/unsupported/checks/codec_extra_unsupported.rb +26 -0
- data/lib/ehbrs/videos/unsupported/checks/codec_unlisted.rb +23 -0
- data/lib/ehbrs/videos/unsupported/checks/codec_unsupported.rb +25 -0
- data/lib/ehbrs/videos/unsupported/checks/invalid_extension.rb +25 -0
- data/lib/ehbrs/videos/unsupported/file.rb +50 -0
- data/lib/ehbrs/videos/unsupported/file/fix.rb +45 -0
- data/lib/ehbrs/videos/unsupported/fix_profile.rb +56 -0
- data/lib/ehbrs/videos/unsupported/fixes/supported_codec.rb +36 -0
- data/lib/ehbrs/videos/unsupported/fixes/supported_container.rb +17 -0
- data/lib/ehbrs/videos/unsupported/profiles.rb +13 -0
- data/lib/ehbrs/videos/unsupported/profiles/base.rb +102 -0
- data/lib/ehbrs/videos/unsupported/profiles/philco.rb +27 -0
- data/lib/ehbrs/videos/unsupported/profiles/samsung.rb +32 -0
- data/lib/ehbrs/videos/unsupported/search.rb +72 -0
- data/lib/ehbrs/videos/unsupported/track.rb +32 -0
- data/vendor/eac_cli/Gemfile +5 -0
- data/vendor/eac_cli/eac_cli.gemspec +18 -0
- data/vendor/eac_cli/lib/eac_cli.rb +7 -0
- data/vendor/eac_cli/lib/eac_cli/default_runner.rb +22 -0
- data/vendor/eac_cli/lib/eac_cli/runner.rb +30 -0
- data/vendor/eac_cli/lib/eac_cli/runner/argument_option.rb +13 -0
- data/vendor/eac_cli/lib/eac_cli/runner/base_option.rb +22 -0
- data/vendor/eac_cli/lib/eac_cli/runner/boolean_option.rb +13 -0
- data/vendor/eac_cli/lib/eac_cli/runner/definition.rb +60 -0
- data/vendor/eac_cli/lib/eac_cli/runner/docopt_doc.rb +65 -0
- data/vendor/eac_cli/lib/eac_cli/runner/positional_argument.rb +15 -0
- data/vendor/eac_cli/lib/eac_cli/version.rb +5 -0
- data/vendor/eac_cli/spec/rubocop_spec.rb +7 -0
- data/vendor/eac_cli/spec/spec_helper.rb +100 -0
- data/vendor/eac_ruby_utils/Gemfile +16 -0
- data/vendor/eac_ruby_utils/MIT-LICENCE +16 -0
- data/vendor/eac_ruby_utils/README.rdoc +3 -0
- data/vendor/eac_ruby_utils/eac_ruby_utils.gemspec +25 -0
- data/vendor/eac_ruby_utils/lib/eac_ruby_utils.rb +23 -0
- data/vendor/eac_ruby_utils/lib/eac_ruby_utils/arguments_consumer.rb +81 -0
- data/vendor/eac_ruby_utils/lib/eac_ruby_utils/common_constructor.rb +133 -0
- data/vendor/eac_ruby_utils/lib/eac_ruby_utils/configs.rb +82 -0
- data/vendor/eac_ruby_utils/lib/eac_ruby_utils/configs/file.rb +66 -0
- data/vendor/eac_ruby_utils/lib/eac_ruby_utils/console.rb +5 -0
- data/vendor/eac_ruby_utils/lib/eac_ruby_utils/console/configs.rb +135 -0
- data/vendor/eac_ruby_utils/lib/eac_ruby_utils/console/docopt_runner.rb +39 -0
- data/vendor/eac_ruby_utils/lib/eac_ruby_utils/console/docopt_runner/_class_methods.rb +18 -0
- data/vendor/eac_ruby_utils/lib/eac_ruby_utils/console/docopt_runner/_doc.rb +25 -0
- data/vendor/eac_ruby_utils/lib/eac_ruby_utils/console/docopt_runner/_settings.rb +19 -0
- data/vendor/eac_ruby_utils/lib/eac_ruby_utils/console/docopt_runner/_subcommands.rb +139 -0
- data/vendor/eac_ruby_utils/lib/eac_ruby_utils/console/speaker.rb +134 -0
- data/vendor/eac_ruby_utils/lib/eac_ruby_utils/console/speaker/_class_methods.rb +39 -0
- data/vendor/eac_ruby_utils/lib/eac_ruby_utils/console/speaker/list.rb +63 -0
- data/vendor/eac_ruby_utils/lib/eac_ruby_utils/console/speaker/node.rb +25 -0
- data/vendor/eac_ruby_utils/lib/eac_ruby_utils/contextualizable.rb +16 -0
- data/vendor/eac_ruby_utils/lib/eac_ruby_utils/core_ext.rb +5 -0
- data/vendor/eac_ruby_utils/lib/eac_ruby_utils/envs.rb +23 -0
- data/vendor/eac_ruby_utils/lib/eac_ruby_utils/envs/base_env.rb +28 -0
- data/vendor/eac_ruby_utils/lib/eac_ruby_utils/envs/command.rb +155 -0
- data/vendor/eac_ruby_utils/lib/eac_ruby_utils/envs/command/extra_options.rb +46 -0
- data/vendor/eac_ruby_utils/lib/eac_ruby_utils/envs/executable.rb +51 -0
- data/vendor/eac_ruby_utils/lib/eac_ruby_utils/envs/file.rb +22 -0
- data/vendor/eac_ruby_utils/lib/eac_ruby_utils/envs/local_env.rb +17 -0
- data/vendor/eac_ruby_utils/lib/eac_ruby_utils/envs/process.rb +19 -0
- data/vendor/eac_ruby_utils/lib/eac_ruby_utils/envs/ssh_env.rb +72 -0
- data/vendor/eac_ruby_utils/lib/eac_ruby_utils/filesystem_cache.rb +53 -0
- data/vendor/eac_ruby_utils/lib/eac_ruby_utils/filesystem_traverser.rb +56 -0
- data/vendor/eac_ruby_utils/lib/eac_ruby_utils/fs_cache.rb +11 -0
- data/vendor/eac_ruby_utils/lib/eac_ruby_utils/listable.rb +17 -0
- data/vendor/eac_ruby_utils/lib/eac_ruby_utils/listable/class_methods.rb +14 -0
- data/vendor/eac_ruby_utils/lib/eac_ruby_utils/listable/instance_methods.rb +30 -0
- data/vendor/eac_ruby_utils/lib/eac_ruby_utils/listable/integer_list.rb +23 -0
- data/vendor/eac_ruby_utils/lib/eac_ruby_utils/listable/list.rb +89 -0
- data/vendor/eac_ruby_utils/lib/eac_ruby_utils/listable/lists.rb +61 -0
- data/vendor/eac_ruby_utils/lib/eac_ruby_utils/listable/string_list.rb +19 -0
- data/vendor/eac_ruby_utils/lib/eac_ruby_utils/listable/value.rb +38 -0
- data/vendor/eac_ruby_utils/lib/eac_ruby_utils/on_clean_ruby_environment.rb +23 -0
- data/vendor/eac_ruby_utils/lib/eac_ruby_utils/options_consumer.rb +62 -0
- data/vendor/eac_ruby_utils/lib/eac_ruby_utils/patch.rb +11 -0
- data/vendor/eac_ruby_utils/lib/eac_ruby_utils/patches.rb +4 -0
- data/vendor/eac_ruby_utils/lib/eac_ruby_utils/patches/class.rb +4 -0
- data/vendor/eac_ruby_utils/lib/eac_ruby_utils/patches/class/common_constructor.rb +9 -0
- data/vendor/eac_ruby_utils/lib/eac_ruby_utils/patches/hash.rb +5 -0
- data/vendor/eac_ruby_utils/lib/eac_ruby_utils/patches/hash/options_consumer.rb +10 -0
- data/vendor/eac_ruby_utils/lib/eac_ruby_utils/patches/hash/sym_keys_hash.rb +10 -0
- data/vendor/eac_ruby_utils/lib/eac_ruby_utils/patches/module.rb +4 -0
- data/vendor/eac_ruby_utils/lib/eac_ruby_utils/patches/module/console_speaker.rb +10 -0
- data/vendor/eac_ruby_utils/lib/eac_ruby_utils/patches/module/listable.rb +10 -0
- data/vendor/eac_ruby_utils/lib/eac_ruby_utils/patches/module/patch.rb +9 -0
- data/vendor/eac_ruby_utils/lib/eac_ruby_utils/patches/module/require_sub.rb +9 -0
- data/vendor/eac_ruby_utils/lib/eac_ruby_utils/patches/module/simple_cache.rb +10 -0
- data/vendor/eac_ruby_utils/lib/eac_ruby_utils/patches/object.rb +5 -0
- data/vendor/eac_ruby_utils/lib/eac_ruby_utils/patches/object/asserts.rb +14 -0
- data/vendor/eac_ruby_utils/lib/eac_ruby_utils/patches/object/if_present.rb +12 -0
- data/vendor/eac_ruby_utils/lib/eac_ruby_utils/patches/object/template.rb +16 -0
- data/vendor/eac_ruby_utils/lib/eac_ruby_utils/paths_hash.rb +95 -0
- data/vendor/eac_ruby_utils/lib/eac_ruby_utils/require_sub.rb +11 -0
- data/vendor/eac_ruby_utils/lib/eac_ruby_utils/rspec.rb +9 -0
- data/vendor/eac_ruby_utils/lib/eac_ruby_utils/rspec/conditional.rb +38 -0
- data/vendor/eac_ruby_utils/lib/eac_ruby_utils/rspec/stubbed_ssh.rb +44 -0
- data/vendor/eac_ruby_utils/lib/eac_ruby_utils/settings_provider.rb +65 -0
- data/vendor/eac_ruby_utils/lib/eac_ruby_utils/simple_cache.rb +42 -0
- data/vendor/eac_ruby_utils/lib/eac_ruby_utils/templates.rb +9 -0
- data/vendor/eac_ruby_utils/lib/eac_ruby_utils/templates/directory.rb +104 -0
- data/vendor/eac_ruby_utils/lib/eac_ruby_utils/templates/file.rb +101 -0
- data/vendor/eac_ruby_utils/lib/eac_ruby_utils/templates/searcher.rb +55 -0
- data/vendor/eac_ruby_utils/lib/eac_ruby_utils/version.rb +5 -0
- data/vendor/eac_ruby_utils/lib/eac_ruby_utils/yaml.rb +13 -0
- data/vendor/eac_ruby_utils/spec/lib/eac_ruby_utils/arguments_consumer_spec.rb +17 -0
- data/vendor/eac_ruby_utils/spec/lib/eac_ruby_utils/common_constructor_spec.rb +36 -0
- data/vendor/eac_ruby_utils/spec/lib/eac_ruby_utils/configs_spec.rb +31 -0
- data/vendor/eac_ruby_utils/spec/lib/eac_ruby_utils/console/configs_spec.rb +14 -0
- data/vendor/eac_ruby_utils/spec/lib/eac_ruby_utils/console/docopt_runner_spec.rb +136 -0
- data/vendor/eac_ruby_utils/spec/lib/eac_ruby_utils/console/speaker_spec.rb +36 -0
- data/vendor/eac_ruby_utils/spec/lib/eac_ruby_utils/envs/executable_spec.rb +33 -0
- data/vendor/eac_ruby_utils/spec/lib/eac_ruby_utils/envs/ssh_env_spec.rb +25 -0
- data/vendor/eac_ruby_utils/spec/lib/eac_ruby_utils/filesystem_cache_spec.rb +32 -0
- data/vendor/eac_ruby_utils/spec/lib/eac_ruby_utils/listable_spec.rb +180 -0
- data/vendor/eac_ruby_utils/spec/lib/eac_ruby_utils/options_consumer_spec.rb +45 -0
- data/vendor/eac_ruby_utils/spec/lib/eac_ruby_utils/patches/hash/options_consumer_spec.rb +17 -0
- data/vendor/eac_ruby_utils/spec/lib/eac_ruby_utils/patches/hash/sym_keys_hash_spec.rb +13 -0
- data/vendor/eac_ruby_utils/spec/lib/eac_ruby_utils/patches/module/console_speaker_spec.rb +13 -0
- data/vendor/eac_ruby_utils/spec/lib/eac_ruby_utils/patches/module/listable_spec.rb +13 -0
- data/vendor/eac_ruby_utils/spec/lib/eac_ruby_utils/patches/module/simple_cache_spec.rb +13 -0
- data/vendor/eac_ruby_utils/spec/lib/eac_ruby_utils/patches/object/if_present_spec.rb +13 -0
- data/vendor/eac_ruby_utils/spec/lib/eac_ruby_utils/patches/object/template_spec.rb +23 -0
- data/vendor/eac_ruby_utils/spec/lib/eac_ruby_utils/patches/object/template_spec_files/path/my_stub_with_template +1 -0
- data/vendor/eac_ruby_utils/spec/lib/eac_ruby_utils/paths_hash_spec.rb +49 -0
- data/vendor/eac_ruby_utils/spec/lib/eac_ruby_utils/settings_provider_spec.rb +46 -0
- data/vendor/eac_ruby_utils/spec/lib/eac_ruby_utils/simple_cache_spec.rb +102 -0
- data/vendor/eac_ruby_utils/spec/lib/eac_ruby_utils/templates/file_spec.rb +34 -0
- data/vendor/eac_ruby_utils/spec/lib/eac_ruby_utils/templates/file_spec_files/expected_content +2 -0
- data/vendor/eac_ruby_utils/spec/lib/eac_ruby_utils/templates/file_spec_files/source.template +2 -0
- data/vendor/eac_ruby_utils/spec/lib/eac_ruby_utils/templates/searcher_spec.rb +30 -0
- data/vendor/eac_ruby_utils/spec/lib/eac_ruby_utils/templates/searcher_spec_files/path1/subdir1/file1.template +1 -0
- data/vendor/eac_ruby_utils/spec/lib/eac_ruby_utils/templates/searcher_spec_files/path1/subdir1/file2 +1 -0
- data/vendor/eac_ruby_utils/spec/lib/eac_ruby_utils/templates/searcher_spec_files/path2/subdir1/file3.template +1 -0
- data/vendor/eac_ruby_utils/spec/locales/pt-BR.yml +38 -0
- data/vendor/eac_ruby_utils/spec/spec_helper.rb +12 -0
- metadata +227 -0
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
Copyright 2018 Eduardo Henrique Bogoni <eduardobogoni@gmail.com>
|
|
2
|
+
|
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
|
|
4
|
+
associated documentation files (the "Software"), to deal in the Software without restriction,
|
|
5
|
+
including without limitation the rights to use, copy, modify, merge, publish, distribute,
|
|
6
|
+
sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
|
|
7
|
+
furnished to do so, subject to the following conditions:
|
|
8
|
+
|
|
9
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial
|
|
10
|
+
portions of the Software.
|
|
11
|
+
|
|
12
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
|
|
13
|
+
NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
14
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES
|
|
15
|
+
OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
|
16
|
+
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
$LOAD_PATH.push File.expand_path('lib', __dir__)
|
|
4
|
+
|
|
5
|
+
# Maintain your gem's version:
|
|
6
|
+
require 'eac_ruby_utils/version'
|
|
7
|
+
|
|
8
|
+
# Describe your gem and declare its dependencies:
|
|
9
|
+
Gem::Specification.new do |s|
|
|
10
|
+
s.name = 'eac_ruby_utils'
|
|
11
|
+
s.version = ::EacRubyUtils::VERSION
|
|
12
|
+
s.authors = ['Esquilo Azul Company']
|
|
13
|
+
s.summary = 'Utilities for E.A.C.\'s Ruby projects.'
|
|
14
|
+
s.license = 'MIT'
|
|
15
|
+
|
|
16
|
+
s.files = Dir['{lib}/**/*', 'MIT-LICENSE', 'README.rdoc']
|
|
17
|
+
|
|
18
|
+
s.add_dependency 'activesupport', '~> 4.0'
|
|
19
|
+
s.add_dependency 'addressable', '~> 2.6'
|
|
20
|
+
s.add_dependency 'colorize', '~> 0.8.1'
|
|
21
|
+
s.add_dependency 'docopt', '~> 0.6.1'
|
|
22
|
+
s.add_dependency 'net-ssh', '~> 4.2'
|
|
23
|
+
s.add_development_dependency 'rspec', '~> 3.8'
|
|
24
|
+
s.add_development_dependency 'rubocop', '~> 0.80.1'
|
|
25
|
+
end
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module EacRubyUtils
|
|
4
|
+
require 'eac_ruby_utils/arguments_consumer'
|
|
5
|
+
require 'eac_ruby_utils/configs'
|
|
6
|
+
require 'eac_ruby_utils/console'
|
|
7
|
+
require 'eac_ruby_utils/contextualizable'
|
|
8
|
+
require 'eac_ruby_utils/core_ext'
|
|
9
|
+
require 'eac_ruby_utils/envs'
|
|
10
|
+
require 'eac_ruby_utils/filesystem_cache'
|
|
11
|
+
require 'eac_ruby_utils/filesystem_traverser'
|
|
12
|
+
require 'eac_ruby_utils/fs_cache'
|
|
13
|
+
require 'eac_ruby_utils/listable'
|
|
14
|
+
require 'eac_ruby_utils/options_consumer'
|
|
15
|
+
require 'eac_ruby_utils/settings_provider'
|
|
16
|
+
require 'eac_ruby_utils/patch'
|
|
17
|
+
require 'eac_ruby_utils/patches'
|
|
18
|
+
require 'eac_ruby_utils/paths_hash'
|
|
19
|
+
require 'eac_ruby_utils/require_sub'
|
|
20
|
+
require 'eac_ruby_utils/rspec'
|
|
21
|
+
require 'eac_ruby_utils/simple_cache'
|
|
22
|
+
require 'eac_ruby_utils/yaml'
|
|
23
|
+
end
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'active_support/hash_with_indifferent_access'
|
|
4
|
+
require 'ostruct'
|
|
5
|
+
|
|
6
|
+
module EacRubyUtils
|
|
7
|
+
class ArgumentsConsumer
|
|
8
|
+
class << self
|
|
9
|
+
def parse(args, positional = [], options = {})
|
|
10
|
+
new(args, positional, options).data
|
|
11
|
+
end
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
attr_reader :positional, :default_options
|
|
15
|
+
|
|
16
|
+
def initialize(positional, default_options)
|
|
17
|
+
@positional = positional.dup.freeze
|
|
18
|
+
@default_options = default_options.dup.freeze
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def parse(args)
|
|
22
|
+
Parser.new(self, args).data
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
class Parser
|
|
26
|
+
attr_reader :data, :arguments_consumer
|
|
27
|
+
|
|
28
|
+
def initialize(arguments_consumer, args)
|
|
29
|
+
@arguments_consumer = arguments_consumer
|
|
30
|
+
@data = ::ActiveSupport::HashWithIndifferentAccess.new
|
|
31
|
+
@options_found = false
|
|
32
|
+
arguments_consumer.positional.each { |key| data[key] = nil }
|
|
33
|
+
data.merge!(arguments_consumer.default_options)
|
|
34
|
+
args.each_with_index { |value, index| add_arg(value, index) }
|
|
35
|
+
data.freeze
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
private
|
|
39
|
+
|
|
40
|
+
def add_arg(value, index)
|
|
41
|
+
arg = ::OpenStruct.new(value: value, index: index)
|
|
42
|
+
if arg.value.is_a?(::Hash)
|
|
43
|
+
add_hash_arg(arg)
|
|
44
|
+
else
|
|
45
|
+
add_positional_arg(arg)
|
|
46
|
+
end
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
def add_hash_arg(arg)
|
|
50
|
+
check_no_more_arguments(arg)
|
|
51
|
+
data.merge!(arg.value)
|
|
52
|
+
@options_found = true
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
def add_positional_arg(arg)
|
|
56
|
+
check_no_more_arguments(arg)
|
|
57
|
+
invalid_argument arg, 'No more valid positional' if
|
|
58
|
+
arg.index >= arguments_consumer.positional.count
|
|
59
|
+
data[arguments_consumer.positional[arg.index]] = arg.value
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
def check_no_more_arguments(arg)
|
|
63
|
+
return unless @options_found
|
|
64
|
+
|
|
65
|
+
invalid_argument arg, 'Hash already found - no more positional allowed'
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
def invalid_argument(arg, message)
|
|
69
|
+
raise InvalidArgumentError.new(self, arg, message)
|
|
70
|
+
end
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
class InvalidArgumentError < StandardError
|
|
74
|
+
def initialize(args_consumer, arg, message)
|
|
75
|
+
@args_consumer = args_consumer
|
|
76
|
+
@arg = arg
|
|
77
|
+
super "#{message} (Arg: #{arg}, Args Consumer: #{args_consumer})"
|
|
78
|
+
end
|
|
79
|
+
end
|
|
80
|
+
end
|
|
81
|
+
end
|
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'active_support/callbacks'
|
|
4
|
+
require 'eac_ruby_utils/arguments_consumer'
|
|
5
|
+
require 'ostruct'
|
|
6
|
+
|
|
7
|
+
module EacRubyUtils
|
|
8
|
+
class CommonConstructor
|
|
9
|
+
attr_reader :args, :options, :after_set_block
|
|
10
|
+
|
|
11
|
+
class << self
|
|
12
|
+
def parse_args_options(args)
|
|
13
|
+
result = ::OpenStruct.new(args: [], options: {})
|
|
14
|
+
options_reached = false
|
|
15
|
+
args.each do |arg|
|
|
16
|
+
raise "Options reached and there is more arguments (Arguments: #{args})" if
|
|
17
|
+
options_reached
|
|
18
|
+
|
|
19
|
+
options_reached = parse_arg_options_process_arg(result, arg)
|
|
20
|
+
end
|
|
21
|
+
result
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
private
|
|
25
|
+
|
|
26
|
+
def parse_arg_options_process_arg(result, arg)
|
|
27
|
+
if arg.is_a?(::Hash)
|
|
28
|
+
result.options = arg
|
|
29
|
+
true
|
|
30
|
+
else
|
|
31
|
+
result.args << arg
|
|
32
|
+
false
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
def initialize(*args, &after_set_block)
|
|
38
|
+
args_processed = self.class.parse_args_options(args)
|
|
39
|
+
@args = args_processed.args
|
|
40
|
+
@options = args_processed.options
|
|
41
|
+
@after_set_block = after_set_block
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
def args_count
|
|
45
|
+
(args_count_min..args_count_max)
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
def args_count_min
|
|
49
|
+
args.count - default_values.count
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
def args_count_max
|
|
53
|
+
args.count
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
def default_values
|
|
57
|
+
options[:default] || []
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
def setup_class(klass)
|
|
61
|
+
setup_class_attr_readers(klass)
|
|
62
|
+
setup_class_attr_writers(klass)
|
|
63
|
+
setup_class_initialize(klass)
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
def setup_class_attr_readers(klass)
|
|
67
|
+
klass.send(:attr_reader, *args)
|
|
68
|
+
klass.send(:public, *args)
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
def setup_class_attr_writers(klass)
|
|
72
|
+
klass.send(:attr_writer, *args)
|
|
73
|
+
klass.send(:private, *args.map { |a| "#{a}=" })
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
def setup_class_initialize(klass)
|
|
77
|
+
common_constructor = self
|
|
78
|
+
klass.include(::ActiveSupport::Callbacks)
|
|
79
|
+
klass.define_callbacks :initialize
|
|
80
|
+
klass.send(:define_method, :initialize) do |*args|
|
|
81
|
+
Initialize.new(common_constructor, args, self).run
|
|
82
|
+
end
|
|
83
|
+
end
|
|
84
|
+
|
|
85
|
+
class Initialize
|
|
86
|
+
attr_reader :common_constructor, :args, :object
|
|
87
|
+
|
|
88
|
+
def initialize(common_constructor, args, object)
|
|
89
|
+
@common_constructor = common_constructor
|
|
90
|
+
@args = args
|
|
91
|
+
@object = object
|
|
92
|
+
end
|
|
93
|
+
|
|
94
|
+
def run
|
|
95
|
+
validate_args_count
|
|
96
|
+
object.run_callbacks :initialize do
|
|
97
|
+
object_attributes_set
|
|
98
|
+
object_after_callback
|
|
99
|
+
end
|
|
100
|
+
end
|
|
101
|
+
|
|
102
|
+
private
|
|
103
|
+
|
|
104
|
+
def arg_value(arg_name)
|
|
105
|
+
arg_index = common_constructor.args.index(arg_name)
|
|
106
|
+
if arg_index < args.count
|
|
107
|
+
args[arg_index]
|
|
108
|
+
else
|
|
109
|
+
common_constructor.default_values[arg_index - common_constructor.args_count_min]
|
|
110
|
+
end
|
|
111
|
+
end
|
|
112
|
+
|
|
113
|
+
def object_after_callback
|
|
114
|
+
return unless common_constructor.after_set_block
|
|
115
|
+
|
|
116
|
+
object.instance_eval(&common_constructor.after_set_block)
|
|
117
|
+
end
|
|
118
|
+
|
|
119
|
+
def object_attributes_set
|
|
120
|
+
common_constructor.args.each do |arg_name|
|
|
121
|
+
object.send("#{arg_name}=", arg_value(arg_name))
|
|
122
|
+
end
|
|
123
|
+
end
|
|
124
|
+
|
|
125
|
+
def validate_args_count
|
|
126
|
+
return if common_constructor.args_count.include?(args.count)
|
|
127
|
+
|
|
128
|
+
raise ArgumentError, "#{object.class}.initialize: wrong number of arguments" \
|
|
129
|
+
" (given #{args.count}, expected #{common_constructor.args_count})"
|
|
130
|
+
end
|
|
131
|
+
end
|
|
132
|
+
end
|
|
133
|
+
end
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'active_support/core_ext/string'
|
|
4
|
+
require 'yaml'
|
|
5
|
+
require 'eac_ruby_utils/configs/file'
|
|
6
|
+
require 'eac_ruby_utils/patches/hash/sym_keys_hash'
|
|
7
|
+
require 'eac_ruby_utils/paths_hash'
|
|
8
|
+
require 'eac_ruby_utils/simple_cache'
|
|
9
|
+
|
|
10
|
+
module EacRubyUtils
|
|
11
|
+
class Configs
|
|
12
|
+
include ::EacRubyUtils::SimpleCache
|
|
13
|
+
|
|
14
|
+
attr_reader :configs_key, :options
|
|
15
|
+
|
|
16
|
+
# Valid options: [:storage_path]
|
|
17
|
+
def initialize(configs_key, options = {})
|
|
18
|
+
@configs_key = configs_key
|
|
19
|
+
@options = options.to_sym_keys_hash.freeze
|
|
20
|
+
load
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
def clear
|
|
24
|
+
file.clear
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
def save
|
|
28
|
+
file.save
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
def load
|
|
32
|
+
file.load
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
def []=(entry_key, entry_value)
|
|
36
|
+
write_entry(entry_key, entry_value)
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
def write_entry(entry_key, entry_value)
|
|
40
|
+
file.write(entry_key, entry_value)
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
def [](entry_key)
|
|
44
|
+
read_entry(entry_key)
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
def read_entry(entry_key)
|
|
48
|
+
file.read(entry_key)
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
def autosave?
|
|
52
|
+
file.autosave?
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
private
|
|
56
|
+
|
|
57
|
+
attr_accessor :data
|
|
58
|
+
|
|
59
|
+
def file_uncached
|
|
60
|
+
::EacRubyUtils::Configs::File.new(
|
|
61
|
+
storage_path, options
|
|
62
|
+
)
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
def storage_path_uncached
|
|
66
|
+
path = options_storage_path || default_storage_path
|
|
67
|
+
return path if ::File.exist?(path) && ::File.size(path).positive?
|
|
68
|
+
|
|
69
|
+
::FileUtils.mkdir_p(::File.dirname(path))
|
|
70
|
+
::File.write(path, {}.to_yaml)
|
|
71
|
+
path
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
def options_storage_path
|
|
75
|
+
options[:storage_path]
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
def default_storage_path
|
|
79
|
+
::File.join(ENV['HOME'], '.config', configs_key, 'settings.yml')
|
|
80
|
+
end
|
|
81
|
+
end
|
|
82
|
+
end
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'active_support/core_ext/string'
|
|
4
|
+
require 'yaml'
|
|
5
|
+
require 'eac_ruby_utils/patches/hash/sym_keys_hash'
|
|
6
|
+
require 'eac_ruby_utils/paths_hash'
|
|
7
|
+
require 'eac_ruby_utils/simple_cache'
|
|
8
|
+
|
|
9
|
+
module EacRubyUtils
|
|
10
|
+
class Configs
|
|
11
|
+
class File
|
|
12
|
+
include ::EacRubyUtils::SimpleCache
|
|
13
|
+
|
|
14
|
+
attr_reader :path, :options
|
|
15
|
+
|
|
16
|
+
# Valid options: [:autosave]
|
|
17
|
+
def initialize(path, options = {})
|
|
18
|
+
@path = path
|
|
19
|
+
@options = options.to_sym_keys_hash.freeze
|
|
20
|
+
load
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
def clear
|
|
24
|
+
self.data = ::EacRubyUtils::PathsHash.new({})
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
def save
|
|
28
|
+
::FileUtils.mkdir_p(::File.dirname(path))
|
|
29
|
+
::File.write(path, data.to_h.to_yaml)
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
def load
|
|
33
|
+
self.data = if ::File.exist?(path) && ::File.size(path).positive?
|
|
34
|
+
::EacRubyUtils::PathsHash.new(YAML.load_file(path))
|
|
35
|
+
else
|
|
36
|
+
{}
|
|
37
|
+
end
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
def []=(entry_key, entry_value)
|
|
41
|
+
write(entry_key, entry_value)
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
def write(entry_key, entry_value)
|
|
45
|
+
data[entry_key] = entry_value
|
|
46
|
+
save if autosave?
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
def [](entry_key)
|
|
50
|
+
read(entry_key)
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
def read(entry_key)
|
|
54
|
+
data[entry_key]
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
def autosave?
|
|
58
|
+
options[:autosave] ? true : false
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
private
|
|
62
|
+
|
|
63
|
+
attr_accessor :data
|
|
64
|
+
end
|
|
65
|
+
end
|
|
66
|
+
end
|