ehbrs-tools 0.14.0 → 0.16.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (140) hide show
  1. checksums.yaml +4 -4
  2. data/lib/ehbrs/runner.rb +4 -7
  3. data/lib/ehbrs/runner/finances.rb +4 -7
  4. data/lib/ehbrs/runner/finances/bb_browser.rb +5 -10
  5. data/lib/ehbrs/runner/fs.rb +4 -7
  6. data/lib/ehbrs/runner/fs/used_space.rb +4 -5
  7. data/lib/ehbrs/runner/google.rb +4 -7
  8. data/lib/ehbrs/runner/google/translate.rb +7 -12
  9. data/lib/ehbrs/runner/self.rb +4 -7
  10. data/lib/ehbrs/runner/self/test.rb +5 -10
  11. data/lib/ehbrs/runner/vg.rb +4 -7
  12. data/lib/ehbrs/runner/vg/ips.rb +9 -14
  13. data/lib/ehbrs/runner/vg/wii.rb +10 -13
  14. data/lib/ehbrs/runner/videos.rb +3 -5
  15. data/lib/ehbrs/runner/videos/extract.rb +10 -13
  16. data/lib/ehbrs/runner/videos/probe.rb +6 -9
  17. data/lib/ehbrs/runner/videos/series.rb +5 -8
  18. data/lib/ehbrs/runner/videos/series/rename.rb +9 -12
  19. data/lib/ehbrs/runner/videos/unsupported.rb +9 -13
  20. data/lib/ehbrs/runner/web_utils.rb +24 -0
  21. data/lib/ehbrs/runner/web_utils/videos.rb +18 -0
  22. data/lib/ehbrs/runner/web_utils/videos/download.rb +63 -0
  23. data/lib/ehbrs/runner/web_utils/videos/upload.rb +75 -0
  24. data/lib/ehbrs/tools/version.rb +1 -1
  25. data/lib/ehbrs/vg/wii/wit/parsers/dump.rb +10 -5
  26. data/lib/ehbrs/videos/file.rb +4 -8
  27. data/lib/ehbrs/videos/series/rename/file/options.rb +3 -13
  28. data/lib/ehbrs/videos/track.rb +20 -10
  29. data/lib/ehbrs/videos/unsupported/checks/codec_extra_unlisted.rb +2 -0
  30. data/lib/ehbrs/videos/unsupported/checks/codec_extra_unsupported.rb +2 -0
  31. data/lib/ehbrs/videos/unsupported/checks/codec_unlisted.rb +2 -0
  32. data/lib/ehbrs/videos/unsupported/checks/codec_unsupported.rb +2 -0
  33. data/lib/ehbrs/videos/unsupported/checks/invalid_extension.rb +2 -0
  34. data/lib/ehbrs/videos/unsupported/fix_profile.rb +1 -0
  35. data/lib/ehbrs/videos/unsupported/fixes/supported_codec.rb +1 -1
  36. data/lib/ehbrs/videos/unsupported/profiles/base.rb +17 -3
  37. data/lib/ehbrs/videos/unsupported/profiles/philco.rb +5 -4
  38. data/vendor/aranha-parsers/Gemfile +5 -0
  39. data/vendor/aranha-parsers/aranha-parsers.gemspec +23 -0
  40. data/vendor/aranha-parsers/lib/aranha/parsers.rb +9 -0
  41. data/vendor/aranha-parsers/lib/aranha/parsers/base.rb +79 -0
  42. data/vendor/aranha-parsers/lib/aranha/parsers/html.rb +11 -0
  43. data/vendor/aranha-parsers/lib/aranha/parsers/html/base.rb +47 -0
  44. data/vendor/aranha-parsers/lib/aranha/parsers/html/item.rb +24 -0
  45. data/vendor/aranha-parsers/lib/aranha/parsers/html/item_list.rb +29 -0
  46. data/vendor/aranha-parsers/lib/aranha/parsers/html/node.rb +13 -0
  47. data/vendor/aranha-parsers/lib/aranha/parsers/html/node/base.rb +36 -0
  48. data/vendor/aranha-parsers/lib/aranha/parsers/html/node/default.rb +126 -0
  49. data/vendor/aranha-parsers/lib/aranha/parsers/invalid_state_exception.rb +8 -0
  50. data/vendor/aranha-parsers/lib/aranha/parsers/patches.rb +11 -0
  51. data/vendor/aranha-parsers/lib/aranha/parsers/patches/ofx_parser.rb +38 -0
  52. data/vendor/aranha-parsers/lib/aranha/parsers/source_address.rb +55 -0
  53. data/vendor/aranha-parsers/lib/aranha/parsers/source_address/file.rb +31 -0
  54. data/vendor/aranha-parsers/lib/aranha/parsers/source_address/hash_http_get.rb +25 -0
  55. data/vendor/aranha-parsers/lib/aranha/parsers/source_address/hash_http_post.rb +45 -0
  56. data/vendor/aranha-parsers/lib/aranha/parsers/source_address/http_get.rb +49 -0
  57. data/vendor/aranha-parsers/lib/aranha/parsers/source_target_fixtures.rb +77 -0
  58. data/vendor/aranha-parsers/lib/aranha/parsers/spec/source_target_fixtures_example.rb +78 -0
  59. data/vendor/aranha-parsers/lib/aranha/parsers/version.rb +7 -0
  60. data/vendor/aranha-parsers/spec/lib/aranha/parsers/source_address/http_get_spec.rb +21 -0
  61. data/vendor/aranha-parsers/spec/lib/aranha/parsers/source_address_spec.rb +74 -0
  62. data/vendor/aranha-parsers/spec/lib/aranha/parsers/source_target_fixtures_spec.rb +27 -0
  63. data/vendor/aranha-parsers/spec/lib/aranha/parsers/source_target_fixtures_spec_files/stub1.source.txt +1 -0
  64. data/vendor/aranha-parsers/spec/lib/aranha/parsers/source_target_fixtures_spec_files/stub1.target.html +1 -0
  65. data/vendor/aranha-parsers/spec/lib/aranha/parsers/source_target_fixtures_spec_files/stub2.source.html +1 -0
  66. data/vendor/aranha-parsers/spec/lib/aranha/parsers/source_target_fixtures_spec_files/stub3.target.yaml +1 -0
  67. data/vendor/aranha-parsers/spec/lib/rubocop_check_spec.rb +7 -0
  68. data/vendor/aranha-parsers/spec/spec_helper.rb +8 -0
  69. data/vendor/eac_cli/eac_cli.gemspec +1 -1
  70. data/vendor/eac_cli/lib/eac_cli/definition.rb +49 -22
  71. data/vendor/eac_cli/lib/eac_cli/definition/alternative.rb +83 -0
  72. data/vendor/eac_cli/lib/eac_cli/definition/base_option.rb +17 -1
  73. data/vendor/eac_cli/lib/eac_cli/{parser/options_collection.rb → definition/help_formatter.rb} +20 -49
  74. data/vendor/eac_cli/lib/eac_cli/definition/positional_argument.rb +21 -4
  75. data/vendor/eac_cli/lib/eac_cli/docopt/doc_builder.rb +18 -40
  76. data/vendor/eac_cli/lib/eac_cli/docopt/doc_builder/alternative.rb +50 -0
  77. data/vendor/eac_cli/lib/eac_cli/docopt/runner_extension.rb +1 -0
  78. data/vendor/eac_cli/lib/eac_cli/parser.rb +21 -3
  79. data/vendor/eac_cli/lib/eac_cli/parser/alternative.rb +88 -0
  80. data/vendor/eac_cli/lib/eac_cli/parser/alternative/argv.rb +17 -0
  81. data/vendor/eac_cli/lib/eac_cli/parser/alternative/double_dash.rb +24 -0
  82. data/vendor/eac_cli/lib/eac_cli/parser/alternative/options.rb +58 -0
  83. data/vendor/eac_cli/lib/eac_cli/parser/alternative/positionals.rb +30 -0
  84. data/vendor/eac_cli/lib/eac_cli/parser/collector.rb +4 -0
  85. data/vendor/eac_cli/lib/eac_cli/patches/object/runner_with.rb +2 -1
  86. data/vendor/eac_cli/lib/eac_cli/runner.rb +7 -3
  87. data/vendor/eac_cli/lib/eac_cli/runner/context.rb +19 -2
  88. data/vendor/eac_cli/lib/eac_cli/runner_with/help.rb +1 -1
  89. data/vendor/eac_cli/lib/eac_cli/runner_with/output_file.rb +5 -1
  90. data/vendor/eac_cli/lib/eac_cli/runner_with/subcommands.rb +96 -0
  91. data/vendor/eac_cli/lib/eac_cli/version.rb +1 -1
  92. data/vendor/eac_cli/spec/lib/eac_cli/definition/alternative_spec.rb +14 -0
  93. data/vendor/eac_cli/spec/lib/eac_cli/docopt/runner_extension_spec.rb +35 -0
  94. data/vendor/eac_cli/spec/lib/eac_cli/parser/alternative_spec.rb +140 -0
  95. data/vendor/eac_cli/spec/lib/eac_cli/runner_spec.rb +57 -40
  96. data/vendor/eac_cli/spec/lib/eac_cli/runner_with/output_file_spec.rb +53 -0
  97. data/vendor/eac_cli/spec/lib/eac_cli/runner_with/subcommands_spec.rb +57 -0
  98. data/vendor/eac_ruby_utils/lib/eac_ruby_utils/abstract_methods.rb +60 -0
  99. data/vendor/eac_ruby_utils/lib/eac_ruby_utils/blank_not_blank.rb +19 -0
  100. data/vendor/eac_ruby_utils/lib/eac_ruby_utils/configs/base.rb +43 -0
  101. data/vendor/eac_ruby_utils/lib/eac_ruby_utils/configs/file.rb +12 -31
  102. data/vendor/eac_ruby_utils/lib/eac_ruby_utils/console/configs.rb +7 -104
  103. data/vendor/eac_ruby_utils/lib/eac_ruby_utils/console/configs/entry_reader.rb +81 -0
  104. data/vendor/eac_ruby_utils/lib/eac_ruby_utils/console/configs/password_entry_reader.rb +18 -0
  105. data/vendor/eac_ruby_utils/lib/eac_ruby_utils/console/configs/read_entry_options.rb +46 -0
  106. data/vendor/eac_ruby_utils/lib/eac_ruby_utils/console/configs/store_passwords_entry_reader.rb +27 -0
  107. data/vendor/eac_ruby_utils/lib/eac_ruby_utils/fs/clearable_directory.rb +57 -0
  108. data/vendor/eac_ruby_utils/lib/eac_ruby_utils/patches/enumerator.rb +4 -0
  109. data/vendor/eac_ruby_utils/lib/eac_ruby_utils/patches/enumerator/current.rb +9 -0
  110. data/vendor/eac_ruby_utils/lib/eac_ruby_utils/patches/enumerator/stopped.rb +14 -0
  111. data/vendor/eac_ruby_utils/lib/eac_ruby_utils/patches/module/abstract_methods.rb +10 -0
  112. data/vendor/eac_ruby_utils/lib/eac_ruby_utils/patches/object/debug.rb +17 -0
  113. data/vendor/eac_ruby_utils/lib/eac_ruby_utils/paths_hash.rb +21 -58
  114. data/vendor/eac_ruby_utils/lib/eac_ruby_utils/paths_hash/entry_key_error.rb +8 -0
  115. data/vendor/eac_ruby_utils/lib/eac_ruby_utils/paths_hash/node.rb +67 -0
  116. data/vendor/eac_ruby_utils/lib/eac_ruby_utils/paths_hash/path_search.rb +39 -0
  117. data/vendor/eac_ruby_utils/lib/eac_ruby_utils/ruby/command.rb +2 -1
  118. data/vendor/eac_ruby_utils/lib/eac_ruby_utils/struct.rb +4 -0
  119. data/vendor/eac_ruby_utils/lib/eac_ruby_utils/version.rb +1 -1
  120. data/vendor/eac_ruby_utils/lib/eac_ruby_utils/yaml.rb +3 -2
  121. data/vendor/eac_ruby_utils/spec/lib/eac_ruby_utils/abstract_methods_spec.rb +28 -0
  122. data/vendor/eac_ruby_utils/spec/lib/eac_ruby_utils/blank_not_blank_spec.rb +16 -0
  123. data/vendor/eac_ruby_utils/spec/lib/eac_ruby_utils/configs_spec.rb +15 -0
  124. data/vendor/eac_ruby_utils/spec/lib/eac_ruby_utils/patches/enumerator/current_spec.rb +26 -0
  125. data/vendor/eac_ruby_utils/spec/lib/eac_ruby_utils/patches/enumerator/stopped_spec.rb +32 -0
  126. data/vendor/eac_ruby_utils/spec/lib/eac_ruby_utils/paths_hash_spec.rb +52 -13
  127. data/vendor/ehbrs_ruby_utils/lib/ehbrs_ruby_utils/version.rb +1 -1
  128. data/vendor/ehbrs_ruby_utils/lib/ehbrs_ruby_utils/videos/container.rb +30 -2
  129. data/vendor/ehbrs_ruby_utils/lib/ehbrs_ruby_utils/videos/convert_job.rb +91 -0
  130. data/vendor/ehbrs_ruby_utils/lib/ehbrs_ruby_utils/videos/stream.rb +51 -0
  131. data/vendor/ehbrs_ruby_utils/lib/ehbrs_ruby_utils/web_utils.rb +9 -0
  132. data/vendor/ehbrs_ruby_utils/lib/ehbrs_ruby_utils/web_utils/instance.rb +32 -0
  133. data/vendor/ehbrs_ruby_utils/lib/ehbrs_ruby_utils/web_utils/videos.rb +11 -0
  134. data/vendor/ehbrs_ruby_utils/lib/ehbrs_ruby_utils/web_utils/videos/file.rb +40 -0
  135. data/vendor/ehbrs_ruby_utils/lib/ehbrs_ruby_utils/web_utils/videos/files_list.rb +76 -0
  136. metadata +78 -7
  137. data/vendor/eac_cli/lib/eac_cli/parser/parse_result.rb +0 -21
  138. data/vendor/eac_cli/lib/eac_cli/parser/positional_collection.rb +0 -49
  139. data/vendor/ehbrs_ruby_utils/lib/ehbrs_ruby_utils/videos/container/file.rb +0 -31
  140. data/vendor/ehbrs_ruby_utils/lib/ehbrs_ruby_utils/videos/container/info.rb +0 -21
