avm-tools 0.94.2 → 0.98.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (173) hide show
  1. checksums.yaml +4 -4
  2. data/lib/avm/app_src.rb +60 -0
  3. data/lib/avm/core_ext.rb +4 -0
  4. data/lib/avm/docker/runner.rb +22 -30
  5. data/lib/avm/eac_asciidoctor_base0/deploy.rb +5 -5
  6. data/lib/avm/eac_rails_base1/runner/bundle.rb +0 -1
  7. data/lib/avm/eac_rails_base1/runner/code_runner.rb +0 -1
  8. data/lib/avm/eac_rails_base1/runner/rails_server.rb +0 -1
  9. data/lib/avm/eac_webapp_base0/instance.rb +5 -2
  10. data/lib/avm/eac_webapp_base0/instance/subcommand_parent.rb +24 -0
  11. data/lib/avm/eac_webapp_base0/runner/data.rb +1 -0
  12. data/lib/avm/eac_webapp_base0/runner/data/dump.rb +1 -1
  13. data/lib/avm/eac_webapp_base0/runner/data/load.rb +1 -1
  14. data/lib/avm/git/issue/complete/_test.rb +2 -2
  15. data/lib/avm/instances/base.rb +6 -12
  16. data/lib/avm/instances/base/entry_keys.rb +17 -0
  17. data/lib/{eac_launcher → avm/launcher}/instances/error.rb +0 -0
  18. data/lib/avm/launcher/instances/runner_helper.rb +41 -0
  19. data/lib/avm/patches/class.rb +4 -0
  20. data/lib/avm/patches/eac_ruby_gems_utils.rb +4 -0
  21. data/lib/avm/patches/eac_ruby_gems_utils/gem.rb +2 -2
  22. data/lib/avm/patches/object.rb +4 -0
  23. data/lib/avm/patches/object/fs_cache.rb +16 -0
  24. data/lib/avm/ruby/bundler.rb +11 -0
  25. data/lib/avm/ruby/bundler/incompatible_parser.rb +25 -0
  26. data/lib/avm/ruby/bundler/incompatible_parser/depends_on.rb +25 -0
  27. data/lib/avm/ruby/bundler/incompatible_parser/gem_conflict.rb +39 -0
  28. data/lib/avm/ruby/bundler/incompatible_parser/in_gemfile.rb +19 -0
  29. data/lib/avm/ruby/bundler/incompatible_parser/line_buffer.rb +73 -0
  30. data/lib/avm/ruby/bundler/incompatible_parser/line_factory.rb +31 -0
  31. data/lib/avm/ruby/bundler/incompatible_parser/line_parser_base.rb +19 -0
  32. data/lib/avm/ruby/bundler/incompatible_parser/version_requirement.rb +26 -0
  33. data/lib/avm/ruby/gems/generator.rb +1 -5
  34. data/lib/avm/ruby/rubocop/_configured.rb +2 -2
  35. data/lib/avm/tools/runner/{local_project.rb → app_src.rb} +3 -3
  36. data/lib/avm/tools/runner/app_src/eac_asciidoctor_base0.rb +14 -0
  37. data/lib/avm/tools/runner/{local_project → app_src}/eac_asciidoctor_base0/build.rb +19 -7
  38. data/lib/avm/tools/runner/{local_project → app_src}/eac_writings_base0.rb +1 -2
  39. data/lib/avm/tools/runner/{local_project → app_src}/eac_writings_base0/build_chapters.rb +1 -2
  40. data/lib/avm/tools/runner/{local_project → app_src}/eac_writings_base0/build_single.rb +1 -2
  41. data/lib/avm/tools/runner/{local_project → app_src}/eac_writings_base0/info.rb +1 -2
  42. data/lib/avm/tools/runner/app_src/info.rb +22 -0
  43. data/lib/avm/tools/runner/{local_project → app_src}/ruby.rb +1 -1
  44. data/lib/avm/tools/runner/{local_project → app_src}/ruby/bundler.rb +1 -1
  45. data/lib/avm/tools/runner/{local_project → app_src}/ruby/bundler/gemfile_lock.rb +1 -1
  46. data/lib/avm/tools/runner/app_src/ruby/bundler/incompatible.rb +84 -0
  47. data/lib/avm/tools/runner/{local_project → app_src}/test.rb +6 -9
  48. data/lib/avm/tools/runner/app_src/update.rb +22 -0
  49. data/lib/avm/tools/runner/{local_project → app_src}/version_bump.rb +1 -1
  50. data/lib/avm/tools/runner/eac_asciidoctor_base0.rb +0 -1
  51. data/lib/avm/tools/runner/eac_rails_base0.rb +0 -1
  52. data/lib/avm/tools/runner/eac_redmine_base0.rb +0 -1
  53. data/lib/avm/tools/runner/eac_wordpress_base0.rb +0 -1
  54. data/lib/avm/tools/runner/eac_writings_base0.rb +0 -1
  55. data/lib/avm/tools/runner/git.rb +0 -1
  56. data/lib/avm/tools/runner/git/commit.rb +0 -1
  57. data/lib/avm/tools/runner/git/dirty_files.rb +17 -22
  58. data/lib/avm/tools/runner/git/issue.rb +21 -36
  59. data/lib/avm/tools/runner/git/organize.rb +6 -9
  60. data/lib/avm/tools/runner/git/revisions_test.rb +14 -25
  61. data/lib/avm/tools/runner/git/subrepo.rb +3 -6
  62. data/lib/avm/tools/runner/git/subrepo/check.rb +10 -13
  63. data/lib/avm/tools/runner/git/subrepo/clone.rb +2 -3
  64. data/lib/avm/tools/runner/git/subrepo/fix.rb +2 -3
  65. data/lib/avm/tools/runner/launcher.rb +3 -6
  66. data/lib/avm/tools/runner/launcher/instances.rb +10 -21
  67. data/lib/avm/tools/runner/launcher/projects.rb +10 -18
  68. data/lib/avm/tools/runner/launcher/publish.rb +18 -26
  69. data/lib/avm/tools/runner/ruby/rubocop.rb +9 -25
  70. data/lib/avm/tools/runner/self.rb +7 -15
  71. data/lib/avm/tools/runner/self/docker.rb +1 -1
  72. data/lib/avm/tools/version.rb +1 -1
  73. data/lib/eac_launcher/instances.rb +1 -1
  74. data/template/avm/ruby/gems/generator/static/spec/rubocop_spec.rb +1 -5
  75. data/template/avm/ruby/gems/generator/static/spec/spec_helper.rb +2 -0
  76. data/vendor/avm-apps/avm-apps.gemspec +1 -1
  77. data/vendor/avm-apps/lib/avm/apps/sources/configuration.rb +57 -0
  78. data/vendor/avm-apps/lib/avm/apps/sources/configuration/_locale.rb +18 -0
  79. data/vendor/avm-apps/lib/avm/apps/sources/configuration/_rubocop.rb +26 -0
  80. data/vendor/avm-apps/lib/avm/apps/sources/configuration/_tests.rb +29 -0
  81. data/vendor/avm-apps/lib/avm/apps/version.rb +1 -1
  82. data/vendor/avm-apps/spec/rubocop_spec.rb +1 -5
  83. data/vendor/avm-apps/spec/spec_helper.rb +3 -0
  84. data/vendor/avm-eac_asciidoctor_base0/Gemfile +5 -0
  85. data/vendor/avm-eac_asciidoctor_base0/avm-eac_asciidoctor_base0.gemspec +20 -0
  86. data/{lib/avm/local_projects.rb → vendor/avm-eac_asciidoctor_base0/lib/avm/eac_asciidoctor_base0.rb} +1 -1
  87. data/{lib/avm/eac_asciidoctor_base0/project.rb → vendor/avm-eac_asciidoctor_base0/lib/avm/eac_asciidoctor_base0/sources.rb} +2 -4
  88. data/vendor/avm-eac_asciidoctor_base0/lib/avm/eac_asciidoctor_base0/sources/base.rb +19 -0
  89. data/vendor/avm-eac_asciidoctor_base0/lib/avm/eac_asciidoctor_base0/sources/build.rb +49 -0
  90. data/vendor/avm-eac_asciidoctor_base0/lib/avm/eac_asciidoctor_base0/sources/build/file.rb +30 -0
  91. data/vendor/avm-eac_asciidoctor_base0/lib/avm/eac_asciidoctor_base0/sources/runner.rb +30 -0
  92. data/vendor/avm-eac_asciidoctor_base0/lib/avm/eac_asciidoctor_base0/sources/runner/build.rb +54 -0
  93. data/vendor/avm-eac_asciidoctor_base0/lib/avm/eac_asciidoctor_base0/version.rb +7 -0
  94. data/vendor/avm-eac_asciidoctor_base0/spec/rubocop_spec.rb +3 -0
  95. data/vendor/avm-eac_asciidoctor_base0/spec/spec_helper.rb +102 -0
  96. data/vendor/eac_cli/eac_cli.gemspec +1 -1
  97. data/vendor/eac_cli/lib/eac_cli/definition/base_option.rb +5 -1
  98. data/vendor/eac_cli/lib/eac_cli/docopt/doc_builder.rb +3 -1
  99. data/vendor/eac_cli/lib/eac_cli/runner/context.rb +2 -1
  100. data/vendor/eac_cli/lib/eac_cli/runner/instance_methods.rb +8 -1
  101. data/vendor/eac_cli/lib/eac_cli/version.rb +1 -1
  102. data/vendor/eac_cli/spec/rubocop_spec.rb +1 -5
  103. data/vendor/eac_cli/spec/spec_helper.rb +3 -0
  104. data/vendor/eac_docker/eac_docker.gemspec +1 -1
  105. data/vendor/eac_docker/lib/eac_docker/version.rb +1 -1
  106. data/vendor/eac_docker/spec/rubocop_spec.rb +1 -5
  107. data/vendor/eac_docker/spec/spec_helper.rb +3 -0
  108. data/vendor/eac_git/eac_git.gemspec +2 -1
  109. data/vendor/eac_git/lib/eac_git/local.rb +4 -0
  110. data/vendor/eac_git/lib/eac_git/local/commit.rb +53 -0
  111. data/vendor/eac_git/lib/eac_git/local/commit/changed_file.rb +46 -0
  112. data/vendor/eac_git/lib/eac_git/local/commit/diff_tree_line.rb +32 -0
  113. data/vendor/eac_git/lib/eac_git/rspec.rb +6 -1
  114. data/{lib/avm/git/spec_helper.rb → vendor/eac_git/lib/eac_git/rspec/stubbed_git_local_repo.rb} +16 -13
  115. data/vendor/eac_git/lib/eac_git/version.rb +1 -1
  116. data/vendor/eac_git/spec/lib/eac_git/local/commit/diff_tree_line_spec.rb +13 -0
  117. data/vendor/eac_git/spec/lib/eac_git/local/commit/diff_tree_line_spec_files/add.source.out +1 -0
  118. data/vendor/eac_git/spec/lib/eac_git/local/commit/diff_tree_line_spec_files/add.target.yaml +7 -0
  119. data/vendor/eac_git/spec/lib/eac_git/local/commit/diff_tree_line_spec_files/modify.source.out +1 -0
  120. data/vendor/eac_git/spec/lib/eac_git/local/commit/diff_tree_line_spec_files/modify.target.yaml +7 -0
  121. data/vendor/eac_git/spec/lib/eac_git/local/commit/diff_tree_line_spec_files/remove.source.out +1 -0
  122. data/vendor/eac_git/spec/lib/eac_git/local/commit/diff_tree_line_spec_files/remove.target.yaml +7 -0
  123. data/vendor/eac_git/spec/lib/eac_git/local/commit/diff_tree_line_spec_files/rename.source.out +2 -0
  124. data/vendor/eac_git/spec/lib/eac_git/local/commit/diff_tree_line_spec_files/rename.target.yaml +13 -0
  125. data/vendor/eac_git/spec/lib/eac_git/local/commit/diff_tree_line_spec_files/rename_modify.source.out +2 -0
  126. data/vendor/eac_git/spec/lib/eac_git/local/commit/diff_tree_line_spec_files/rename_modify.target.yaml +13 -0
  127. data/vendor/eac_git/spec/lib/eac_git/local/commit_spec.rb +58 -0
  128. data/vendor/eac_git/spec/rubocop_spec.rb +1 -5
  129. data/vendor/eac_git/spec/spec_helper.rb +3 -0
  130. data/vendor/eac_ruby_base0/eac_ruby_base0.gemspec +4 -4
  131. data/vendor/eac_ruby_base0/lib/eac_ruby_base0/runner_with/input.rb +36 -0
  132. data/vendor/eac_ruby_base0/lib/eac_ruby_base0/runner_with/output.rb +58 -0
  133. data/vendor/eac_ruby_base0/lib/eac_ruby_base0/version.rb +1 -1
  134. data/vendor/eac_ruby_base0/spec/lib/eac_ruby_base0/runner_with/output_spec.rb +81 -0
  135. data/vendor/eac_ruby_base0/spec/rubocop_spec.rb +1 -5
  136. data/vendor/eac_ruby_base0/spec/spec_helper.rb +3 -0
  137. data/vendor/eac_ruby_gems_utils/eac_ruby_gems_utils.gemspec +4 -3
  138. data/vendor/eac_ruby_gems_utils/lib/eac_ruby_gems_utils/tests/base.rb +7 -10
  139. data/vendor/eac_ruby_gems_utils/lib/eac_ruby_gems_utils/tests/multiple.rb +9 -2
  140. data/vendor/eac_ruby_gems_utils/lib/eac_ruby_gems_utils/version.rb +1 -1
  141. data/vendor/eac_ruby_gems_utils/spec/code/rubocop_check_spec.rb +1 -5
  142. data/vendor/eac_ruby_gems_utils/spec/spec_helper.rb +3 -0
  143. data/vendor/eac_ruby_utils/eac_ruby_utils.gemspec +2 -1
  144. data/vendor/eac_ruby_utils/lib/eac_ruby_utils/filesystem_cache.rb +6 -0
  145. data/vendor/eac_ruby_utils/lib/eac_ruby_utils/fs/logs.rb +63 -0
  146. data/vendor/eac_ruby_utils/lib/eac_ruby_utils/inflector.rb +9 -1
  147. data/vendor/eac_ruby_utils/lib/eac_ruby_utils/listable/list.rb +3 -2
  148. data/vendor/eac_ruby_utils/lib/eac_ruby_utils/patches/kernel/nyi.rb +4 -2
  149. data/vendor/eac_ruby_utils/lib/eac_ruby_utils/patches/regexp/to_parser.rb +10 -0
  150. data/vendor/eac_ruby_utils/lib/eac_ruby_utils/patches/string/inflector.rb +4 -2
  151. data/vendor/eac_ruby_utils/lib/eac_ruby_utils/regexp_parser.rb +34 -0
  152. data/vendor/eac_ruby_utils/lib/eac_ruby_utils/settings_provider.rb +10 -29
  153. data/vendor/eac_ruby_utils/lib/eac_ruby_utils/settings_provider/setting_value.rb +69 -0
  154. data/vendor/eac_ruby_utils/lib/eac_ruby_utils/simple_cache.rb +18 -5
  155. data/vendor/eac_ruby_utils/lib/eac_ruby_utils/version.rb +1 -1
  156. data/vendor/eac_ruby_utils/spec/lib/eac_ruby_utils/settings_provider_spec.rb +8 -0
  157. data/vendor/eac_ruby_utils/spec/lib/eac_ruby_utils/simple_cache_spec.rb +9 -0
  158. data/vendor/eac_ruby_utils/spec/rubocop_check_spec.rb +1 -5
  159. data/vendor/eac_ruby_utils/spec/spec_helper.rb +3 -0
  160. metadata +120 -39
  161. data/lib/avm/eac_asciidoctor_base0/build.rb +0 -47
  162. data/lib/avm/eac_asciidoctor_base0/build/file.rb +0 -28
  163. data/lib/avm/instances/configuration.rb +0 -55
  164. data/lib/avm/instances/configuration/_locale.rb +0 -16
  165. data/lib/avm/instances/configuration/_rubocop.rb +0 -24
  166. data/lib/avm/instances/configuration/_tests.rb +0 -27
  167. data/lib/avm/local_projects/instance.rb +0 -62
  168. data/lib/avm/tools/runner/local_project/eac_asciidoctor_base0.rb +0 -32
  169. data/lib/avm/tools/runner/local_project/info.rb +0 -25
  170. data/lib/avm/tools/runner/local_project/update.rb +0 -25
  171. data/lib/eac_launcher/instances/runner_helper.rb +0 -42
  172. data/vendor/eac_cli/lib/eac_cli/runner_with/output_file.rb +0 -30
  173. data/vendor/eac_cli/spec/lib/eac_cli/runner_with/output_file_spec.rb +0 -53
