smart_core 0.6.0 → 0.7.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (52) hide show
  1. checksums.yaml +4 -4
  2. data/.rspec +1 -1
  3. data/.rubocop.yml +1 -1
  4. data/.travis.yml +5 -8
  5. data/CHANGELOG.md +5 -0
  6. data/README.md +14 -4
  7. data/lib/smart_core.rb +2 -2
  8. data/lib/smart_core/container.rb +49 -56
  9. data/lib/smart_core/container/arbitary_lock.rb +22 -0
  10. data/lib/smart_core/container/definition_dsl.rb +105 -55
  11. data/lib/smart_core/container/{command_set.rb → definition_dsl/command_set.rb} +20 -26
  12. data/lib/smart_core/container/definition_dsl/commands.rb +12 -0
  13. data/lib/smart_core/container/{commands → definition_dsl/commands}/base.rb +3 -3
  14. data/lib/smart_core/container/definition_dsl/commands/definition/compose.rb +46 -0
  15. data/lib/smart_core/container/definition_dsl/commands/definition/namespace.rb +51 -0
  16. data/lib/smart_core/container/{commands → definition_dsl/commands/definition}/register.rb +13 -25
  17. data/lib/smart_core/container/definition_dsl/commands/instantiation/compose.rb +50 -0
  18. data/lib/smart_core/container/definition_dsl/commands/instantiation/freeze_state.rb +24 -0
  19. data/lib/smart_core/container/dependency_compatability.rb +3 -3
  20. data/lib/smart_core/container/dependency_compatability/definition.rb +42 -0
  21. data/lib/smart_core/container/dependency_compatability/general.rb +61 -0
  22. data/lib/smart_core/container/dependency_compatability/registry.rb +28 -29
  23. data/lib/smart_core/container/dependency_resolver.rb +9 -4
  24. data/lib/smart_core/container/entities.rb +11 -0
  25. data/lib/smart_core/container/{entity.rb → entities/base.rb} +7 -7
  26. data/lib/smart_core/container/entities/dependency.rb +38 -0
  27. data/lib/smart_core/container/entities/dependency_builder.rb +50 -0
  28. data/lib/smart_core/container/entities/namespace.rb +73 -0
  29. data/lib/smart_core/container/entities/namespace_builder.rb +41 -0
  30. data/lib/smart_core/container/errors.rb +43 -0
  31. data/lib/smart_core/container/key_guard.rb +5 -5
  32. data/lib/smart_core/container/mixin.rb +20 -16
  33. data/lib/smart_core/container/registry.rb +224 -201
  34. data/lib/smart_core/container/registry_builder.rb +38 -5
  35. data/lib/smart_core/{exceptions.rb → errors.rb} +0 -0
  36. data/lib/smart_core/operation.rb +3 -1
  37. data/lib/smart_core/operation/{custom.rb → callback.rb} +7 -7
  38. data/lib/smart_core/operation/result.rb +1 -1
  39. data/lib/smart_core/operation/result_interface.rb +3 -3
  40. data/lib/smart_core/version.rb +1 -1
  41. data/smart_core.gemspec +6 -5
  42. metadata +28 -38
  43. data/lib/smart_core/container/command_definer.rb +0 -117
  44. data/lib/smart_core/container/commands.rb +0 -9
  45. data/lib/smart_core/container/commands/namespace.rb +0 -53
  46. data/lib/smart_core/container/dependency.rb +0 -44
  47. data/lib/smart_core/container/dependency_builder.rb +0 -48
  48. data/lib/smart_core/container/dependency_compatability/abstract.rb +0 -59
  49. data/lib/smart_core/container/dependency_compatability/command_set.rb +0 -35
  50. data/lib/smart_core/container/exceptions.rb +0 -31
  51. data/lib/smart_core/container/memoized_dependency.rb +0 -28
  52. data/lib/smart_core/container/namespace.rb +0 -51
@@ -1,56 +1,58 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  # @api private
4
- # @since 0.5.0
5
- class SmartCore::Container::CommandSet
6
- # @since 0.5.0
4
+ # @since 0.7.0
5
+ class SmartCore::Container::DefinitionDSL::CommandSet
6
+ # @since 0.7.0
7
7
  include Enumerable
