smart_core 0.4.0 → 0.5.0
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 +4 -4
- data/.rubocop.yml +2 -2
- data/.travis.yml +3 -3
- data/CHANGELOG.md +3 -0
- data/Gemfile +0 -3
- data/README.md +5 -33
- data/Rakefile +2 -0
- data/lib/smart_core/container/command_definer.rb +117 -0
- data/lib/smart_core/container/command_set.rb +82 -0
- data/lib/smart_core/container/commands/base.rb +12 -0
- data/lib/smart_core/container/commands/namespace.rb +53 -0
- data/lib/smart_core/container/commands/register.rb +63 -0
- data/lib/smart_core/container/commands.rb +9 -0
- data/lib/smart_core/container/definition_dsl.rb +66 -0
- data/lib/smart_core/container/dependency.rb +44 -0
- data/lib/smart_core/container/dependency_builder.rb +48 -0
- data/lib/smart_core/container/dependency_compatability/abstract.rb +59 -0
- data/lib/smart_core/container/dependency_compatability/command_set.rb +35 -0
- data/lib/smart_core/container/dependency_compatability/registry.rb +37 -0
- data/lib/smart_core/container/dependency_compatability.rb +9 -0
- data/lib/smart_core/container/dependency_resolver.rb +16 -0
- data/lib/smart_core/container/entity.rb +26 -0
- data/lib/smart_core/container/exceptions.rb +31 -0
- data/lib/smart_core/container/key_guard.rb +31 -0
- data/lib/smart_core/container/memoized_dependency.rb +28 -0
- data/lib/smart_core/container/mixin.rb +82 -0
- data/lib/smart_core/container/namespace.rb +51 -0
- data/lib/smart_core/container/registry.rb +227 -0
- data/lib/smart_core/container/registry_builder.rb +18 -0
- data/lib/smart_core/container.rb +123 -0
- data/lib/smart_core/exceptions.rb +23 -0
- data/lib/smart_core/initializer/attribute/builder.rb +99 -0
- data/lib/smart_core/initializer/attribute/value_finalizer/lambda.rb +32 -0
- data/lib/smart_core/initializer/attribute/value_finalizer/method.rb +32 -0
- data/lib/smart_core/initializer/attribute/value_finalizer.rb +24 -0
- data/lib/smart_core/initializer/attribute.rb +123 -0
- data/lib/smart_core/initializer/attribute_definer.rb +162 -0
- data/lib/smart_core/{operation → initializer}/attribute_set.rb +17 -17
- data/lib/smart_core/initializer/exceptions.rb +47 -0
- data/lib/smart_core/initializer/extension.rb +39 -0
- data/lib/smart_core/initializer/extension_definer.rb +62 -0
- data/lib/smart_core/initializer/extension_set.rb +75 -0
- data/lib/smart_core/{operation → initializer}/initialization_dsl.rb +82 -42
- data/lib/smart_core/initializer/instance_attribute_accessing.rb +49 -0
- data/lib/smart_core/initializer/instance_builder.rb +164 -0
- data/lib/smart_core/initializer/type.rb +49 -0
- data/lib/smart_core/initializer/type_set.rb +52 -0
- data/lib/smart_core/initializer.rb +89 -0
- data/lib/smart_core/injector.rb +6 -0
- data/lib/smart_core/operation/exceptions.rb +4 -20
- data/lib/smart_core/operation/instance_builder.rb +16 -124
- data/lib/smart_core/operation/state.rb +7 -0
- data/lib/smart_core/operation/step.rb +43 -0
- data/lib/smart_core/operation/step_set.rb +73 -0
- data/lib/smart_core/operation/success.rb +11 -0
- data/lib/smart_core/operation.rb +10 -12
- data/lib/smart_core/schema.rb +6 -0
- data/lib/smart_core/validator/commands.rb +2 -0
- data/lib/smart_core/validator/exceptions.rb +1 -1
- data/lib/smart_core/version.rb +1 -1
- data/lib/smart_core.rb +5 -0
- data/smart_core.gemspec +14 -14
- metadata +57 -15
- data/lib/smart_core/operation/attribute.rb +0 -66
- data/lib/smart_core/operation/attribute_definer.rb +0 -160
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: da28c0f798db1236c70e630dc0f036260075038c6089d0640deec903ab72063b
|
4
|
+
data.tar.gz: b0bf5ff34eece47159f43acf3d7c870adcbcfc10c1f526a16a6c851587cbacb9
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 998f834b2631eb097b3824b00154cdea38103b488de00ae6022f29bddb2d012cfdd2b2039963d7663781e260a4bc841122ceaafaf26540b3dd18c77ac7d0a3fe
|
7
|
+
data.tar.gz: 838c859715897c223c11a6104efcb8110ff00a08f9897cc3e927ac8440a704528cc980262c0554628f967dd8980f6309648256f0b2199e20e7864315360b89db
|
data/.rubocop.yml
CHANGED
@@ -5,7 +5,7 @@ inherit_gem:
|
|
5
5
|
|
6
6
|
AllCops:
|
7
7
|
UseCache: true
|
8
|
-
TargetRubyVersion: 2.6.
|
8
|
+
TargetRubyVersion: 2.6.3
|
9
9
|
Include:
|
10
10
|
- bin/console
|
11
11
|
- lib/**/*.rb
|
@@ -14,6 +14,6 @@ AllCops:
|
|
14
14
|
- Rakefile
|
15
15
|
- smart_core.gemspec
|
16
16
|
|
17
|
-
# NOTE: support for
|
17
|
+
# NOTE: support for old ruby versions
|
18
18
|
Style/RedundantBegin:
|
19
19
|
Enabled: false
|
data/.travis.yml
CHANGED
data/CHANGELOG.md
CHANGED
data/Gemfile
CHANGED
data/README.md
CHANGED
@@ -24,39 +24,11 @@ require 'smart_core'
|
|
24
24
|
|
25
25
|
#### Completed abstractions:
|
26
26
|
|
27
|
-
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
- no dependencies;
|
33
|
-
|
34
|
-
- [**Validation Object**](#validation-object) (`SmartCore::Validator`)
|
35
|
-
- support for nested validations;
|
36
|
-
- inheritance works as expected `:)`;
|
37
|
-
- `#error` - adds an error code;
|
38
|
-
- `#fatal` - adds an error code and stops the current method execution flow;
|
39
|
-
- command-style DSL;
|
40
|
-
- thread-safe;
|
41
|
-
- no dependencies;
|
42
|
-
|
43
|
-
---
|
44
|
-
|
45
|
-
#### Operation Object
|
46
|
-
|
47
|
-
```ruby
|
48
|
-
class Service < SmartCore::Operation
|
49
|
-
# soon...
|
50
|
-
end
|
51
|
-
```
|
52
|
-
|
53
|
-
#### Validation Object
|
54
|
-
|
55
|
-
```ruby
|
56
|
-
class Validator < SmartCore::Validator
|
57
|
-
# soon...
|
58
|
-
end
|
59
|
-
```
|
27
|
+
- Service Object (Operation, Functional Object);
|
28
|
+
- Initializer (DSL);
|
29
|
+
- Validator (Validation Layer);
|
30
|
+
- Dependency Container;
|
31
|
+
- (Soon) Schema;
|
60
32
|
|
61
33
|
---
|
62
34
|
|
data/Rakefile
CHANGED
@@ -4,6 +4,7 @@ require 'bundler/gem_tasks'
|
|
4
4
|
require 'rspec/core/rake_task'
|
5
5
|
require 'rubocop'
|
6
6
|
require 'rubocop-rspec'
|
7
|
+
require 'rubocop-performance'
|
7
8
|
require 'rubocop/rake_task'
|
8
9
|
|
9
10
|
RuboCop::RakeTask.new(:rubocop) do |t|
|
@@ -11,6 +12,7 @@ RuboCop::RakeTask.new(:rubocop) do |t|
|
|
11
12
|
|
12
13
|
t.options = ['--config', config_path]
|
13
14
|
t.requires << 'rubocop-rspec'
|
15
|
+
t.requires << 'rubocop-performance'
|
14
16
|
end
|
15
17
|
|
16
18
|
RSpec::Core::RakeTask.new(:rspec)
|
@@ -0,0 +1,117 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# @api private
|
4
|
+
# @since 0.5.0
|
5
|
+
class SmartCore::Container
|
6
|
+
class CommandDefiner
|
7
|
+
# @param container_klass [Class<SmartCore::Container>]
|
8
|
+
# @return [void]
|
9
|
+
#
|
10
|
+
# @api private
|
11
|
+
# @since 0.5.0
|
12
|
+
def initialize(container_klass)
|
13
|
+
@container_klass = container_klass
|
14
|
+
@access_lock = Mutex.new
|
15
|
+
end
|
16
|
+
|
17
|
+
# @param namespace_name [String, Symbol]
|
18
|
+
# @param dependency_definitions [Proc]
|
19
|
+
# @return [void]
|
20
|
+
#
|
21
|
+
# @api private
|
22
|
+
# @since 0.5.0
|
23
|
+
def append_namespace(namespace_name, dependency_definitions)
|
24
|
+
thread_safe do
|
25
|
+
command = build_namespace_command(namespace_name, dependency_definitions)
|
26
|
+
prevent_dependency_overlap!(command)
|
27
|
+
append_command(command)
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
# @param dependency_name [String, Symbol]
|
32
|
+
# @param dependency_definition [Proc]
|
33
|
+
# @param options [Hash<Symbol,Any>]
|
34
|
+
# @return [void]
|
35
|
+
#
|
36
|
+
# @api private
|
37
|
+
# @since 0.5.0
|
38
|
+
def append_register(dependency_name, dependency_definition, options)
|
39
|
+
thread_safe do
|
40
|
+
command = build_register_command(dependency_name, dependency_definition, options)
|
41
|
+
prevent_namespace_overlap!(command)
|
42
|
+
append_command(command)
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
private
|
47
|
+
|
48
|
+
# @return [Class<SmartCore::Container>]
|
49
|
+
#
|
50
|
+
# @api private
|
51
|
+
# @since 0.5.0
|
52
|
+
attr_reader :container_klass
|
53
|
+
|
54
|
+
# @param namespace_name [String]
|
55
|
+
# @return [void]
|
56
|
+
#
|
57
|
+
# @api private
|
58
|
+
# @since 0.5.0
|
59
|
+
def prevent_dependency_overlap(namespace_name)
|
60
|
+
DependencyCompatability::CommandSet.prevent_dependency_overlap!(
|
61
|
+
container_klass, namespace_name
|
62
|
+
)
|
63
|
+
end
|
64
|
+
|
65
|
+
# @param dependency_name [String]
|
66
|
+
# @return [void]
|
67
|
+
#
|
68
|
+
# @api private
|
69
|
+
# @since 0.5.0
|
70
|
+
def prevent_namespace_overlap(dependency_name)
|
71
|
+
DependencyCompatability::CommandSet.prevent_namespace_overlap!(
|
72
|
+
container_klass, dependency_name
|
73
|
+
)
|
74
|
+
end
|
75
|
+
|
76
|
+
# @param namespace_name [String]
|
77
|
+
# @param dependency_definitions [Proc]
|
78
|
+
# @return [SmartCore::Container::Commands::Namespace]
|
79
|
+
#
|
80
|
+
# @api private
|
81
|
+
# @since 0.5.0
|
82
|
+
def build_namespace_command(namespace_name, dependency_definitions)
|
83
|
+
SmartCore::Container::Commands::Namespace.new(
|
84
|
+
namespace_name, dependency_definitions
|
85
|
+
)
|
86
|
+
end
|
87
|
+
|
88
|
+
# @param dependency_name [String]
|
89
|
+
# @param dependency_definition [Proc]
|
90
|
+
# @param option [Hash<Symbol,Any>]
|
91
|
+
# @return [SmartCore::Container::Commands::Register]
|
92
|
+
#
|
93
|
+
def build_register_command(dependency_name, dependency_definition, options)
|
94
|
+
SmartCore::Container::Commands::Register.new(
|
95
|
+
dependency_name, dependency_definition, **options
|
96
|
+
)
|
97
|
+
end
|
98
|
+
|
99
|
+
# @param command [SmartCore::Container::Commands::Base]
|
100
|
+
# @return [void]
|
101
|
+
#
|
102
|
+
# @api private
|
103
|
+
# @since 0.5.0
|
104
|
+
def append_command(command)
|
105
|
+
container_klass.__commands__.add_command(command)
|
106
|
+
end
|
107
|
+
|
108
|
+
# @param block [Block]
|
109
|
+
# @return [Any]
|
110
|
+
#
|
111
|
+
# @api private
|
112
|
+
# @since 0.5.0
|
113
|
+
def thread_safe(&block)
|
114
|
+
@access_lock.synchronize(&block)
|
115
|
+
end
|
116
|
+
end
|
117
|
+
end
|
@@ -0,0 +1,82 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# @api private
|
4
|
+
# @since 0.5.0
|
5
|
+
class SmartCore::Container::CommandSet
|
6
|
+
# @since 0.5.0
|
7
|
+
include Enumerable
|
8
|
+
|
9
|
+
# @return [Array<SmartCore::Container::Commands::Base>]
|
10
|
+
#
|
11
|
+
# @api private
|
12
|
+
# @since 0.5.0
|
13
|
+
attr_reader :commands
|
14
|
+
|
15
|
+
# @api private
|
16
|
+
# @since 0.5.0
|
17
|
+
def initialize
|
18
|
+
@commands = []
|
19
|
+
@access_lock = Mutex.new
|
20
|
+
end
|
21
|
+
|
22
|
+
# @param command [SmartCore::Container::Commands::Base]
|
23
|
+
# @return [void]
|
24
|
+
#
|
25
|
+
# @api private
|
26
|
+
# @since 0.5.0
|
27
|
+
def add_command(command)
|
28
|
+
thread_safe { commands << command }
|
29
|
+
end
|
30
|
+
alias_method :<<, :add_command
|
31
|
+
|
32
|
+
# @yield [SmartCore::Container::Commands::Base]
|
33
|
+
# @return [Enumerable]
|
34
|
+
#
|
35
|
+
# @api private
|
36
|
+
# @since 0.5.0
|
37
|
+
def each(&block)
|
38
|
+
thread_safe { block_given? ? commands.each(&block) : commands.each }
|
39
|
+
end
|
40
|
+
|
41
|
+
# @param command_set [SmartCore::Container::CommandSet]
|
42
|
+
# @return [void]
|
43
|
+
#
|
44
|
+
# @api private
|
45
|
+
# @since 0.5.0
|
46
|
+
def concat(command_set)
|
47
|
+
thread_safe { commands.concat(command_set.dup.commands) }
|
48
|
+
end
|
49
|
+
|
50
|
+
# @return [SmartCore::Operation::AttributeSet]
|
51
|
+
#
|
52
|
+
# @api private
|
53
|
+
# @since 0.2.0
|
54
|
+
def dup
|
55
|
+
thread_safe do
|
56
|
+
self.class.new.tap do |duplicate|
|
57
|
+
commands.each do |command|
|
58
|
+
duplicate.add_command(command.dup)
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
# @return [void]
|
65
|
+
#
|
66
|
+
# @api private
|
67
|
+
# @since 0.5.0
|
68
|
+
def clear
|
69
|
+
thread_safe { commands.clear }
|
70
|
+
end
|
71
|
+
|
72
|
+
private
|
73
|
+
|
74
|
+
# @param block [Proc]
|
75
|
+
# @return [Any]
|
76
|
+
#
|
77
|
+
# @api private
|
78
|
+
# @since 0.5.0
|
79
|
+
def thread_safe(&block)
|
80
|
+
@access_lock.synchronize(&block)
|
81
|
+
end
|
82
|
+
end
|
@@ -0,0 +1,53 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module SmartCore::Container::Commands
|
4
|
+
# @api private
|
5
|
+
# @since 0.5.0
|
6
|
+
class Namespace < Base
|
7
|
+
# @return [String, Symbol]
|
8
|
+
#
|
9
|
+
# @api private
|
10
|
+
# @since 0.5.0
|
11
|
+
attr_reader :namespace_name
|
12
|
+
|
13
|
+
# @param namespace_name [String, Symbol]
|
14
|
+
# @param dependency_definitions [Proc]
|
15
|
+
# @return [void]
|
16
|
+
#
|
17
|
+
# @see [SmartCore::Container::KeyGuard]
|
18
|
+
#
|
19
|
+
# @api private
|
20
|
+
# @since 0.5.0
|
21
|
+
def initialize(namespace_name, dependency_definitions)
|
22
|
+
SmartCore::Container::KeyGuard.indifferently_accessable_key(namespace_name).tap do |name|
|
23
|
+
@namespace_name = name
|
24
|
+
@dependency_definitions = dependency_definitions
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
# @param registry [SmartCore::Container::Registry]
|
29
|
+
# @return [void]
|
30
|
+
#
|
31
|
+
# @api private
|
32
|
+
# @since 0.5.0
|
33
|
+
def call(registry)
|
34
|
+
registry.namespace(namespace_name, &dependency_definitions)
|
35
|
+
end
|
36
|
+
|
37
|
+
# @return [SmartCore::Container::Commands::Namespace]
|
38
|
+
#
|
39
|
+
# @api private
|
40
|
+
# @since 0.5.0
|
41
|
+
def dup
|
42
|
+
self.class.new(namespace_name, dependency_definitions)
|
43
|
+
end
|
44
|
+
|
45
|
+
private
|
46
|
+
|
47
|
+
# @return [Proc]
|
48
|
+
#
|
49
|
+
# @api private
|
50
|
+
# @since 0.5.0
|
51
|
+
attr_reader :dependency_definitions
|
52
|
+
end
|
53
|
+
end
|
@@ -0,0 +1,63 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module SmartCore::Container::Commands
|
4
|
+
# @api private
|
5
|
+
# @since 0.5.0
|
6
|
+
class Register < Base
|
7
|
+
# @return [String, Symbol]
|
8
|
+
#
|
9
|
+
# @api private
|
10
|
+
# @since 0.5.0
|
11
|
+
attr_reader :dependency_name
|
12
|
+
|
13
|
+
# @param dependency_name [String, Symbol]
|
14
|
+
# @param options [Hash<Symbol,Any>]
|
15
|
+
# @param dependency_definition [Proc]
|
16
|
+
# @option memoize [Boolean]
|
17
|
+
# @return [void]
|
18
|
+
#
|
19
|
+
# @todo option list
|
20
|
+
# @see [SmartCore::Container::KeyGuard]
|
21
|
+
#
|
22
|
+
# @api private
|
23
|
+
# @since 0.5.0
|
24
|
+
def initialize(dependency_name, dependency_definition, **options)
|
25
|
+
SmartCore::Container::KeyGuard.indifferently_accessable_key(dependency_name).tap do |name|
|
26
|
+
@dependency_name = name
|
27
|
+
@options = options
|
28
|
+
@dependency_definition = dependency_definition
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
# @param registry [SmartCore::Container::Registry]
|
33
|
+
# @return [void]
|
34
|
+
#
|
35
|
+
# @api private
|
36
|
+
# @since 0.5.0
|
37
|
+
def call(registry)
|
38
|
+
registry.register(dependency_name, **options, &dependency_definition)
|
39
|
+
end
|
40
|
+
|
41
|
+
# @return [SmartCore::Container::Commands::Register]
|
42
|
+
#
|
43
|
+
# @api private
|
44
|
+
# @since 0.5.0
|
45
|
+
def dup
|
46
|
+
self.class.new(dependency_name, dependency_definition, **options)
|
47
|
+
end
|
48
|
+
|
49
|
+
private
|
50
|
+
|
51
|
+
# @return [Proc]
|
52
|
+
#
|
53
|
+
# @api private
|
54
|
+
# @since 0.5.0
|
55
|
+
attr_reader :dependency_definition
|
56
|
+
|
57
|
+
# @return [Hash<Symbol,Any>]
|
58
|
+
#
|
59
|
+
# @api private
|
60
|
+
# @since 0.5.0
|
61
|
+
attr_reader :options
|
62
|
+
end
|
63
|
+
end
|
@@ -0,0 +1,66 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# @api private
|
4
|
+
# @since 0.5.0
|
5
|
+
module SmartCore::Container::DefinitionDSL
|
6
|
+
class << self
|
7
|
+
# @param base_klass [Class<SmartCore::Container>]
|
8
|
+
# @return [void]
|
9
|
+
#
|
10
|
+
# @api private
|
11
|
+
# @since 0.5.0
|
12
|
+
def included(base_klass)
|
13
|
+
base_klass.instance_variable_set(:@__commands__, SmartCore::Container::CommandSet.new)
|
14
|
+
base_klass.singleton_class.send(:attr_reader, :__commands__)
|
15
|
+
base_klass.extend(ClassMethods)
|
16
|
+
base_klass.singleton_class.prepend(ClassInheritance)
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
# @api private
|
21
|
+
# @since 0.5.0
|
22
|
+
module ClassInheritance
|
23
|
+
# @param child_klass [Class<SmartCore::Container>]
|
24
|
+
# @return [void]
|
25
|
+
#
|
26
|
+
# @api private
|
27
|
+
# @since 0.5.0
|
28
|
+
def inherited(child_klass)
|
29
|
+
child_klass.instance_variable_set(:@__commands__, SmartCore::Container::CommandSet.new)
|
30
|
+
child_klass.__commands__.concat(__commands__)
|
31
|
+
child_klass.singleton_class.prepend(ClassInheritance)
|
32
|
+
super
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
# @api private
|
37
|
+
# @since 0.5.0
|
38
|
+
module ClassMethods
|
39
|
+
# @param namespace_name [String, Symbol]
|
40
|
+
# @param dependency_definitions [Block]
|
41
|
+
# @return [void]
|
42
|
+
#
|
43
|
+
# @api public
|
44
|
+
# @since 0.5.0
|
45
|
+
def namespace(namespace_name, &dependency_definitions)
|
46
|
+
__commands__ << SmartCore::Container::Commands::Namespace.new(
|
47
|
+
namespace_name, dependency_definitions
|
48
|
+
)
|
49
|
+
end
|
50
|
+
|
51
|
+
# @param dependency_name [String, Symbol]
|
52
|
+
# @param options [Hash<Symbol,Any>]
|
53
|
+
# @param dependency_definition [Proc]
|
54
|
+
# @return [void]
|
55
|
+
#
|
56
|
+
# @todo option list
|
57
|
+
#
|
58
|
+
# @api public
|
59
|
+
# @since 0.5.0
|
60
|
+
def register(dependency_name, **options, &dependency_definition)
|
61
|
+
__commands__ << SmartCore::Container::Commands::Register.new(
|
62
|
+
dependency_name, dependency_definition, **options
|
63
|
+
)
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
@@ -0,0 +1,44 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# @api private
|
4
|
+
# @since 0.5.0
|
5
|
+
class SmartCore::Container::Dependency < SmartCore::Container::Entity
|
6
|
+
# @param dependency_definition [Proc]
|
7
|
+
# @param options [Hash<Symbol,Any>]
|
8
|
+
# @return [void]
|
9
|
+
#
|
10
|
+
# @todo option list
|
11
|
+
#
|
12
|
+
# @api private
|
13
|
+
# @since 0.5.0
|
14
|
+
def initialize(external_name, dependency_definition, **options)
|
15
|
+
@dependency_definition = dependency_definition
|
16
|
+
@access_lock = Mutex.new
|
17
|
+
super(external_name)
|
18
|
+
end
|
19
|
+
|
20
|
+
# @return [Any]
|
21
|
+
#
|
22
|
+
# @api private
|
23
|
+
# @since 0.5.0
|
24
|
+
def call
|
25
|
+
thread_safe { dependency_definition.call }
|
26
|
+
end
|
27
|
+
|
28
|
+
private
|
29
|
+
|
30
|
+
# @return [Proc]
|
31
|
+
#
|
32
|
+
# @api private
|
33
|
+
# @since 0.5.0
|
34
|
+
attr_reader :dependency_definition
|
35
|
+
|
36
|
+
# @param block [Proc]
|
37
|
+
# @return [Any]
|
38
|
+
#
|
39
|
+
# @api private
|
40
|
+
# @since 0.5.0
|
41
|
+
def thread_safe(&block)
|
42
|
+
@access_lock.synchronize(&block)
|
43
|
+
end
|
44
|
+
end
|
@@ -0,0 +1,48 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# @api private
|
4
|
+
# @since 0.5.0
|
5
|
+
module SmartCore::Container::DependencyBuilder
|
6
|
+
class << self
|
7
|
+
# @param external_name [String]
|
8
|
+
# @param dependency_definition [Proc]
|
9
|
+
# @option memoize [Boolean]
|
10
|
+
# @return [SmartCore::Container::Dependency, SmartCore::Container::MemoizedDependency]
|
11
|
+
#
|
12
|
+
# @api private
|
13
|
+
# @since 0.5.0
|
14
|
+
def build(external_name, dependency_definition, memoize: false, **options)
|
15
|
+
# @todo: raise an error if memoize is not a boolean
|
16
|
+
|
17
|
+
if memoize
|
18
|
+
build_memoized_dependency(external_name, dependency_definition, **options)
|
19
|
+
else
|
20
|
+
build_dependency(external_name, dependency_definition, **options)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
private
|
25
|
+
|
26
|
+
# @param external_name [String]
|
27
|
+
# @param dependency_definition [Proc]
|
28
|
+
# @param options [Hash<Symbol,Any>]
|
29
|
+
# @return [SmartCore::Container::MemoizedDependency]
|
30
|
+
#
|
31
|
+
# @api private
|
32
|
+
# @since 0.5.0
|
33
|
+
def build_memoized_dependency(external_name, dependency_definition, **options)
|
34
|
+
SmartCore::Container::MemoizedDependency.new(external_name, dependency_definition, **options)
|
35
|
+
end
|
36
|
+
|
37
|
+
# @param external_name [String]
|
38
|
+
# @param dependency_definition [Proc]
|
39
|
+
# @param options [Hash<Symbol,Any>]
|
40
|
+
# @return [SmartCore::Container::Dependency]
|
41
|
+
#
|
42
|
+
# @api private
|
43
|
+
# @since 0.5.0
|
44
|
+
def build_dependency(external_name, dependency_definition, **options)
|
45
|
+
SmartCore::Container::Dependency.new(external_name, dependency_definition, **options)
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
@@ -0,0 +1,59 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# @api private
|
4
|
+
# @since 0.5.0
|
5
|
+
class SmartCore::Container::DependencyCompatability::Abstract
|
6
|
+
class << self
|
7
|
+
# @param dependency_root [Any]
|
8
|
+
# @param dependency_name [String]
|
9
|
+
# @return [void]
|
10
|
+
#
|
11
|
+
# @raise [SmartCore::Container::NamespaceOverlapError]
|
12
|
+
#
|
13
|
+
# @api private
|
14
|
+
# @since 0.5.0
|
15
|
+
def prevent_namespace_overlap!(dependency_root, dependency_name)
|
16
|
+
raise(
|
17
|
+
SmartCore::Container::NamespaceOverlapError,
|
18
|
+
"Trying to overlap already registered :#{dependency_name} " \
|
19
|
+
"namespace with :#{dependency_name} dependency!"
|
20
|
+
) if potential_namespace_overlap?(dependency_root, dependency_name)
|
21
|
+
end
|
22
|
+
|
23
|
+
# @param dependency_root [Any]
|
24
|
+
# @param namespace_name [String]
|
25
|
+
# @return [void]
|
26
|
+
#
|
27
|
+
# @raise [SmartCore::Container::DependencyOverlapError]
|
28
|
+
#
|
29
|
+
# @api private
|
30
|
+
# @since 0.5.0
|
31
|
+
def prevent_dependency_overlap!(dependency_root, namespace_name)
|
32
|
+
raise(
|
33
|
+
SmartCore::Container::DependencyOverlapError,
|
34
|
+
"Trying to overlap already registered :#{namespace_name} " \
|
35
|
+
"dependency with :#{namespace_name} namespace!"
|
36
|
+
) if potential_dependency_overlap?(dependency_root, namespace_name)
|
37
|
+
end
|
38
|
+
|
39
|
+
# @param dependency_root [Any]
|
40
|
+
# @param dependency [Any]
|
41
|
+
# @return [Boolean]
|
42
|
+
#
|
43
|
+
# @api private
|
44
|
+
# @since 0.5.0
|
45
|
+
def potential_namespace_overlap?(dependency_root, dependency)
|
46
|
+
raise NoMethodError
|
47
|
+
end
|
48
|
+
|
49
|
+
# @param dependency_root [Any]
|
50
|
+
# @param namespace [Any]
|
51
|
+
# @return [Boolean]
|
52
|
+
#
|
53
|
+
# @api private
|
54
|
+
# @since 0.5.0
|
55
|
+
def potential_dependency_overlap?(dependency_root, namespace)
|
56
|
+
raise NoMethodError
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|