@@ -27,6 +27,12 @@ module EacRubyGemsUtils
27
27
  decorated_gems.flat_map(&:tests)
28
28
  end
29
29
 
30
+ def clear_logs
31
+ all_tests.each do |test|
32
+ test.logs.remove_all
33
+ end
34
+ end
35
+
30
36
  def prepare_all_gems
31
37
  infom 'Preparing all gems...'
32
38
  decorated_gems.each(&:prepare)
@@ -47,8 +53,7 @@ module EacRubyGemsUtils
47
53
  warn 'Some test did not pass:'
48
54
  failed_tests.each do |test|
49
55
  infov ' * Test', test
50
- infov ' * STDOUT', test.stdout_cache.content_path
51
- infov ' * STDERR', test.stderr_cache.content_path
56
+ info test.logs.truncate_all
52
57
  end
53
58
  else
54
59
  success 'All tests passed'
@@ -60,6 +65,8 @@ module EacRubyGemsUtils
60
65
  prepare_all_gems
61
66
  test_all_gems
62
67
  final_results_banner
68
+ ensure
69
+ clear_logs
63
70
  end
64
71
 
65
72
  def start_banner
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module EacRubyGemsUtils
4
- VERSION = '0.8.0'
4
+ VERSION = '0.9.2'
5
5
  end
