eac_ruby_utils 0.94.1 → 0.95.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 42e10ce8f90ca6726da456c6a2296cfcf0d96bf0a4206cdef4a2ca67cf99bc49
4
- data.tar.gz: 9003e2761d3d975caedf977a6ed4d1e1ad952edb9fe6e103d84234fbceb8f5c8
3
+ metadata.gz: 5a2b2410fc984940c1cc192be3ffaac013123a4c0fd0fd988a4b43a672bee200
4
+ data.tar.gz: 35dd27fbf96e03aab9a57f43929c46d283a2abe43d6694701992d9c757e1c990
5
5
  SHA512:
6
- metadata.gz: 2c02f79618161caf149d1fd21400ba66e7c637571415b7d59931d254ba1462057a87d45b309382c6d53345c1326a08f261da055f49db282951a903a77c6bcee0
7
- data.tar.gz: fa963ff8cc8cc135b3a51577b98607e4c3d1e02100e572531baf099f4b57bfef7e2294c6f7ae7899063f97ef05ffbe2f41353dde92e08fac7200c5113e3d2482
6
+ metadata.gz: 665c44626a59bea44ed2970a62145531d00c7a62533d5be77319ff1ba38187851622d317df989ffa998f6f92a39d43655199b637b81448b4a506bc80ce03745d
7
+ data.tar.gz: f4444dbfa9b43c2c953641b20f0eba35873b957851ca65bdc221a9435c9542dd2231a8a87e27e9bda26fd8437b9952bbc126f8a4a836a0714b1da90f5fa4f546
@@ -68,12 +68,12 @@ module EacRubyUtils
68
68
 
69
69
  def setup_class_attr_readers(klass)
70
70
  klass.send(:attr_reader, *args)
71
- klass.send(:public, *args)
71
+ klass.send(:public, *args) if args.any?
72
72
  end
73
73
 
74
74
  def setup_class_attr_writers(klass)
75
75
  klass.send(:attr_writer, *args)
76
- klass.send(:private, *args.map { |a| "#{a}=" })
76
+ klass.send(:private, *args.map { |a| "#{a}=" }) if args.any?
77
77
  end
78
78
 
79
79
  def setup_class_initialize(klass)
@@ -1,5 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require 'eac_ruby_utils/locales/from_all_gems'
3
4
  require 'eac_ruby_utils/patches'
4
5
  require 'active_support/dependencies/autoload'
5
6
  require 'active_support/core_ext'
@@ -9,7 +9,7 @@ module EacRubyUtils
9
9
  attr_reader :mapping
10
10
 
11
11
  def initialize(mapping)
12
- @mapping = mapping.map { |k, v| [k.to_sym, v] }.to_h.freeze
12
+ @mapping = mapping.transform_keys(&:to_sym).freeze
13
13
  end
14
14
 
15
15
  def format(string)
@@ -41,7 +41,7 @@ module EacRubyUtils
41
41
  def lib_file_found?
42
42
  gemspec.require_paths.any? do |require_path|
43
43
  ::Pathname.new(require_path).expand_path(gemspec.gem_dir).join(path_to_require + '.rb')
44
- .file?
44
+ .file?
45
45
  end
46
46
  end
47
47
 
@@ -29,7 +29,7 @@ module EacRubyUtils
29
29
  # @return [Array<EacRubyUtils::GemsRegistry::Gem>]
30
30
  def all_gems
31
31
  ::Gem::Specification.map { |gemspec| ::EacRubyUtils::GemsRegistry::Gem.new(self, gemspec) }
32
- .sort
32
+ .sort
33
33
  end
34
34
  end
35
35
  end
@@ -16,7 +16,7 @@ module EacRubyUtils
16
16
  # @raise [ArgumentError]
17
17
  def variableize(string, validate = true)
18
18
  r = ::ActiveSupport::Inflector.transliterate(string).gsub(/[^_a-z0-9]/i, '_')
19
- .gsub(/_+/, '_').gsub(/_\z/, '').gsub(/\A_/, '').downcase
19
+ .gsub(/_+/, '_').gsub(/_\z/, '').gsub(/\A_/, '').downcase
20
20
  m = VARIABLE_NAME_PATTERN.match(r)
