eac_ruby_utils 0.94.2 → 0.95.2
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 +4 -4
- data/lib/eac_ruby_utils/common_constructor.rb +2 -2
- data/lib/eac_ruby_utils/core_ext.rb +1 -0
- data/lib/eac_ruby_utils/locales/from_all_gems.rb +4 -0
- data/lib/eac_ruby_utils/locales/from_gem.rb +47 -0
- data/lib/eac_ruby_utils/locales/module_i18n_translate.rb +74 -0
- data/lib/eac_ruby_utils/locales.rb +9 -0
- data/lib/eac_ruby_utils/method_class.rb +35 -0
- data/lib/eac_ruby_utils/patches/module/i18n_translate.rb +2 -25
- data/lib/eac_ruby_utils/patches/module/method_class.rb +9 -0
- data/lib/eac_ruby_utils/require_sub.rb +86 -33
- data/lib/eac_ruby_utils/ruby/on_clean_environment.rb +13 -1
- data/lib/eac_ruby_utils/version.rb +1 -1
- metadata +22 -2
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 9e74dec765cecd11cda7fcfba909574ee9358c79bd257da602a447f7ca9d0e21
|
|
4
|
+
data.tar.gz: 5d1b59a63a0d652700cb3079431c6b5c7ba3b49d993c3d07712ecc042d83ccab
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: f53504f7b9e093892250948b8539c4fce1f4b27623ba802ad1cce0961c444e5055a6f512e698322928f7fc2adb87d4fe1a1f1a702afd5c2aaf86fc5a8a53bbb9
|
|
7
|
+
data.tar.gz: 9ea207ad6ca0a61fa4ad353c64be5bbd5f7b85a597a5a8b675544091e0e043298ca9077565734106f15a6d9a8a8be21bfc4d6bc14d8959c189f9fed961a86abe
|
|
@@ -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)
|
|
@@ -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,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 '
|
|
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
|
-
|
|
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
|
|
@@ -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
|
-
|
|
38
|
+
def base
|
|
39
|
+
options[OPTION_BASE] || raise('Option :base not setted')
|
|
40
|
+
end
|
|
30
41
|
|
|
31
|
-
def
|
|
32
|
-
|
|
42
|
+
def base?
|
|
43
|
+
options[OPTION_BASE] ? true : false
|
|
44
|
+
end
|
|
33
45
|
|
|
34
|
-
|
|
35
|
-
|
|
46
|
+
def include_modules
|
|
47
|
+
sub_files.each(&:include_module)
|
|
36
48
|
end
|
|
37
49
|
|
|
38
|
-
def
|
|
39
|
-
return
|
|
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
|
-
|
|
42
|
-
|
|
54
|
+
raise ::ArgumentError, "Invalid value for 'options[OPTION_INCLUDE_MODULES]':" \
|
|
55
|
+
" \"#{options[OPTION_INCLUDE_MODULES]}\""
|
|
56
|
+
end
|
|
43
57
|
|
|
44
|
-
|
|
45
|
-
|
|
58
|
+
def require_sub_files
|
|
59
|
+
sub_files.each(&:require_file)
|
|
46
60
|
end
|
|
47
61
|
|
|
48
|
-
def
|
|
49
|
-
|
|
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
|
-
|
|
52
|
-
|
|
53
|
-
next unless constant.is_a?(::Module) && !constant.is_a?(::Class)
|
|
67
|
+
class SubFile
|
|
68
|
+
attr_reader :owner, :path
|
|
54
69
|
|
|
55
|
-
|
|
70
|
+
def initialize(owner, path)
|
|
71
|
+
@owner = owner
|
|
72
|
+
@path = path
|
|
56
73
|
end
|
|
57
|
-
end
|
|
58
74
|
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
end
|
|
75
|
+
def base_constant
|
|
76
|
+
return nil unless owner.base?
|
|
62
77
|
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
78
|
+
owner.base.const_get(constant_name)
|
|
79
|
+
rescue ::NameError
|
|
80
|
+
nil
|
|
81
|
+
end
|
|
66
82
|
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
83
|
+
def constant_name
|
|
84
|
+
::ActiveSupport::Inflector.camelize(::File.basename(path, '.rb'))
|
|
85
|
+
end
|
|
70
86
|
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
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
|
-
|
|
78
|
-
|
|
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
|
|
@@ -1,15 +1,27 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
+
require 'bundler'
|
|
4
|
+
|
|
3
5
|
module EacRubyUtils
|
|
4
6
|
module Ruby
|
|
5
7
|
class << self
|
|
6
8
|
# Executes a block in an environment when the variables BUNDLE* and RUBY* are removed.
|
|
7
9
|
def on_clean_environment
|
|
8
|
-
|
|
10
|
+
bundler_with_unbundled_env do
|
|
11
|
+
on_clean_envvars('BUNDLE', 'RUBY') { yield }
|
|
12
|
+
end
|
|
9
13
|
end
|
|
10
14
|
|
|
11
15
|
private
|
|
12
16
|
|
|
17
|
+
def bundler_with_unbundled_env(&block)
|
|
18
|
+
if ::Bundler.respond_to?(:with_unbundled_env)
|
|
19
|
+
::Bundler.with_unbundled_env(&block)
|
|
20
|
+
else
|
|
21
|
+
::Bundler.with_clean_env(&block)
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
|
|
13
25
|
def on_clean_envvars(*start_with_vars)
|
|
14
26
|
old_values = envvars_starting_with(start_with_vars)
|
|
15
27
|
old_values.each_key { |k| ENV.delete(k) }
|
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.
|
|
4
|
+
version: 0.95.2
|
|
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-
|
|
11
|
+
date: 2022-07-17 00:00:00.000000000 Z
|
|
12
12
|
dependencies:
|
|
13
13
|
- !ruby/object:Gem::Dependency
|
|
14
14
|
name: activesupport
|
|
@@ -44,6 +44,20 @@ dependencies:
|
|
|
44
44
|
- - "~>"
|
|
45
45
|
- !ruby/object:Gem::Version
|
|
46
46
|
version: '2.6'
|
|
47
|
+
- !ruby/object:Gem::Dependency
|
|
48
|
+
name: bundler
|
|
49
|
+
requirement: !ruby/object:Gem::Requirement
|
|
50
|
+
requirements:
|
|
51
|
+
- - ">="
|
|
52
|
+
- !ruby/object:Gem::Version
|
|
53
|
+
version: '0'
|
|
54
|
+
type: :runtime
|
|
55
|
+
prerelease: false
|
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
57
|
+
requirements:
|
|
58
|
+
- - ">="
|
|
59
|
+
- !ruby/object:Gem::Version
|
|
60
|
+
version: '0'
|
|
47
61
|
- !ruby/object:Gem::Dependency
|
|
48
62
|
name: filesize
|
|
49
63
|
requirement: !ruby/object:Gem::Requirement
|
|
@@ -160,6 +174,11 @@ files:
|
|
|
160
174
|
- lib/eac_ruby_utils/listable/symbol_list.rb
|
|
161
175
|
- lib/eac_ruby_utils/listable/value.rb
|
|
162
176
|
- lib/eac_ruby_utils/local_time_zone.rb
|
|
177
|
+
- lib/eac_ruby_utils/locales.rb
|
|
178
|
+
- lib/eac_ruby_utils/locales/from_all_gems.rb
|
|
179
|
+
- lib/eac_ruby_utils/locales/from_gem.rb
|
|
180
|
+
- lib/eac_ruby_utils/locales/module_i18n_translate.rb
|
|
181
|
+
- lib/eac_ruby_utils/method_class.rb
|
|
163
182
|
- lib/eac_ruby_utils/options_consumer.rb
|
|
164
183
|
- lib/eac_ruby_utils/patch.rb
|
|
165
184
|
- lib/eac_ruby_utils/patches.rb
|
|
@@ -187,6 +206,7 @@ files:
|
|
|
187
206
|
- lib/eac_ruby_utils/patches/module/i18n_translate.rb
|
|
188
207
|
- lib/eac_ruby_utils/patches/module/immutable.rb
|
|
189
208
|
- lib/eac_ruby_utils/patches/module/listable.rb
|
|
209
|
+
- lib/eac_ruby_utils/patches/module/method_class.rb
|
|
190
210
|
- lib/eac_ruby_utils/patches/module/patch.rb
|
|
191
211
|
- lib/eac_ruby_utils/patches/module/require_sub.rb
|
|
192
212
|
- lib/eac_ruby_utils/patches/module/simple_cache.rb
|