@@ -1,7 +1,3 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'eac_ruby_gem_support/spec/examples/rubocop_check'
4
-
5
- RSpec.describe ::RuboCop do
6
- include_examples 'rubocop_check', ::File.expand_path('../..', __dir__)
7
- end
3
+ ::EacRubyGemSupport::Rspec.default.describe_rubocop
@@ -97,4 +97,7 @@ RSpec.configure do |config|
97
97
  # # test failures related to randomization by passing the same `--seed` value
98
98
  # # as the one that triggered the failure.
99
99
  # Kernel.srand config.seed
100
+
101
+ require 'eac_ruby_gem_support/rspec'
102
+ ::EacRubyGemSupport::Rspec.setup(::File.expand_path('..', __dir__), config)
100
103
  end
@@ -19,6 +19,7 @@ Gem::Specification.new do |s|
19
19
  s.add_dependency 'addressable', '~> 2.6'
20
20
  s.add_dependency 'colorize', '~> 0.8.1'
21
21
  s.add_dependency 'docopt', '~> 0.6.1'
22
+ s.add_dependency 'filesize'
22
23
  s.add_dependency 'net-ssh', '~> 4.2'
23
- s.add_development_dependency 'eac_ruby_gem_support', '~> 0.1', '>= 0.1.1'
24
+ s.add_development_dependency 'eac_ruby_gem_support', '~> 0.2'
24
25
  end