8
8
 
9
- # @return [Array<SmartCore::Container::Commands::Base>]
9
+ # @return [Array<SmartCore::Container::DefinitionDSL::Commands::Base>]
10
10
  #
11
11
  # @api private
12
- # @since 0.5.0
12
+ # @since 0.7.0
13
13
  attr_reader :commands
14
14
 
15
+ # @return [void]
16
+ #
15
17
  # @api private
16
- # @since 0.5.0
18
+ # @since 0.7.0
17
19
  def initialize
18
20
  @commands = []
19
- @access_lock = Mutex.new
21
+ @access_lock = SmartCore::Container::ArbitaryLock.new
20
22
  end
21
23
 
22
- # @param command [SmartCore::Container::Commands::Base]
24
+ # @param [SmartCore::Container::DefinitionDSL::Commands::Base]
23
25
  # @return [void]
24
26
  #
25
27
  # @api private
26
- # @since 0.5.0
28
+ # @since 0.7.0
27
29
  def add_command(command)
28
30
  thread_safe { commands << command }
29
31
  end
30
32
  alias_method :<<, :add_command
31
33
 
32
- # @yield [SmartCore::Container::Commands::Base]
34
+ # @param block [Block]
33
35
  # @return [Enumerable]
34
36
  #
35
37
  # @api private
36
- # @since 0.5.0
38
+ # @since 0.7.0
37
39
  def each(&block)
38
40
  thread_safe { block_given? ? commands.each(&block) : commands.each }
39
41
  end
40
42
 
41
- # @param command_set [SmartCore::Container::CommandSet]
43
+ # @param command_set [SmartCore::Container::DefinitionDSL::CommandSet]
42
44
  # @return [void]
43
45
  #
44
46
  # @api private
45
- # @since 0.5.0
47
+ # @since 0.7.0
46
48
  def concat(command_set)
47
49
  thread_safe { commands.concat(command_set.dup.commands) }
48
50
  end
49
51
 
50
- # @return [SmartCore::Operation::AttributeSet]
52
+ # @return [SmartCore::Container::DefinitionDSL::CommandSet]
51
53
  #
52
54
  # @api private
53
- # @since 0.2.0
55
+ # @since 0.7.0
54
56
  def dup
55
57
  thread_safe do
56
58
  self.class.new.tap do |duplicate|
@@ -61,22 +63,14 @@ class SmartCore::Container::CommandSet
61
63
  end
62
64
  end
63
65
 
64
- # @return [void]
65
- #
66
- # @api private
67
- # @since 0.5.0
68
- def clear
69
- thread_safe { commands.clear }
70
- end
71
-
72
66
  private
73
67
 
74
- # @param block [Proc]
68
+ # @param block [Block]
75
69
  # @return [Any]
76
70
  #
77
71
  # @api private
78
- # @since 0.5.0
72
+ # @since 0.7.0
79
73
  def thread_safe(&block)
80
- @access_lock.synchronize(&block)
74
+ @access_lock.thread_safe(&block)
81
75
  end
82
76
  end
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ # @api private
4
+ # @since 0.7.0
5
+ module SmartCore::Container::DefinitionDSL::Commands
6
+ require_relative 'commands/base'
7
+ require_relative 'commands/definition/namespace'
8
+ require_relative 'commands/definition/register'
9
+ require_relative 'commands/definition/compose'
10
+ require_relative 'commands/instantiation/compose'
11
+ require_relative 'commands/instantiation/freeze_state'
12
+ end
@@ -1,12 +1,12 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  # @api private
4
- # @since 0.5.0
5
- class SmartCore::Container::Commands::Base
4
+ # @since 0.7.0
5
+ class SmartCore::Container::DefinitionDSL::Commands::Base
6
6
  # @param regsitry [SmartCore::Container::Registry]
7
7
  # @return [void]
8
8
  #
9
9
  # @api private
10
- # @since 0.5.0
10
+ # @since 0.7.0
11
11
  def call(registry); end
12
12
  end
