aux 0.0.3 → 0.0.4

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: e4131935771dd658e83d739479ccd2cac86caf8ef96162328807d73cad1cc741
4
- data.tar.gz: 388c65d71c5902f843a4537cc6f75cce9af412403920632a03e9a27397561b2f
3
+ metadata.gz: acf9da895d77d8ab39cc92765e586e69e0300e15cedc201fdd18a30a02221d4c
4
+ data.tar.gz: c6aa3892d3a1691f2e0192a3094c33838a5e5b40121538b06862e1c7491c7190
5
5
  SHA512:
6
- metadata.gz: 3812e0f949dfd058abdc242d086e0b87487d275b0fd2443f871022e11b5ccef21decabf361fef3efd39db9303896694e17a8519e2d0857155919585a2ab92e77
7
- data.tar.gz: cdbc3645ac99b5e5a42f26a2a0cf268fa03c4f6a108cc4f33c1652354d58a27a8f06ba28a3f327fe557a732dce0fc14a8613127794260182459fe0a3283d8cd8
6
+ metadata.gz: c10a70b5b35a62161e4f6c48362fda7bc51e08a046c638ac3d8a8311fac53648fc1bdcd526e0c4d7302e1c9ce36c80d97f845dc0fa5d32f9e8ebc1332aad408d
7
+ data.tar.gz: ae97078e7e7cc0d627711a1c147befecadcfa70e057376378402d59a8dda844ad63f4596a10fd1ee26c594f0db8f8189d0bfdb6e9a3962b557899747503b3c35
@@ -1,13 +1,10 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'aux/utilities'
4
-
5
3
  module Aux
6
4
  module Pluggable
7
5
  # Describes methods that would be inherited by pluggable classes
8
6
  module ClassMethods
9
7
  # @param dependencies [Hash{Symbol => Object}]
10
- # @return [self]
11
8
  def new(**dependencies)
12
9
  instance = allocate
13
10
 
@@ -18,9 +15,12 @@ module Aux
18
15
 
19
16
  # Configure dependencies that come from the customized initialization procedure
20
17
  dependencies.each do |name, dependency|
21
- instance.instance_variable_set("@#{name}", dependency)
18
+ instance.instance_variable_set("@#{name}", dependency) if @_dependencies.include?(name)
22
19
  end
23
20
 
21
+ # Run the rest of customized initialization procedure
22
+ instance.send(:initialize, **dependencies)
23
+
24
24
  instance
25
25
  end
26
26
 
@@ -28,38 +28,27 @@ module Aux
28
28
 
29
29
  # @param initialize [TrueClass, FalseClass]
30
30
  # @param memoize [TrueClass, FalseClass]
31
+ # @param scope [TrueClass, Symbol, String, nil]
31
32
  # @param as [Symbol, String, nil]
32
- def register(initialize: false, memoize: false, as: nil)
33
- @_registration = Registration.new(as || Aux::Utilities.underscore(name), initialize, memoize)
34
-
35
- @_registry.register(@_registration.name, memoize: @_registration.memoization_required) do
36
- @_registration.initialization_required ? new : self
37
- end
33
+ def register(initialize: false, memoize: false, scope: true, as: nil)
34
+ dependency_cipher = Utilities.dependency_cipher(name, scope: scope, code: as)
35
+ @_registry.register(dependency_cipher, memoize: memoize) { initialize ? new : self }
38
36
  end
39
37
 
40
38
  # @param code [Symbol, String]
41
39
  # @param initialization_block [Proc, nil]
42
- # @param scope [TrueClass, Symbol, String]
40
+ # @param scope [TrueClass, Symbol, String, nil]
43
41
  # @param as [Symbol, String]
44
- # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength, Metrics/PerceivedComplexity
42
+ # rubocop:disable Metrics/AbcSize, Metrics/MethodLength
45
43
  def resolve(code, initialization_block = nil, scope: true, as: nil)
46
- # First, we need to determine the appropriate namespace (also called the scope) in which to resolve something.
47
- # By default, we assume that the developers want to resolve a dependency from the same namespace as the
48
- # referencing class. Another approach is to allow developers to set the correct scope themselves.
49
- scope =
50
- case scope
51
- when TrueClass
52
- Aux::Utilities.underscore(name).rpartition('.').first
53
- when Symbol, String
54
- scope
55
- end
44
+ dependency_cipher = Utilities.dependency_cipher(name, scope: scope, code: code)
45
+ dependency_alias = as || code
56
46
 
57
47
  # The process of preloading classes is a bit messy. Therefore, why we wrap the dependency in a Proc.
58
48
  # This allows us not to worry about the absence of the referenced class in the registry during assembly.
