eac_ruby_utils 0.100.0 → 0.102.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: e6f852b958cf7ed8f6f05d3860d738de716c1c5a7456917b4705abf4ab78b546
4
- data.tar.gz: ea80e22f5fec3c6d5958a544a348790a01491490dc0f75ac98d8d59b88955122
3
+ metadata.gz: b5f792278304cbfea532d8d098e2fb06b2d91b6868ca12e0fd52bdb615910ba8
4
+ data.tar.gz: 7164404e6bd6ba2f034e376f4cffd615babd64eb1f6ebf1e344bcdcefdab8e6f
5
5
  SHA512:
6
- metadata.gz: 9b9be95919c93a7b3800d726bb52055c9ecfe92b422601af67cc7ad50bbd0a6602d0d92d120759ad408661201040512545286fc376832031f594e46ea1d7d121
7
- data.tar.gz: 50674addc08d99c21ffcd2160a8beeb0418f49bd1bd6b2a815544179495b8fce3b4e33fb98074ab530a8def5052db2022048d758943afff3ade8855a3d6bc512
6
+ metadata.gz: 347dcea0f538268722c6d6012c70d3b69d33a68e83ae47af1f07eb07aa4a8cbf69c712a60c3cb789636b4320d96448287efbc2b65111f4e48fb79f6386e20cad
7
+ data.tar.gz: 19edd251815456d531862729af882ecd5cc051b625f358117322037c188a3b8acca43cffe4b60062beed520bffd9fe33ecb3838f822b85f0171a47a7cf366f06
@@ -0,0 +1,36 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'eac_ruby_utils/common_constructor/instance_initialize'
4
+ require 'eac_ruby_utils/common_constructor/super_args'
5
+
6
+ module EacRubyUtils
7
+ class CommonConstructor
8
+ class ClassAccessors
9
+ attr_reader :common_constructor, :klass
10
+
11
+ def initialize(common_constructor, klass)
12
+ @common_constructor = common_constructor
13
+ @klass = klass
14
+ end
15
+
16
+ def args
17
+ common_constructor.all_args
18
+ end
19
+
20
+ def perform
21
+ setup_class_attr_readers
22
+ setup_class_attr_writers
23
+ end
24
+
25
+ def setup_class_attr_readers
26
+ klass.send(:attr_reader, *args)
27
+ klass.send(:public, *args) if args.any?
28
+ end
29
+
30
+ def setup_class_attr_writers
31
+ klass.send(:attr_writer, *args)
32
+ klass.send(:private, *args.map { |a| "#{a}=" }) if args.any?
33
+ end
34
+ end
35
+ end
36
+ end
@@ -13,17 +13,27 @@ module EacRubyUtils
13
13
  @klass = klass
14
14
  end
15
15
 
16
- def run
16
+ def define_initialize_callbacks
17
+ klass.include(::ActiveSupport::Callbacks)
18
+ klass.define_callbacks :initialize
19
+ end
20
+
21
+ def define_initialize_method
17
22
  class_initialize = self
18
- klass.send(:define_method, :initialize) do |*args|
23
+ klass.send(:define_method, :initialize) do |*args, &block|
19
24
  ::EacRubyUtils::CommonConstructor::InstanceInitialize.new(
20
- class_initialize.common_constructor, args, self
21
- ).run
25
+ class_initialize.common_constructor, args, self, block
26
+ ).perform
22
27
  super(*::EacRubyUtils::CommonConstructor::SuperArgs.new(
23
28
  class_initialize, args, self
24
29
  ).result)
25
30
  end
26
31
  end
32
+
33
+ def perform
34
+ define_initialize_callbacks
35
+ define_initialize_method
36
+ end
27
37
  end
28
38
  end
29
39
  end
@@ -3,18 +3,20 @@
3
3
  module EacRubyUtils
4
4
  class CommonConstructor
5
5
  class InstanceInitialize
6
- attr_reader :common_constructor, :args, :object
6
+ attr_reader :common_constructor, :args, :object, :block
7
7
 
8
- def initialize(common_constructor, args, object)
8
+ def initialize(common_constructor, args, object, block)
9
9
  @common_constructor = common_constructor
10
10
  @args = args
11
11
  @object = object
12
+ @block = block
12
13
  end
13
14
 
14
- def run
15
+ def perform
15
16
  validate_args_count
16
17
  object.run_callbacks :initialize do
17
18
  object_attributes_set