@@ -24,6 +24,12 @@ module EacRubyUtils
24
24
  ::File.read(content_path)
25
25
  end
26
26
 
27
+ def read_or_cache
28
+ write(yield) unless cached?
29
+
30
+ read
31
+ end
32
+
27
33
  def write(value)
28
34
  assert_directory_on_path
29
35
  ::File.write(content_path, value)
@@ -0,0 +1,63 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'active_support/core_ext/string/filters'
4
+ require 'eac_ruby_utils/fs/temp'
5
+ require 'filesize'
6
+
7
+ module EacRubyUtils
8
+ module Fs
9
+ class Logs
10
+ TRUNCATE_DEFAULT_LENGTH = 1000
11
+ TRUNCATE_APPEND_TEXT = '(...) '
12
+
13
+ def [](label)
14
+ log_set.fetch(sanitize_label(label))
15
+ end
16
+
17
+ def add(label)
18
+ log_set[sanitize_label(label)] = ::EacRubyUtils::Fs::Temp.file
19
+
20
+ self
21
+ end
22
+
23
+ def remove_all
24
+ log_set.each_key { |label| remove(label) }
25
+
26
+ self
27
+ end
28
+
29
+ def remove(label)
30
+ log_set.fetch(sanitize_label(label)).remove
31
+ log_set.delete(sanitize_label(label))
32
+ end
33
+
34
+ def truncate(label, length = TRUNCATE_DEFAULT_LENGTH)
35
+ content = self[label].read.strip
36
+ return content if content.length <= TRUNCATE_DEFAULT_LENGTH
37
+
38
+ TRUNCATE_APPEND_TEXT + content[content.length - length + TRUNCATE_APPEND_TEXT.length,
39
+ length - TRUNCATE_APPEND_TEXT.length]
40
+ end
41
+
42
+ def truncate_all(length = TRUNCATE_DEFAULT_LENGTH)
43
+ s = "Files: #{log_set.length}\n"
44
+ log_set.each do |label, path|
45
+ x = truncate(label, length)
46
+ y = [label, path, ::Filesize.from("#{path.size} B").pretty].join(' / ')
47
+ s += x.blank? ? ">>> #{y} (Blank) <<<" : ">>> #{y}\n#{x}\n<<< #{y}\n"
48
+ end
49
+ s
50
+ end
51
+
52
+ private
53
+
54
+ def sanitize_label(label)
55
+ label.to_sym
56
+ end
57
+
58
+ def log_set
59
+ @log_set ||= {}
60
+ end
61
+ end
62
+ end
63
+ end
@@ -7,11 +7,19 @@ module EacRubyUtils
7
7
  class << self
