eac_ruby_utils 0.99.0 → 0.102.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: 788923b15e15ee32a9ef7b73d99a04cfbc2b6b263bb0277541714267b07ab511
4
- data.tar.gz: 9a13bab214667dbb9e616d33bfb147ef6c7d8abb0fb9249f6f432f34be6db077
3
+ metadata.gz: 2c7e86736386edb4c749cb5fbe79308fc1fd55021153c0dda8cb663ed85e50ab
4
+ data.tar.gz: 350abe9716ee7bc7ac031c65c7ac94b029a1b1a3f0ea80de98cb877519c53d6f
5
5
  SHA512:
6
- metadata.gz: b5e8c453a6fcb2d6803f3fd1b7bd41386e9be8e53b74d3dba54892366ede77b279addb98a44030a6ac6b85f3c3781306fd8b8062fe77a4ac8619b8da8ce943f9
7
- data.tar.gz: 77665df94b421278636db62c478a6b2c2716e42f3661799d1bbebea13f20ca4dcc9753a246fbf47a48866791adf06389173fc2006e01fb69a9b87293b86e5ea6
6
+ metadata.gz: c2a88f7354c4529dc5a00e00f19276b5445d226375e135ff35eb4aa0e01cf7bf4326efafdb0524c87f9430dc779b16215a806a3558a19ff3ba1c540d52a25e93
7
+ data.tar.gz: 6a8c6f5e3bef91ea69eee363ed327893385b6ee7f93063078957c3fa55b509b0c30a28cba603a9872be2b18d5172e5310c116dd6c9c54de85a15b9e40ce8cd27
@@ -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
@@ -1,18 +1,33 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class Object
4
- def pretty_debug
4
+ def pretty_debug(options = {})
5
+ print_debug_title(options)
6
+
5
7
  STDERR.write(pretty_inspect)
6
8
 
7
9
  self
8
10
  end
9
11
 
10
- def print_debug
12
+ def print_debug(options = {})
13
+ print_debug_title(options)
11
14
  STDERR.write(to_debug + "\n")
12
15
 
13
16
  self
14
17
  end
15
18
 
19
+ def print_debug_title(title)
20
+ if title.is_a?(::Hash)
21
+ return unless title[:title]
22
+
23
+ title = title[:title]
24
+ end
25
+ char = '='
26
+ STDERR.write(char * (4 + title.length) + "\n")
27
+ STDERR.write("#{char} #{title} #{char}\n")
28
+ STDERR.write(char * (4 + title.length) + "\n")
29
+ end
30
+
16
31
  def to_debug
17
32
  "|#{::Object.instance_method(:to_s).bind(self).call}|#{self}|"
18
33
  end
@@ -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.99.0'
4
+ VERSION = '0.102.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.99.0
4
+ version: 0.102.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-08-14 00:00:00.000000000 Z
11
+ date: 2022-08-18 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
@@ -255,6 +256,8 @@ files:
255
256
  - lib/eac_ruby_utils/ruby.rb
256
257
  - lib/eac_ruby_utils/ruby/command.rb
257
258
  - lib/eac_ruby_utils/ruby/on_clean_environment.rb
259
+ - lib/eac_ruby_utils/ruby/on_replace_objects.rb
260
+ - lib/eac_ruby_utils/ruby/on_replace_objects/replace_instance_method.rb
258
261
  - lib/eac_ruby_utils/settings_provider.rb
259
262
  - lib/eac_ruby_utils/settings_provider/setting_value.rb
260
263
  - lib/eac_ruby_utils/simple_cache.rb