aux 0.0.3 → 0.0.5

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: e4131935771dd658e83d739479ccd2cac86caf8ef96162328807d73cad1cc741
4
- data.tar.gz: 388c65d71c5902f843a4537cc6f75cce9af412403920632a03e9a27397561b2f
3
+ metadata.gz: 4b5ace631919c60e6e5204bfb33dad68a6b8583c98d26206b613811b15b2fe29
4
+ data.tar.gz: 77bb7c2c22c97181fc1e5d10ecf77dba3296f0b188c38ede324d221777613f02
5
5
  SHA512:
6
- metadata.gz: 3812e0f949dfd058abdc242d086e0b87487d275b0fd2443f871022e11b5ccef21decabf361fef3efd39db9303896694e17a8519e2d0857155919585a2ab92e77
7
- data.tar.gz: cdbc3645ac99b5e5a42f26a2a0cf268fa03c4f6a108cc4f33c1652354d58a27a8f06ba28a3f327fe557a732dce0fc14a8613127794260182459fe0a3283d8cd8
6
+ metadata.gz: 7edf473b2d627ca0fb4c9f16197177eb3de5d98a042b269097d1f5317e440dac298cac592aede48e8ecf42855f9bb78c86887f0611fee770f5fb164bf871b164
7
+ data.tar.gz: 2aa2ffbe10186145ce53a5e54a9759169883d789c618575798eba40f056cc4107add3abc942ce105280a5feef7ef9befe93f326d05764b75d558aa9149489d91
@@ -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.5'
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.5
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
@@ -64,8 +64,7 @@ files:
64
64
  - lib/aux.rb
65
65
  - lib/aux/pluggable.rb
66
66
  - lib/aux/pluggable/class_methods.rb
67
- - lib/aux/pluggable/registration.rb
68
- - lib/aux/utilities.rb
67
+ - lib/aux/pluggable/utilities.rb
69
68
  - lib/aux/validations.rb
70
69
  - lib/aux/validations/error.rb
71
70
  - lib/aux/validations/errors.rb
@@ -1,25 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Aux
4
- module Pluggable
5
- # Describes a wrapper for registry data
6
- class Registration
7
- # @!attribute [r] name
8
- # @return [Symbol, String]
9
- # @!attribute [r] initialization_required
10
- # @return [TrueClass, FalseClass]
11
- # @!attribute [r] memoization_required
12
- # @return [TrueClass, FalseClass]
13
- attr_reader :name, :initialization_required, :memoization_required
14
-
15
- # @param name [Symbol, String]
16
- # @param initialization_required [TrueClass, FalseClass]
17
- # @param memoization_required [TrueClass, FalseClass]
18
- def initialize(name, initialization_required, memoization_required)
19
- @name = name
20
- @initialization_required = initialization_required
21
- @memoization_required = memoization_required
22
- end
23
- end
24
- end
25
- end
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