8
8
  VARIABLE_NAME_PATTERN = /[_a-z][_a-z0-9]*/i.freeze
9
9
 
10
- def variableize(string)
10
+ # Convert a string to a variable format: first character as a lowercase letter or underscore
11
+ # and other as a lowercase letter, underscore or numbers.
12
+ # @param string [String] The source string.
13
+ # @param validate [Boolean] Affect the outcome when the result builded is not in a valid
14
+ # variable format. If `true`, it raises a {ArgumentError}. If `false`, return `nil`.
15
+ # @return [String, nil]
16
+ # @raise [ArgumentError]
17
+ def variableize(string, validate = true)
11
18
  r = ::ActiveSupport::Inflector.transliterate(string).gsub(/[^_a-z0-9]/i, '_')
12
19
  .gsub(/_+/, '_').gsub(/_\z/, '').gsub(/\A_/, '').downcase
13
20
  m = VARIABLE_NAME_PATTERN.match(r)
14
21
  return r if m
22
+ return nil unless validate
15
23
 
16
24
  raise ::ArgumentError, "Invalid variable name \"#{r}\" was generated " \
17
25
  "from string \"#{string}\""
@@ -54,8 +54,9 @@ module EacRubyUtils
54
54
  end
55
55
 
56
56
  def value_validate!(value, error_class = ::StandardError)