@@ -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,57 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'fileutils'
4
+ require 'pathname'
5
+
6
+ module EacRubyUtils
7
+ module Fs
8
+ class ClearableDirectory < ::Pathname
9
+ CLEARABLE_BASENAME = '.clearable_directory'
10
+
11
+ def clear
12
+ validate_clearable
13
+ directory? ? clear_directory : clear_no_directory
14
+ mkpath
15
+ ::FileUtils.touch(clearable_note_file.to_path)
16
+ self
17
+ end
18
+
19
+ def clearable?
20
+ clearable_negate_message ? true : false
21
+ end
22
+
23
+ def clearable_negate_message
24
+ return if !exist? || empty?
25
+ return "Path \"#{self}\" exists, is not empty and is not a directory" unless directory?
26
+ return if clearable_note_file.exist?
27
+
28
+ "Directory \"#{self}\" is not empty and does not have a #{CLEARABLE_BASENAME} file"
29
+ end
30
+
31
+ def clearable_note_file
32
+ join(CLEARABLE_BASENAME)
33
+ end
34
+
35
+ def validate_clearable
36
+ message = clearable_negate_message
37
+ raise message if message
38
+ end
39
+
40
+ private
41
+
42
+ def clear_directory
43
+ children.each do |child|
44
+ if child.directory?
45
+ child.rmtree
46
+ elsif child.file?
47
+ child.unlink
48
+ end
49
+ end
50
+ end
51
+
52
+ def clear_no_directory
53
+ ::FileUtils.rm_rf(to_path)
54
+ end
55
+ end
56
+ end
57
+ 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,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'eac_ruby_utils/abstract_methods'
4
+
5
+ class Module
6
+ def enable_abstract_methods(*methods)
7
+ include ::EacRubyUtils::AbstractMethods
8
+ abstract_methods(*methods)
9
+ end
10
+ 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,21 +1,25 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'active_support/core_ext/object'
4
- require 'eac_ruby_utils/patches/object/asserts'
3
+ require 'eac_ruby_utils/core_ext'
5
4
 