@@ -0,0 +1,46 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SmartCore::Container::DefinitionDSL::Commands::Definition
4
+ # @api private
5
+ # @since 0.7.0
6
+ class Compose < SmartCore::Container::DefinitionDSL::Commands::Base
7
+ # @param container_klass [Class<SmartCore::Container>]
8
+ # @return [void]
9
+ #
10
+ # @api private
11
+ # @since 0.7.0
12
+ def initialize(container_klass)
13
+ raise(
14
+ SmartCore::ArgumentError,
15
+ 'Container class should be a subtype of Quantum::Container'
16
+ ) unless container_klass < SmartCore::Container
17
+
18
+ @container_klass = container_klass
19
+ end
20
+
21
+ # @param registry [SmartCore::Container::Registry]
22
+ # @return [void]
23
+ #
24
+ # @api private
25
+ # @since 0.7.0
26
+ def call(registry)
27
+ SmartCore::Container::RegistryBuilder.build_definitions(container_klass, registry)
28
+ end
29
+
30
+ # @return [SmartCore::Container::DefinitionDSL::Commands::Definition::Compose]
31
+ #
32
+ # @api private
33
+ # @since 0.7.0
34
+ def dup
35
+ self.class.new(container_klass)
36
+ end
37
+
38
+ private
39
+
40
+ # @return [Class<SmartCore::Container>]
41
+ #
42
+ # @api private
43
+ # @since 0.7.0
44
+ attr_reader :container_klass
45
+ end
46
+ end
@@ -0,0 +1,51 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SmartCore::Container::DefinitionDSL::Commands::Definition
4
+ # @api private
5
+ # @since 0.7.0
6
+ class Namespace < SmartCore::Container::DefinitionDSL::Commands::Base
7
+ # @return [String]
8
+ #
9
+ # @api private
10
+ # @since 0.7.0
11
+ attr_reader :namespace_name
12
+
13
+ # @param namespace_name [String, Symbol]
14
+ # @param dependencies_definition [Proc]
15
+ # @return [void]
16
+ #
17
+ # @api private
18
+ # @since 0.7.0
19
+ def initialize(namespace_name, dependencies_definition)
20
+ SmartCore::Container::KeyGuard.indifferently_accessable_key(namespace_name).tap do |name|
21
+ @namespace_name = name
22
+ @dependencies_definition = dependencies_definition
23
+ end
24
+ end
25
+
26
+ # @param registry [SmartCore::Container::Registry]
27
+ # @return [void]
28
+ #
29
+ # @api private
30
+ # @since 0.7.0
31
+ def call(registry)
32
+ registry.register_namespace(namespace_name, &dependencies_definition)
33
+ end
34
+
35
+ # @return [SmartCore::Container::DefinitionDSL::Commands::Definition::Namespace]
36
+ #
37
+ # @api private
38
+ # @since 0.7.0
39
+ def dup
40
+ self.class.new(namespace_name, dependencies_definition)
41
+ end
42
+
43
+ private
44
+
45
+ # @return [Proc]
46
+ #
47
+ # @api private
48
+ # @since 0.7.0
49
+ attr_reader :dependencies_definition
50
+ end
51
+ end
@@ -1,30 +1,24 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- module SmartCore::Container::Commands
3
+ module SmartCore::Container::DefinitionDSL::Commands::Definition
4
4
  # @api private
5
- # @since 0.5.0
6
- class Register < Base
7
- # @return [String, Symbol]
5
+ # @since 0.7.0
6
+ class Register < SmartCore::Container::DefinitionDSL::Commands::Base
7
+ # @return [String]
8
8
  #
9
9
  # @api private
10
- # @since 0.5.0
10
+ # @since 0.7.0
11
11
  attr_reader :dependency_name
12
12
 
13
13
  # @param dependency_name [String, Symbol]
14
- # @param options [Hash<Symbol,Any>]
15
14
  # @param dependency_definition [Proc]
16
- # @option memoize [Boolean]
17
15
  # @return [void]
18
16
  #
19
- # @todo option list
20
- # @see [SmartCore::Container::KeyGuard]
21
- #
22
17
  # @api private
23
- # @since 0.5.0
24
- def initialize(dependency_name, dependency_definition, **options)
18
+ # @since 0.7.0
19
+ def initialize(dependency_name, dependency_definition)
25
20
  SmartCore::Container::KeyGuard.indifferently_accessable_key(dependency_name).tap do |name|