57
- value_valid?(value) ||
58
- raise(error_class, "Invalid value: \"#{value}\" (Valid: #{values_to_s})")
57
+ return value if value_valid?(value)
58
+
59
+ raise(error_class, "Invalid value: \"#{value}\" (Valid: #{values_to_s})")
59
60
  end
60
61
 
61
62
  def values_to_s
@@ -2,7 +2,9 @@
2
2
 
3
3
  module Kernel
4
4
  # Raise exception with text "Not yet implemented".
5
- def nyi
6
- raise "Not yet implemented (Called in #{caller.first})"
5
+ def nyi(message = nil)
6
+ s = "Not yet implemented (Called in #{caller.first})"
7
+ s += ": #{message}" if message
8
+ raise s
7
9
  end
8
10
  end
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'eac_ruby_utils/regexp_parser'
4
+
5
+ class Regexp
6
+ # @return [::EacRubyUtils::RegexpParser]
7
+ def to_parser(&block)
8
+ ::EacRubyUtils::RegexpParser.new(self, &block)
9
+ end
10
+ end
@@ -3,7 +3,9 @@
3
3
  require 'eac_ruby_utils/inflector'
4
4
 
5
5
  class String
6
- def variableize
7
- ::EacRubyUtils::Inflector.variableize(self)
6
+ # Shortcut to `EacRubyUtils::Inflector.variableize(self, ...)`.
7
+ # @see EacRubyUtils::Inflector.variableize
8
+ def variableize(validate = true)
9
+ ::EacRubyUtils::Inflector.variableize(self, validate)
8
10
  end