6
5
  module EacRubyUtils
7
6
  class PathsHash
7
+ require_sub __FILE__
8
+
8
9
  class << self
9
10
  def parse_entry_key(entry_key)
10
11
  r = entry_key.to_s.strip
11
- raise EntryKeyError, 'Entry key cannot start or end with dot' if
12
+ raise ::EacRubyUtils::PathsHash::EntryKeyError, 'Entry key cannot start or end with dot' if
12
13
  r.start_with?('.') || r.end_with?('.')
13
14
 
14
15
  r = r.split('.').map(&:strip)
15
- raise EntryKeyError, "Entry key \"#{entry_key}\" is empty" if r.empty?
16
+ if r.empty?
17
+ raise ::EacRubyUtils::PathsHash::EntryKeyError, "Entry key \"#{entry_key}\" is empty"
18
+ end
16
19
  return r.map(&:to_sym) unless r.any?(&:blank?)
17
20
 
18
- raise EntryKeyError, "Entry key \"#{entry_key}\" has at least one blank part"
21
+ raise ::EacRubyUtils::PathsHash::EntryKeyError,
22
+ "Entry key \"#{entry_key}\" has at least one blank part"
19
23
  end
20
24
  end
21
25
 
@@ -26,68 +30,27 @@ module EacRubyUtils
26
30
  end
