eac_ruby_utils 0.94.2 → 0.95.0

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: 6123804bfe63fb8449caccf2c037cc3e226c9d2859894feea80b5487fc472187
4
- data.tar.gz: d47032f60cbd693dcd7ec2cfa72dcaf1c7c1bc69abd8354126f272a11a452702
3
+ metadata.gz: a1b0627e0ccb021df698834490b180324889cc47023a515e79f39032fef0361b
4
+ data.tar.gz: b0397d6a521f65e0559cb647ddbbb18ffd9024953816e1acefae8c19c596e974
5
5
  SHA512:
6
- metadata.gz: d2edf68d551b316ba760f7c7c89a03b6b85262e9e6843506f7346d9f92a883cfe43a2efd591a497389bdde11dee9bcc19b5443aa4ba7e130d167ecfec047e9b8
7
- data.tar.gz: 11fd72d374f34a61bba2d856ccfae437dc97010e8f2b074a6cb6f4ed2867a9f1f8515e073b4986b93518dd7b7d04a9293b71b5934d8d09582c66acb4e494f37b
6
+ metadata.gz: 92f1e95bbd7b2bff4ee4427a403df3a28ede98d87538b02b349eafbd9688398c0e355324e1d0f1d66fc28da0c83a0180e516dde95a1cfd90f92e20b977fa6aae
7
+ data.tar.gz: cc4749b029a9a93c4ef5b42350a8701b84070131ae18cd4855ec343a41ada74c5990d161de6e1f826bc9b98bae1c46bfb166a0ac2f96b501345372bbe1985752
@@ -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'
@@ -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
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module EacRubyUtils
4
- VERSION = '0.94.2'
4
+ VERSION = '0.95.0'
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.2
4
+ version: 0.95.0
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-04 00:00:00.000000000 Z
11
+ date: 2022-06-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -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