19
+ object_block_set
18
20
  object_after_callback
19
21
  end
20
22
  end
@@ -36,12 +38,21 @@ module EacRubyUtils
36
38
  object.instance_eval(&common_constructor.after_set_block)
37
39
  end
38
40
 
41
+ def object_attribute_set(attr_name, attr_value)
42
+ object.send("#{attr_name}=", attr_value)
43
+ end
44
+
39
45
  def object_attributes_set
40
46
  common_constructor.args.each do |arg_name|
41
- object.send("#{arg_name}=", arg_value(arg_name))
47
+ object_attribute_set(arg_name, arg_value(arg_name))
42
48
  end
43
49
  end
44
50
 
51
+ def object_block_set
52
+ block_arg = common_constructor.block_arg
53
+ object_attribute_set(block_arg, block) if block_arg
54
+ end
55
+
45
56
  def validate_args_count
46
57
  return if common_constructor.args_count.include?(args.count)
47
58
 
@@ -2,12 +2,13 @@
2
2
 
3
3
  require 'active_support/callbacks'
4
4
  require 'eac_ruby_utils/arguments_consumer'
5
+ require 'eac_ruby_utils/common_constructor/class_accessors'
5
6
  require 'eac_ruby_utils/common_constructor/class_initialize'
6
7
  require 'ostruct'
7
8
 
8
9
  module EacRubyUtils
9
10
  class CommonConstructor
10
- attr_reader :args, :options, :after_set_block
11
+ attr_reader :all_args, :options, :after_set_block
11
12
 
12
13
  class << self
13
14
  def parse_args_options(args)
@@ -37,11 +38,15 @@ module EacRubyUtils
37
38
 
38
39
  def initialize(*args, &after_set_block)
39
40
  args_processed = self.class.parse_args_options(args)
40
- @args = args_processed.args
41
+ @all_args = args_processed.args
41
42
  @options = args_processed.options
42
43
  @after_set_block = after_set_block
43
44
  end
44
45
 
46
+ def args
47
+ block_arg? ? all_args[0..-2] : all_args
48
+ end
49
+
45
50
  def args_count
46
51
  (args_count_min..args_count_max)
47
52
  end
@@ -54,32 +59,32 @@ module EacRubyUtils
54
59
  args.count
55
60
  end
56
61
 
62
+ def block_arg
63
+ block_arg? ? all_args.last : nil
64
+ end
65
+
66
+ def block_arg?
67
+ options[:block_arg] || false
68
+ end
69
+
57
70
  def default_values
58
71
  options[:default] || []
59
72
  end
60
73
 
61
74
  def setup_class(klass)
62
- setup_class_attr_readers(klass)
63
- setup_class_attr_writers(klass)
75
+ setup_class_accessors(klass)
76
+
64
77
  setup_class_initialize(klass)
65
78
 
66
79
  klass
67
80
  end
68
81
 
69
- def setup_class_attr_readers(klass)
70
- klass.send(:attr_reader, *args)
71
- klass.send(:public, *args) if args.any?
72
- end
73
-
74
- def setup_class_attr_writers(klass)
75
- klass.send(:attr_writer, *args)
76
- klass.send(:private, *args.map { |a| "#{a}=" }) if args.any?
82
+ def setup_class_accessors(klass)
83
+ ::EacRubyUtils::CommonConstructor::ClassAccessors.new(self, klass).perform
77
84
  end
78
85
 
79
86
  def setup_class_initialize(klass)
80
- klass.include(::ActiveSupport::Callbacks)
81
- klass.define_callbacks :initialize
82
- ::EacRubyUtils::CommonConstructor::ClassInitialize.new(self, klass).run
87
+ ::EacRubyUtils::CommonConstructor::ClassInitialize.new(self, klass).perform
83
88
  end
84
89
 
85
90
  def super_args
@@ -3,6 +3,7 @@
3
3
  require 'active_support/core_ext/module/introspection'
4
4
  require 'eac_ruby_utils/patches/class/common_constructor'
5
5
  require 'eac_ruby_utils/patches/module/common_concern'
6
+ require 'eac_ruby_utils/patches/module/module_parent'
6
7
  require 'eac_ruby_utils/patches/string/inflector'
7
8
 
8
9
  module EacRubyUtils
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'active_support/core_ext/module/introspection'
4
+
5
+ ::Module.alias_method :module_parent, :parent unless ::Module.method_defined?(:module_parent)
@@ -1,37 +1,70 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'bundler'
4
+ require 'eac_ruby_utils/ruby/on_replace_objects'
4
5
 