27
31
 
28
32
  def [](entry_key)
29
- root.read_entry(self.class.parse_entry_key(entry_key), [])
33
+ root.read_entry(::EacRubyUtils::PathsHash::PathSearch.parse_entry_key(entry_key))
30
34
  end
31
35
 
32
36
  def []=(entry_key, entry_value)
33
- root.write_entry(self.class.parse_entry_key(entry_key), entry_value, [])
37
+ root.write_entry(
38
+ ::EacRubyUtils::PathsHash::PathSearch.parse_entry_key(entry_key), entry_value
39
+ )
34
40
  end
35
41
 
36
- delegate :to_h, to: :root
37
-
38
- private
39
-
40
- attr_reader :data
41
-
42
- class EntryKeyError < StandardError
42
+ def fetch(entry_key)
43
+ root.fetch(::EacRubyUtils::PathsHash::PathSearch.parse_entry_key(entry_key))
43
44
  end
44
45
 
45
- class Node
46
- def initialize(source_hash)
47
- source_hash.assert_argument(Hash, 'source_hash')
48
- @data = source_hash.map { |k, v| [k.to_sym, v.is_a?(Hash) ? Node.new(v) : v] }.to_h
49
- end
50
-
51
- def to_h
52
- data.map { |k, v| [k, v.is_a?(Node) ? v.to_h : v] }.to_h
53
- end
54
-
55
- def read_entry(path, current)
56
- validate_path(path, current)
57
- node_key = path.shift
58
- node = data[node_key]
59
- return (node.is_a?(Node) ? node.to_h : node) if path.empty?
60
- return nil if node.blank?
61
- return node.read_entry(path, current + [node_key]) if node.is_a?(Node)
62
-
63
- raise(EntryKeyError,
64
- "Path #{current.join(',')} is not a Node and path continues (#{current + path})")
65
- end
66
-
67
- def write_entry(path, value, current)
68
- validate_path(path, current)
69
- node_key = path.shift
70
- write_entry_value(path, node_key, value, current)
71
- end
72
-
73
- private
46
+ def key?(entry_key)
47
+ root.entry?(::EacRubyUtils::PathsHash::PathSearch.parse_entry_key(entry_key))
48
+ end
74
49
 