9
11
  end
@@ -0,0 +1,34 @@
1
+ # frozen_string_literal: true
2
+
3
+ module EacRubyUtils
4
+ class RegexpParser
5
+ attr_reader :pattern, :builder_proc
6
+
7
+ def initialize(pattern, &builder_proc)
8
+ @pattern = pattern
9
+ @builder_proc = builder_proc
10
+ end
11
+
12
+ def parse(string)
13
+ internal_parse(string)[1]
14
+ end
15
+
16
+ def parse!(string)
17
+ match, result = internal_parse(string)
18
+ return result if match
19
+
20
+ raise ::ArgumentError, "String \"#{string}\" does not match pattern \"#{pattern}\""
21
+ end
22
+
23
+ private
24
+
25
+ def internal_parse(string)
26
+ m = pattern.match(string)
27
+ if m
28
+ [true, builder_proc ? builder_proc.call(m) : m]
29
+ else
30
+ [false, nil]
31
+ end
32
+ end
33
+ end
34
+ end
@@ -1,15 +1,14 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'active_support/core_ext/string/inflections'
4
- require 'eac_ruby_utils/options_consumer'
3
+ require 'eac_ruby_utils/require_sub'
5
4
 
6
5
  module EacRubyUtils
7
6
  # Provide a option by constant, method or options object.
8
7
  module SettingsProvider
8
+ ::EacRubyUtils.require_sub __FILE__, base: self
9
+
9
10
  def setting_constant_name(key, fullname = false)
10
- name = key.to_s.underscore.upcase
11
- name = "#{self.class.name}::#{name}" if fullname
12
- name
11
+ setting_value_instance(key).constant_name(fullname)
13
12
  end
14
13
 
15
14
  def setting_search_order
@@ -25,41 +24,23 @@ module EacRubyUtils
25
24
  end
26
25
 
27
26
  def setting_value(key, options = {})
28
- options = parse_setting_value_options(options)
29
- options.order.each do |method|
30
- value = send("setting_value_by_#{method}", key)
31
- return value if value
32
- end
33
- return nil unless options.required
34
-
35
- raise "Setting \"#{key}\" not found. Supply in #{settings_object_name}, implement a " \
36
- "\"#{key}\" method or declare a #{setting_constant_name(key, true)} constant."
27
+ setting_value_instance(key, options).value
37
28
  end
38
29
 
39
30
  def setting_value_by_constant(key)
40
- constant_name = setting_constant_name(key)
41
- begin
42
- self.class.const_get(constant_name)
43
- rescue NameError
44
- nil
45
- end
31
+ setting_value_instance(key).value_by_constant
46
32
  end
47
33
 
48
34
  def setting_value_by_method(key)
49
- respond_to?(key) ? send(key) : nil
35
+ setting_value_instance(key).value_by_method
50
36
  end
51
37
 
52
38
  def setting_value_by_settings_object(key)
53
- settings_object[key.to_s] || settings_object[key.to_sym]
39
+ setting_value_instance(key).value_by_settings_object
54
40
  end
55
41
 
56
- private
57
-
58
- def parse_setting_value_options(options)
59
- r = ::EacRubyUtils::OptionsConsumer.new(options).consume_all(:required, :order, ostruct: true)
60
- r.required = true if r.required.nil?
61
- r.order = setting_search_order if r.order.nil?
62
- r
42
+ def setting_value_instance(key, options = {})
43
+ ::EacRubyUtils::SettingsProvider::SettingValue.new(self, key, options)
63
44
  end
64
45
  end
65
46
  end