59
49
  # Using an initialization block can be convenient in some cases, for example, to resolve some
60
50
  # of the global settings without accessing the whole thing.
61
- dependency_cipher = [scope, code].reject { |part| part.nil? || part.empty? }.join('.')
62
- dependency =
51
+ @_dependencies[dependency_alias] =
63
52
  if initialization_block
64
53
  -> { initialization_block.call(@_registry.resolve(dependency_cipher)) }
65
54
  else
@@ -67,16 +56,10 @@ module Aux
67
56
  end
68
57
 
69
58
  # The final step is to declare instance variables and methods to access them.
70
- dependency_alias = as || code
71
-
72
- if defined?(@_registration) && @_registration&.initialization_required
73
- @_dependencies[dependency_alias] = dependency
74
- end
75
-
76
59
  instance_eval do
77
60
  define_method(dependency_alias) do
78
61
  if instance_variable_get("@#{dependency_alias}").is_a?(Proc)
79
- instance_variable_set("@#{dependency_alias}", instance_variable_get("@#{dependency_alias}").call)
62
+ instance_variable_get("@#{dependency_alias}").call
80
63
  else
81
64
  instance_variable_get("@#{dependency_alias}")
82
65
  end
@@ -86,11 +69,11 @@ module Aux
86
69
  end
87
70
 
88
71
  singleton_class.class_eval do
89
- define_method(dependency_alias) { dependency.call }
72
+ define_method(dependency_alias) { @_dependencies[dependency_alias].call }
90
73
  private dependency_alias
91
74
  end
92
75
  end
93
- # rubocop:enable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength, Metrics/PerceivedComplexity
76
+ # rubocop:enable Metrics/AbcSize, Metrics/MethodLength
94
77
  end
95
78
  end
96
79
  end
@@ -0,0 +1,31 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Aux
4
+ module Pluggable
5
+ # Random methods for internal usage
6
+ module Utilities
7
+ # First, we need to determine the appropriate namespace (also called the scope) in which to resolve something.
8
+ # By default, we assume that the developers want to resolve a dependency from the same namespace as the
9
+ # referencing class. Another approach is to allow developers to set the correct scope themselves.
10
+ #
11
+ # @param class_name [Class]
12
+ # @param scope [TrueClass, Symbol, String, nil]
13
+ # @param code [TrueClass, Symbol, String, nil]
14
+ # @return [String]
15
+ def self.dependency_cipher(class_name, scope: nil, code: nil)
16
+ native_cipher = dependency_native_cipher(class_name)
17
+ native_cipher_partitions = native_cipher.rpartition('.')
18
+ scope = scope.is_a?(TrueClass) ? native_cipher_partitions.first : scope
19
+ code = code.nil? ? native_cipher_partitions.last : code
20
+
21
+ [scope, code].reject { |part| part.nil? || part.empty? }.join('.')
22
+ end
23
+
24
+ # @param class_name [Class]
25
+ # @return [String]
26
+ def self.dependency_native_cipher(class_name)
27
+ class_name.dup.gsub(/::/, '.').gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2').gsub(/([a-z\d])([A-Z])/, '\1_\2').downcase
28
+ end
29
+ end
30
+ end
31
+ end
data/lib/aux/pluggable.rb CHANGED
@@ -1,7 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'aux/pluggable/class_methods'
4
- require 'aux/pluggable/registration'
4
+ require 'aux/pluggable/utilities'
5
5
 
6
6
  module Aux
7
7
  # Describes interface that makes any class able to register itself as well as resolve dependencies
data/lib/aux/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Aux
4
- VERSION = '0.0.3'
4
+ VERSION = '0.0.4'
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: aux
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Evgeny Boyko
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-02-28 00:00:00.000000000 Z
11
+ date: 2023-03-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activemodel
@@ -65,7 +65,7 @@ files:
65
65
  - lib/aux/pluggable.rb
66
66
  - lib/aux/pluggable/class_methods.rb
67
67
  - lib/aux/pluggable/registration.rb
68
- - lib/aux/utilities.rb
68
+ - lib/aux/pluggable/utilities.rb
69
69
  - lib/aux/validations.rb
70
70
  - lib/aux/validations/error.rb
71
71
  - lib/aux/validations/errors.rb
data/lib/aux/utilities.rb DELETED
@@ -1,12 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Aux
4
- # Random methods for internal usage
5
- module Utilities
6
- # @param class_name [String]
7
- # @return [String]
8
- def self.underscore(class_name)
9
- class_name.dup.gsub(/::/, '.').gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2').gsub(/([a-z\d])([A-Z])/, '\1_\2').downcase
10
- end
11
- end
12
- end