75
- def write_entry_value(path, node_key, value, current)
76
- if path.empty?
77
- data[node_key] = value.is_a?(Hash) ? Node.new(value) : value
78
- else
79
- data[node_key] = Node.new({}) unless data[node_key].is_a?(Node)
80
- data[node_key].write_entry(path, value, current + [node_key])
81
- end
82
- end
50
+ delegate :to_h, to: :root
83
51
 
84
- def validate_path(path, current)
85
- path.assert_argument(Array, 'path')
86
- current.assert_argument(Array, 'current')
87
- raise EntryKeyError, 'Path is empty' if path.empty?
88
- end
52
+ private
89
53
 
90
- attr_reader :data
91
- end
54
+ attr_reader :data
92
55
  end
93
56
  end
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ module EacRubyUtils
4
+ class PathsHash
5
+ class EntryKeyError < StandardError
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,67 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'eac_ruby_utils/core_ext'
4
+ require 'eac_ruby_utils/paths_hash/entry_key_error'
5
+
6
+ module EacRubyUtils
7
+ class PathsHash
8
+ class Node
9
+ PATH_SEARCH_UNENDED_ERROR_MESSAGE = 'Path search is not a Node and is not ended'
10
+
11
+ def initialize(source_hash)
12
+ source_hash.assert_argument(Hash, 'source_hash')
13
+ @data = source_hash.map { |k, v| [k.to_sym, v.is_a?(Hash) ? Node.new(v) : v] }.to_h
14
+ end
15
+
16
+ def entry?(path_search)
17
+ return false unless data.key?(path_search.cursor)
18
+ return true if path_search.ended?
19
+ return data.fetch(path_search.cursor).entry?(path_search.succeeding) if
20
+ data.fetch(path_search.cursor).is_a?(Node)
21
+
22
+ false # Paths continue and there is not available nodes
23
+ end
24
+
25
+ def fetch(path_search)
26
+ if data.key?(path_search.cursor)
27
+ node = data.fetch(path_search.cursor)
28
+ return (node.is_a?(Node) ? node.to_h : node) if path_search.ended?
29
+ return nil if node.blank?
30
+ return node.fetch(path_search.succeeding) if node.is_a?(Node)
31
+ end
32
+
33
+ path_search.raise_error(PATH_SEARCH_UNENDED_ERROR_MESSAGE)
34
+ end
35
+
36
+ def to_h
37
+ data.map { |k, v| [k, v.is_a?(Node) ? v.to_h : v] }.to_h
38
+ end
39
+
40
+ def read_entry(path_search)
41
+ node = data[path_search.cursor]
42
+ return (node.is_a?(Node) ? node.to_h : node) if path_search.ended?
43
+ return nil if node.blank?
44
+ return node.read_entry(path_search.succeeding) if node.is_a?(Node)
45
+
46
+ path_search.raise_error(PATH_SEARCH_UNENDED_ERROR_MESSAGE)
47
+ end
48
+
49
+ def write_entry(path_search, value)
50
+ if path_search.ended?
51
+ data[path_search.cursor] = value.is_a?(Hash) ? self.class.new(value) : value
52
+ else
53
+ assert_data_node(path_search.cursor).write_entry(path_search.succeeding, value)
54
+ end
55
+ end
56
+
57
+ private
58
+
59
+ attr_reader :data
60
+
61
+ def assert_data_node(key)
62
+ data[key] = self.class.new({}) unless data[key].is_a?(Node)
63
+ data[key]
64
+ end
65
+ end
66
+ end
67
+ end