26
21
  @dependency_name = name
27
- @options = options
28
22
  @dependency_definition = dependency_definition
29
23
  end
30
24
  end
@@ -33,17 +27,17 @@ module SmartCore::Container::Commands
33
27
  # @return [void]
34
28
  #
35
29
  # @api private
36
- # @since 0.5.0
30
+ # @since 0.7.0
37
31
  def call(registry)
38
- registry.register(dependency_name, **options, &dependency_definition)
32
+ registry.register_dependency(dependency_name, &dependency_definition)
39
33
  end
40
34
 
41
- # @return [SmartCore::Container::Commands::Register]
35
+ # @return [SmartCore::Container::DefinitionDSL::Commands::Definition::Register]
42
36
  #
43
37
  # @api private
44
- # @since 0.5.0
38
+ # @since 0.7.0
45
39
  def dup
46
- self.class.new(dependency_name, dependency_definition, **options)
40
+ self.class.new(dependency_name, dependency_definition)
47
41
  end
48
42
 
49
43
  private
@@ -51,13 +45,7 @@ module SmartCore::Container::Commands
51
45
  # @return [Proc]
52
46
  #
53
47
  # @api private
54
- # @since 0.5.0
48
+ # @since 0.7.0
55
49
  attr_reader :dependency_definition
56
-
57
- # @return [Hash<Symbol,Any>]
58
- #
59
- # @api private
60
- # @since 0.5.0
61
- attr_reader :options
62
50
  end
63
51
  end
@@ -0,0 +1,50 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SmartCore::Container::DefinitionDSL::Commands::Instantiation
4
+ # @api private
5
+ # @since 0.7.0
6
+ class Compose < SmartCore::Container::DefinitionDSL::Commands::Base
7
+ # @param container_klass [Class<SmartCore::Container>]
8
+ # @return [void]
9
+ #
10
+ # @api private
11
+ # @since 0.7.0
12
+ def initialize(container_klass)
13
+ raise(
14
+ SmartCore::ArgumentError,
15
+ 'Container class should be a subtype of Quantum::Container'
16
+ ) unless container_klass < SmartCore::Container
17
+
18
+ @container_klass = container_klass
19
+ end
20
+
21
+ # @param registry [SmartCore::Container::Registry]
22
+ # @return [void]
23
+ #
24
+ # @api private
25
+ # @since 0.7.0
26
+ def call(registry)
27
+ SmartCore::Container::RegistryBuilder.build_state(
28
+ container_klass, registry, ignored_commands: [
29
+ SmartCore::Container::DefinitionDSL::Commands::Instantiation::FreezeState
30
+ ]
31
+ )
32
+ end
33
+
34
+ # @return [SmartCore::Container::DefinitionDSL::Commands::Instantiation::Compose]
35
+ #
36
+ # @api private
37
+ # @since 0.7.0
38
+ def dup
39
+ self.class.new(container_klass)
40
+ end
41
+
42
+ private
43
+
44
+ # @return [Class<SmartCore::Container>]
45
+ #
46
+ # @api private
47
+ # @since 0.7.0
48
+ attr_reader :container_klass
49
+ end
50
+ end
@@ -0,0 +1,24 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SmartCore::Container::DefinitionDSL::Commands::Instantiation
4
+ # @api private
5
+ # @since 0.7.0
6
+ class FreezeState < SmartCore::Container::DefinitionDSL::Commands::Base
7
+ # @param registry [SmartCore::Container::Registry]
8
+ # @return [void]
9
+ #
10
+ # @api private
11
+ # @since 0.7.0
12
+ def call(registry)
13
+ registry.freeze!
14
+ end
15
+
16
+ # @return [SmartCore::Container::DefinitionDSL::Commands::Instantiation::FreezeState]
17
+ #
18
+ # @api private
19
+ # @since 0.7.0
20
+ def dup
21
+ self.class.new
22
+ end
23
+ end
24
+ end
@@ -1,9 +1,9 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  # @api private
4
- # @since 0.5.0
4
+ # @since 0.7.0
5
5
  module SmartCore::Container::DependencyCompatability
6
- require_relative 'dependency_compatability/abstract'
7
- require_relative 'dependency_compatability/command_set'
6
+ require_relative 'dependency_compatability/general'
7
+ require_relative 'dependency_compatability/definition'
8
8
  require_relative 'dependency_compatability/registry'