21
21
  return r if m
22
22
  return nil unless validate
@@ -43,7 +43,7 @@ module EacRubyUtils
43
43
  end
44
44
 
45
45
  def hash_keys_validate!(hash, error_class = ::StandardError)
46
- hash.keys.each { |key| value_validate!(key, error_class) }
46
+ hash.each_key { |key| value_validate!(key, error_class) }
47
47
  hash
48
48
  end
49
49
 
@@ -82,7 +82,7 @@ module EacRubyUtils
82
82
  end
83
83
 
84
84
  def find_list_by_method(method)
85
- @values.values.each do |v|
85
+ @values.each_value do |v|
86
86
  return v if method.to_s == "value_#{v.key}"
87
87
  end
88
88
  nil
@@ -93,7 +93,7 @@ module EacRubyUtils
93
93
  end
94
94
 
95
95
  def apply_constants
96
- @values.values.each do |v|
96
+ @values.each_value do |v|
97
97
  @lists.source.const_set(v.constant_name, v.value)
98
98
  end
99
99
  end
@@ -36,7 +36,7 @@ module EacRubyUtils
36
36
  return nil unless executable.exist?
37
37
 
38
38
  TIMEDATECTL_TIMEZONE_LINE_PATTERN.if_match(executable.command.execute!) { |m| m[1] }
39
- .if_present { |v| ::ActiveSupport::TimeZone[v] }
39
+ .if_present { |v| ::ActiveSupport::TimeZone[v] }
40
40
  end
41
41
 
42
42
  # @return [ActiveSupport::TimeZone]