@@ -0,0 +1,69 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'active_support/core_ext/string/inflections'
4
+ require 'eac_ruby_utils/listable'
5
+ require 'eac_ruby_utils/simple_cache'
6
+ require 'eac_ruby_utils/struct'
7
+
8
+ module EacRubyUtils
9
+ module SettingsProvider
10
+ class SettingValue
11
+ include ::EacRubyUtils::Listable
12
+ include ::EacRubyUtils::SimpleCache
13
+
14
+ attr_reader :source, :key, :options
15
+ lists.add_symbol :option, :default, :order, :required
16
+
17
+ def initialize(source, key, options)
18
+ @source = source
19
+ @key = key
20
+ @options = options
21
+ end
22
+
23
+ def constant_name(fullname = false)
24
+ name = key.to_s.underscore.upcase
25
+ name = "#{source.class.name}::#{name}" if fullname
26
+ name
27
+ end
28
+
29
+ def value
30
+ parsed_options.order.each do |method|
31
+ value = send("value_by_#{method}")
32
+ return value if value
33
+ end
34
+ return parsed_options.default if parsed_options.respond_to?(OPTION_DEFAULT)
35
+ return nil unless parsed_options.required
36
+
37
+ raise_key_not_found
38
+ end
39
+
40
+ def value_by_constant
41
+ source.class.const_get(constant_name)
42
+ rescue NameError
43
+ nil
44
+ end
45
+
46
+ def value_by_method
47
+ source.respond_to?(key, true) ? source.send(key) : nil
48
+ end
49
+
50
+ def value_by_settings_object
51
+ source.settings_object[key.to_s] || source.settings_object[key.to_sym]
52
+ end
53
+
54
+ private
55
+
56
+ def parsed_options_uncached
57
+ r = self.class.lists.option.hash_keys_validate!(options.symbolize_keys)
58
+ r[:required] = true unless r.key?(OPTION_REQUIRED)
59
+ r[:order] = source.setting_search_order if r[OPTION_ORDER].nil?
60
+ ::EacRubyUtils::Struct.new(r)
61
+ end
62
+
63
+ def raise_key_not_found
64
+ raise "Setting \"#{key}\" not found. Supply in #{source.settings_object_name}, implement "\
65
+ "a \"#{key}\" method or declare a #{constant_name(true)} constant."
66
+ end
67
+ end
68
+ end
69
+ end
@@ -2,12 +2,18 @@
2
2
 
3
3
  module EacRubyUtils
4
4
  module SimpleCache
5
- UNCACHED_METHOD_PATTERN = /\A(\s+)_uncached\z/.freeze
5
+ UNCACHED_METHOD_NAME_SUFFIX = '_uncached'
6
+ UNCACHED_METHOD_PATTERN = /\A(\s+)_#{::Regexp.quote(UNCACHED_METHOD_NAME_SUFFIX)}\z/.freeze
7
+
8
+ class << self
9
+ def uncached_method_name(method_name)
10
+ "#{method_name}#{UNCACHED_METHOD_NAME_SUFFIX}"
11
+ end
12
+ end
6
13
 
7
14
  def method_missing(method, *args, &block)
8
- uncached_method = "#{method}_uncached"
9
- if respond_to?(uncached_method, true)
10
- call_method_with_cache(uncached_method, args, &block)
15
+ if respond_to?(::EacRubyUtils::SimpleCache.uncached_method_name(method), true)
16
+ call_method_with_cache(method, args, &block)
11
17
  else
12
18
  super
13
19
  end
@@ -35,10 +41,17 @@ module EacRubyUtils
35
41
  raise 'Não é possível realizar o cache de métodos com bloco' if block
36
42
 
37
43
  key = ([method] + args).join('@@@')
38
- cache_keys[key] = send(method, *args) unless cache_keys.key?(key)
44
+ unless cache_keys.key?(key)
45
+ uncached_value = call_uncached_method(method, args)
46
+ cache_keys[key] = uncached_value
47
+ end
39
48
  cache_keys[key]
40
49
  end
41
50
 
51
+ def call_uncached_method(method, args)
52
+ send(::EacRubyUtils::SimpleCache.uncached_method_name(method), *args)
53
+ end
54
+
42
55
  def cache_keys
43
56
  @cache_keys ||= {}
44
57
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module EacRubyUtils
4
- VERSION = '0.59.0'
4
+ VERSION = '0.63.0'
5
5
  end