9
9
  end
@@ -0,0 +1,42 @@
1
+ # frozen_string_literal: true
2
+
3
+ # @api private
4
+ # @since 0.7.0
5
+ module SmartCore::Container::DependencyCompatability::Definition
6
+ class << self
7
+ # @since 0.7.0
8
+ include SmartCore::Container::DependencyCompatability::General
9
+
10
+ # @param container_klass [Class<SmartCore::Container>]
11
+ # @param dependency_name [String, Symbol]
12
+ # @return [Boolean]
13
+ #
14
+ # @api private
15
+ # @since 0.7.0
16
+ def potential_namespace_overlap?(container_klass, dependency_name)
17
+ begin
18
+ anonymous_container = Class.new(container_klass).new
19
+ anonymous_container.register(dependency_name, &(proc {}))
20
+ false
21
+ rescue SmartCore::Container::DependencyOverNamespaceOverlapError
22
+ true
23
+ end
24
+ end
25
+
26
+ # @param container_klass [Class<SmartCore::Container>]
27
+ # @param namespace_name [String, Symbol]
28
+ # @return [Boolean]
29
+ #
30
+ # @api private
31
+ # @since 0.7.0
32
+ def potential_dependency_overlap?(container_klass, namespace_name)
33
+ begin
34
+ anonymous_container = Class.new(container_klass).new
35
+ anonymous_container.namespace(namespace_name, &(proc {}))
36
+ false
37
+ rescue SmartCore::Container::NamespaceOverDependencyOverlapError
38
+ true
39
+ end
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,61 @@
1
+ # frozen_string_literal: true
2
+
3
+ # @api private
4
+ # @since 0.7.0
5
+ module SmartCore::Container::DependencyCompatability::General
6
+ # @param context [Class<SmartCore::Container>, SmartCore::Container::Registry]
7
+ # @param dependency_name [String, Symbol]
8
+ # @return [void]
9
+ #
10
+ # @raise [SmartCore::Container::DependencyOverNamespaceOverlapError]
11
+ #
12
+ # @api private
13
+ # @since 0.7.0
14
+ def prevent_namespace_overlap!(context, dependency_name)
15
+ raise(
16
+ SmartCore::Container::DependencyOverNamespaceOverlapError,
17
+ "Trying to overlap already registered '#{dependency_name}' namespace " \
18
+ "with '#{dependency_name}' dependency!"
19
+ ) if potential_namespace_overlap?(context, dependency_name)
20
+ end
21
+
22
+ # @param context [Class<SmartCore::Container>, SmartCore::Container::Registry]
23
+ # @param namespace_name [String, Symbol]
24
+ # @return [void]
25
+ #
26
+ # @raise [SmartCore::Container::NamespaceOverDependencyOverlapError]
27
+ #
28
+ # @api private
29
+ # @since 0.7.0
30
+ def prevent_dependency_overlap!(context, namespace_name)
31
+ raise(
32
+ SmartCore::Container::NamespaceOverDependencyOverlapError,
33
+ "Trying to overlap already registered '#{namespace_name}' dependency " \
34
+ "with '#{namespace_name}' namespace!"
35
+ ) if potential_dependency_overlap?(context, namespace_name)
36
+ end
37
+
38
+ # @param context [Class<SmartCore::Container>, SmartCore::Container::Registry]
39
+ # @param dependency_name [String, Symbol]
40
+ # @return [Boolean]
41
+ #
42
+ # @api private
43
+ # @since 0.7.0
44
+ def potential_namespace_overlap?(context, dependency_name)
45
+ # :nocov:
46
+ raise NoMethodError
47
+ # :nocov:
48
+ end
49
+
50
+ # @param context [Class<SmartCore::Container>, SmartCore::Container::Registry]
51
+ # @param namespace_name [String, Symbol]
52
+ # @return [Boolean]
53
+ #
54
+ # @api private
55
+ # @since 0.7.0
56
+ def potential_dependency_overlap?(context, namespace_name)
57
+ # :nocov:
58
+ raise NoMethodError
59
+ # :nocov:
60
+ end
61
+ end