@@ -0,0 +1,4 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'eac_ruby_utils/locales/from_gem'
4
+ ::EacRubyUtils::Locales::FromGem.include_all
@@ -0,0 +1,47 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'i18n'
4
+ require 'eac_ruby_utils/patches/class/common_constructor'
5
+ require 'eac_ruby_utils/patches/object/to_pathname'
6
+
7
+ module EacRubyUtils
8
+ module Locales
9
+ class FromGem
10
+ class << self
11
+ def include_all(i18n_obj = nil)
12
+ ::Gem::Specification.each { |gemspec| new(gemspec, i18n_obj).include }
13
+ end
14
+ end
15
+
16
+ LOCALES_DIR_SUBPATH = 'locale'
17
+ LOCALES_FILES_GLOB_PATTERNS = %w[*.yaml *.yml].freeze
18
+
19
+ common_constructor :gemspec, :i18n_obj, default: [nil] do
20
+ self.i18n_obj ||= ::I18n
21
+ end
22
+
23
+ # @return [Boolean]
24
+ delegate :exist?, to: :path
25
+
26
+ # @return [Pathname, nil]
27
+ def include
28
+ return nil unless exist?
29
+
30
+ ::I18n.load_path += paths_to_load.map(&:to_path)
31
+ path
32
+ end
33
+
34
+ # @return [Pathname]
35
+ def path
36
+ gemspec.gem_dir.to_pathname.join(LOCALES_DIR_SUBPATH)
37
+ end
38
+
39
+ # @return [Pathname]
40
+ def paths_to_load
41
+ return [] unless exist?
42
+
43
+ LOCALES_FILES_GLOB_PATTERNS.inject([]) { |a, e| a + path.glob(e) }
44
+ end
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,74 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'active_support/core_ext/string/inflections'
4
+ require 'eac_ruby_utils/common_constructor'
5
+ require 'i18n'
6
+
7
+ module EacRubyUtils
8
+ module Locales
9
+ class ModuleI18nTranslate
10
+ TRANSLATE_LOCALE_KEY = :__locale
11
+
12
+ common_constructor :the_module, :entry_suffix, :values, default: [{}]
13
+
14
+ def ancestor_i18n_translate(ancestor)
15
+ t = self.class.new(ancestor, entry_suffix, values)
16
+ t.exists? ? t.i18n_translate : nil
17
+ end
18
+
19
+ def ancestors_i18n_translate
20
+ the_module.ancestors.lazy.map { |v| ancestor_i18n_translate(v) }.find(&:present?)
21
+ end
22
+
23
+ def exists?
24
+ ::I18n.exists?(i18n_key)
25
+ end
26
+
27
+ def i18n_key
28
+ "#{module_entry_prefix}.#{entry_suffix}"
29
+ end
30
+
31
+ def i18n_options
32
+ values.except(TRANSLATE_LOCALE_KEY)
33
+ end
34
+
35
+ def i18n_translate
36
+ ::I18n.translate(i18n_key, i18n_options)
37
+ end
38
+
39
+ def locale
40
+ values[TRANSLATE_LOCALE_KEY]
41
+ end
42
+
43
+ def module_entry_prefix
44
+ the_module.name.underscore.gsub('/', '.')
45
+ end
46
+
47
+ def on_locale(&block)
48
+ if locale.present?
49
+ on_present_locale(&block)
50
+ else
51
+ block.call
52
+ end
53
+ end
54
+
55
+ def result
56
+ on_locale do
57
+ ancestors_i18n_translate || i18n_translate
58
+ end
59
+ end
60
+
61
+ private
62
+
63
+ def on_present_locale(&block)
64
+ old_locale = ::I18n.locale
65
+ begin
66
+ ::I18n.locale = locale
67
+ block.call
68
+ ensure
69
+ ::I18n.locale = old_locale
70
+ end
71
+ end
72
+ end
73
+ end
74
+ end
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'eac_ruby_utils/require_sub'
4
+
5
+ module EacRubyUtils
6
+ module Locales
7
+ ::EacRubyUtils.require_sub __FILE__
8
+ end
9
+ end
@@ -0,0 +1,35 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'active_support/core_ext/module/introspection'
4
+ require 'eac_ruby_utils/patches/class/common_constructor'
5
+ require 'eac_ruby_utils/patches/module/common_concern'
6
+ require 'eac_ruby_utils/patches/string/inflector'
7
+
8
+ module EacRubyUtils
9
+ module MethodClass
10
+ common_concern do
11
+ ::EacRubyUtils::MethodClass::Setup.new(self)
12
+ end
13
+
14
+ class Setup
15
+ common_constructor :method_class do
16
+ perform
17
+ end
18
+
19
+ def perform
20
+ the_setup = self
21
+ sender_module.define_method(method_name) do |*args, &block|
22
+ the_setup.method_class.new(self, *args, &block).result
23
+ end
24
+ end
25
+
26
+ def method_name
27
+ method_class.name.demodulize.underscore.variableize
28
+ end
29
+
30
+ def sender_module
31
+ method_class.module_parent
32
+ end
33
+ end
34
+ end
35
+ end
@@ -1,32 +1,9 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'active_support/core_ext/string/inflections'
4
- require 'i18n'
3
+ require 'eac_ruby_utils/locales/module_i18n_translate'
5
4
 
6
5
  class Module
7
- TRANSLATE_LOCALE_KEY = :__locale
8
-
9
6
  def i18n_translate(entry_suffix, values = {})
10
- on_i18n_locale(values.delete(TRANSLATE_LOCALE_KEY)) do
11
- ::I18n.translate(i18n_translate_entry_full(entry_suffix), values)
12
- end
13
- end
14
-
15
- def i18n_translate_entry_full(entry_suffix)
16
- "#{i18n_translate_entry_self_prefix}.#{entry_suffix}"
17
- end
18
-
19
- def i18n_translate_entry_self_prefix
20
- name.underscore.gsub('/', '.')
21
- end
22
-
23
- def on_i18n_locale(locale)
24
- old_locale = ::I18n.locale
25
- begin
26
- ::I18n.locale = locale
27
- yield
28
- ensure
29
- ::I18n.locale = old_locale
30
- end
7
+ ::EacRubyUtils::Locales::ModuleI18nTranslate.new(self, entry_suffix, values).result
31
8
  end