5
6
  module EacRubyUtils
6
7
  module Ruby
7
8
  class << self
8
9
  # Executes a block in an environment when the variables BUNDLE* and RUBY* are removed.
9
- def on_clean_environment
10
- bundler_with_unbundled_env do
11
- on_clean_envvars('BUNDLE', 'RUBY') { yield }
12
- end
10
+ def on_clean_environment(&block)
11
+ OnCleanEnvironment.new(&block).perform
13
12
  end
14
13
 
15
- private
14
+ class OnCleanEnvironment
15
+ ENVVARS_PREFIXES_TO_CLEAN = %w[BUNDLE RUBY].freeze
16
+
17
+ attr_reader :block, :original_env
16
18
 
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)
19
+ def initialize(&block)
20
+ @block = block
21
+ @original_env = ::ENV.to_h
22
22
  end
23
- end
24
23
 
25
- def on_clean_envvars(*start_with_vars)
26
- old_values = envvars_starting_with(start_with_vars)
27
- old_values.each_key { |k| ENV.delete(k) }
28
- yield
29
- ensure
30
- old_values&.each { |k, v| ENV[k] = v }
31
- end
24
+ # @return [Array<String>]
25
+ def envvars_prefixes_to_clean
26
+ ENVVARS_PREFIXES_TO_CLEAN
27
+ end
32
28
 
33
- def envvars_starting_with(start_with_vars)
34
- ENV.select { |k, _v| start_with_vars.any? { |var| k.start_with?(var) } }
29
+ def perform
30
+ bundler_with_unbundled_env do
31
+ on_clean_envvars
32
+ end
33
+ end
34
+
35
+ private
36
+
37
+ def bundler_with_unbundled_env(&block)
38
+ with_bundler_modified do
39
+ ::Bundler.send(bundler_with_env_method_name, &block)
40
+ end
41
+ end
42
+
43
+ def bundler_with_env_method_name
44
+ if ::Bundler.respond_to?(:with_unbundled_env)
45
+ :with_unbundled_env
46
+ else
47
+ :with_clean_env
48
+ end
49
+ end
50
+
51
+ def clean_env
52
+ r = original_env.dup
53
+ r.delete_if { |k, _| envvars_prefixes_to_clean.any? { |prefix| k.start_with?(prefix) } }
54
+ r
55
+ end
56
+
57
+ def on_clean_envvars
58
+ ::Bundler.send('with_env', clean_env) { block.call }
59
+ end
60
+
61
+ def with_bundler_modified(&block)
62
+ cloned_env = original_env.dup
63
+ ::EacRubyUtils::Ruby.on_replace_objects do |replacer|
64
+ replacer.replace_self_method(::Bundler, :original_env) { cloned_env }
65
+ block.call
66
+ end
67
+ end
35
68
  end
36
69
  end
37
70
  end
@@ -0,0 +1,36 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'active_support/inflector'
4
+ require 'eac_ruby_utils/ruby/on_replace_objects/replace_instance_method'
5
+
6
+ module EacRubyUtils
7
+ module Ruby
8
+ class OnReplaceObjects
9
+ class ReplaceInstanceMethod
10
+ attr_reader :a_module, :method_new_block, :original_method
11
+
12
+ def initialize(a_module, method_name, &method_new_block)
13
+ @a_module = a_module
14
+ @original_method = a_module.instance_method(method_name)
15
+ @method_new_block = method_new_block
16
+ end
17
+
18
+ def apply
19
+ a_module.define_method(method_name, &method_new_block)
20
+
21
+ self
22
+ end
23
+
24
+ def method_name
25
+ original_method.name
26
+ end
27
+
28
+ def restore
29
+ a_module.define_method(method_name, original_method)
30
+
31
+ self
32
+ end
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,52 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'active_support/inflector'
4
+ require 'eac_ruby_utils/ruby/on_replace_objects/replace_instance_method'
5
+
6
+ module EacRubyUtils
7
+ module Ruby
8
+ class << self
9
+ def on_replace_objects
10
+ replacer = OnReplaceObjects.new
11
+ replacer.on_replacement do
12
+ yield(replacer)
13
+ end
14
+ end
15
+ end
16
+
17
+ class OnReplaceObjects
18
+ def on_replacement(&block)
19
+ clear_replacements
20
+ block.call(self)
21
+ ensure
22
+ restore_replacements
23
+ end
24
+
25
+ def replace_instance_method(a_module, method_name, &block)
26
+ add_replacement(__method__, a_module, method_name, &block)
27
+ end
28
+
29
+ def replace_self_method(object, method_name, &block)
30
+ add_replacement(:replace_instance_method, object.singleton_class, method_name, &block)
31
+ end
32
+
33
+ private
34
+
35
+ def add_replacement(method_name, *args, &block)
36
+ @replacements << replacement_class(method_name).new(*args, &block).apply
37
+ end
38
+
39
+ def replacement_class(method_name)
40
+ self.class.const_get(::ActiveSupport::Inflector.camelize(method_name))
41
+ end
42
+
43
+ def clear_replacements
44
+ @replacements = []
45
+ end
46
+
47
+ def restore_replacements
48
+ @replacements.reverse.each(&:restore)
49
+ end
50
+ end
51
+ end
52
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module EacRubyUtils
4
- VERSION = '0.100.0'
4
+ VERSION = '0.102.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.100.0
4
+ version: 0.102.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-08-14 00:00:00.000000000 Z
11
+ date: 2022-09-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -36,14 +36,14 @@ dependencies:
36
36
  requirements:
37
37
  - - "~>"
38
38
  - !ruby/object:Gem::Version
39
- version: '2.6'
39
+ version: '2.8'
40
40
  type: :runtime
41
41
  prerelease: false
42
42
  version_requirements: !ruby/object:Gem::Requirement
43
43
  requirements:
44
44
  - - "~>"
45
45
  - !ruby/object:Gem::Version
46
- version: '2.6'
46
+ version: '2.8'
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: bundler
49
49
  requirement: !ruby/object:Gem::Requirement
@@ -62,16 +62,16 @@ dependencies:
62
62
  name: filesize
63
63
  requirement: !ruby/object:Gem::Requirement
64
64
  requirements:
65
- - - ">="
65
+ - - "~>"
66
66
  - !ruby/object:Gem::Version
67
- version: '0'
67
+ version: '0.2'
68
68
  type: :runtime
69
69
  prerelease: false
70
70
  version_requirements: !ruby/object:Gem::Requirement
71
71
  requirements:
72
- - - ">="
72
+ - - "~>"
73
73
  - !ruby/object:Gem::Version
74
- version: '0'
74
+ version: '0.2'
75
75
  - !ruby/object:Gem::Dependency
76
76
  name: net-ssh
77
77
  requirement: !ruby/object:Gem::Requirement
@@ -121,6 +121,7 @@ files:
121
121
  - lib/eac_ruby_utils/common_concern/class_setup.rb
122
122
  - lib/eac_ruby_utils/common_concern/module_setup.rb
123
123
  - lib/eac_ruby_utils/common_constructor.rb
124
+ - lib/eac_ruby_utils/common_constructor/class_accessors.rb
124
125
  - lib/eac_ruby_utils/common_constructor/class_initialize.rb
125
126
  - lib/eac_ruby_utils/common_constructor/instance_initialize.rb
126
127
  - lib/eac_ruby_utils/common_constructor/super_args.rb
@@ -210,6 +211,7 @@ files:
210
211
  - lib/eac_ruby_utils/patches/module/immutable.rb
211
212
  - lib/eac_ruby_utils/patches/module/listable.rb
212
213
  - lib/eac_ruby_utils/patches/module/method_class.rb
214
+ - lib/eac_ruby_utils/patches/module/module_parent.rb
213
215
  - lib/eac_ruby_utils/patches/module/patch.rb
214
216
  - lib/eac_ruby_utils/patches/module/require_sub.rb
215
217
  - lib/eac_ruby_utils/patches/module/simple_cache.rb
@@ -255,6 +257,8 @@ files:
255
257
  - lib/eac_ruby_utils/ruby.rb
256
258
  - lib/eac_ruby_utils/ruby/command.rb
257
259
  - lib/eac_ruby_utils/ruby/on_clean_environment.rb
260
+ - lib/eac_ruby_utils/ruby/on_replace_objects.rb
261
+ - lib/eac_ruby_utils/ruby/on_replace_objects/replace_instance_method.rb
258
262
  - lib/eac_ruby_utils/settings_provider.rb
259
263
  - lib/eac_ruby_utils/settings_provider/setting_value.rb
260
264
  - lib/eac_ruby_utils/simple_cache.rb