32
9
  end
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'eac_ruby_utils/method_class'
4
+
5
+ class Module
6
+ def enable_method_class
7
+ ::EacRubyUtils.patch(self, ::EacRubyUtils::MethodClass)
8
+ end
9
+ end
@@ -1,6 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'active_support/inflector'
4
+ require 'active_support/dependencies'
4
5
  require 'eac_ruby_utils/listable'
5
6
 
6
7
  module EacRubyUtils
@@ -11,6 +12,14 @@ module EacRubyUtils
11
12
  end
12
13
 
13
14
  class RequireSub
15
+ INCLUDE_MODULES_MAP = {
16
+ nil => nil,
17
+ false => nil,
18
+ true => :include,
19
+ include: :include,
20
+ prepend: :prepend
21
+ }.freeze
22
+
14
23
  include ::EacRubyUtils::Listable
15
24
  lists.add_symbol :option, :base, :include_modules, :require_dependency
16
25
 
@@ -26,56 +35,100 @@ module EacRubyUtils
26
35
  include_modules
27
36
  end
28
37
 
29
- private
38
+ def base
39
+ options[OPTION_BASE] || raise('Option :base not setted')
40
+ end
30
41
 
31
- def active_support_require(path)
32
- return false unless options[OPTION_REQUIRE_DEPENDENCY]
42
+ def base?
43
+ options[OPTION_BASE] ? true : false
44
+ end
33
45
 
34
- ::Kernel.require_dependency(path)
35
- true
46
+ def include_modules
47
+ sub_files.each(&:include_module)
36
48
  end
37
49
 
38
- def autoload_require(path)
39
- return false unless base?
50
+ def include_or_prepend_method
51
+ return INCLUDE_MODULES_MAP.fetch(options[OPTION_INCLUDE_MODULES]) if
52
+ INCLUDE_MODULES_MAP.key?(options[OPTION_INCLUDE_MODULES])
40
53
 
41
- basename = ::File.basename(path, '.*')
42
- return false if basename.start_with?('_')
54
+ raise ::ArgumentError, "Invalid value for 'options[OPTION_INCLUDE_MODULES]':" \
55
+ " \"#{options[OPTION_INCLUDE_MODULES]}\""
56
+ end
43
57
 
44
- base.autoload ::ActiveSupport::Inflector.camelize(basename), path
45
- true
58
+ def require_sub_files
59
+ sub_files.each(&:require_file)
46
60
  end
47
61
 
48
- def include_modules
49
- return unless options[OPTION_INCLUDE_MODULES]
62
+ def sub_files
63
+ @sub_files ||= Dir["#{File.dirname(file)}/#{::File.basename(file, '.*')}/*.rb"].sort
64
+ .map { |path| SubFile.new(self, path) }
65
+ end
50
66
 
51
- base.constants.each do |constant_name|
52
- constant = base.const_get(constant_name)
53
- next unless constant.is_a?(::Module) && !constant.is_a?(::Class)
67
+ class SubFile
68
+ attr_reader :owner, :path
54
69
 
55
- base.include(constant)
70
+ def initialize(owner, path)
71
+ @owner = owner
72
+ @path = path
56
73
  end
57
- end
58
74
 
59
- def base
60
- options[OPTION_BASE] || raise('Option :base not setted')
61
- end
75
+ def base_constant
76
+ return nil unless owner.base?
62
77
 
63
- def base?
64
- options[OPTION_BASE] ? true : false
65
- end
78
+ owner.base.const_get(constant_name)
79
+ rescue ::NameError
80
+ nil
81
+ end
66
82
 
67
- def kernel_require(path)
68
- ::Kernel.require(path)
69
- end
83
+ def constant_name
84
+ ::ActiveSupport::Inflector.camelize(::File.basename(path, '.rb'))
85
+ end
70
86
 
71
- def require_sub_files
72
- Dir["#{File.dirname(file)}/#{::File.basename(file, '.*')}/*.rb"].sort.each do |path|
73
- require_sub_file(path)
87
+ def include_module
88
+ return unless module?
89
+
90
+ owner.include_or_prepend_method.if_present do |v|
91
+ owner.base.send(v, base_constant)
92
+ end
93
+ end
94
+
95
+ def include_or_prepend_method
96
+ return :include if owner.options[OPTION_INCLUDE_MODULES]
97
+ return :prepend if owner.options[OPTION_PREPEND_MODULES]
98
+
99
+ nil
100
+ end
101
+
102
+ def module?
103
+ base_constant.is_a?(::Module) && !base_constant.is_a?(::Class)
104
+ end
105
+
106
+ def require_file
107
+ active_support_require || autoload_require || kernel_require
108
+ end
109
+
110
+ private
111
+
112
+ def active_support_require
113
+ return false unless owner.options[OPTION_REQUIRE_DEPENDENCY]
114
+
115
+ ::Kernel.require_dependency(path)
116
+ true
74
117
  end
75
- end
76
118
 
77
- def require_sub_file(path)
78
- active_support_require(path) || autoload_require(path) || kernel_require(path)
119
+ def autoload_require
120
+ return false unless owner.base?
121
+
122
+ basename = ::File.basename(path, '.*')
123
+ return false if basename.start_with?('_')
124
+
125
+ owner.base.autoload ::ActiveSupport::Inflector.camelize(basename), path
126
+ true
127
+ end
128
+
129
+ def kernel_require
130
+ ::Kernel.require(path)
131
+ end
79
132
  end
80
133
  end
81
134
  end
@@ -12,7 +12,7 @@ module EacRubyUtils
12
12
 
13
13
  def on_clean_envvars(*start_with_vars)
14
14
  old_values = envvars_starting_with(start_with_vars)
15
- old_values.keys.each { |k| ENV.delete(k) }
15
+ old_values.each_key { |k| ENV.delete(k) }
16
16
  yield
17
17
  ensure
18
18
  old_values&.each { |k, v| ENV[k] = v }
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module EacRubyUtils
4
- VERSION = '0.94.1'
4
+ VERSION = '0.95.1'
5
5
  end
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.94.1
4
+ version: 0.95.1
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: 2022-06-02 00:00:00.000000000 Z
11
+ date: 2022-06-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -78,14 +78,14 @@ dependencies:
78
78
  requirements:
79
79
  - - "~>"
80
80
  - !ruby/object:Gem::Version
81
- version: 0.5.0
81
+ version: 0.5.1
82
82
  type: :development
83
83
  prerelease: false
84
84
  version_requirements: !ruby/object:Gem::Requirement
85
85
  requirements:
86
86
  - - "~>"
87
87
  - !ruby/object:Gem::Version
88
- version: 0.5.0
88
+ version: 0.5.1
89
89
  description:
90
90
  email:
91
91
  executables: []
@@ -160,6 +160,11 @@ files:
160
160
  - lib/eac_ruby_utils/listable/symbol_list.rb
161
161
  - lib/eac_ruby_utils/listable/value.rb
162
162
  - lib/eac_ruby_utils/local_time_zone.rb
163
+ - lib/eac_ruby_utils/locales.rb
164
+ - lib/eac_ruby_utils/locales/from_all_gems.rb
165
+ - lib/eac_ruby_utils/locales/from_gem.rb
166
+ - lib/eac_ruby_utils/locales/module_i18n_translate.rb
167
+ - lib/eac_ruby_utils/method_class.rb
163
168
  - lib/eac_ruby_utils/options_consumer.rb
164
169
  - lib/eac_ruby_utils/patch.rb
165
170
  - lib/eac_ruby_utils/patches.rb
@@ -187,6 +192,7 @@ files:
187
192
  - lib/eac_ruby_utils/patches/module/i18n_translate.rb
188
193
  - lib/eac_ruby_utils/patches/module/immutable.rb
189
194
  - lib/eac_ruby_utils/patches/module/listable.rb
195
+ - lib/eac_ruby_utils/patches/module/method_class.rb
190
196
  - lib/eac_ruby_utils/patches/module/patch.rb
191
197
  - lib/eac_ruby_utils/patches/module/require_sub.rb
192
198
  - lib/eac_ruby_utils/patches/module/simple_cache.rb