smart_initializer 0.1.0.alpha4 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (60) hide show
  1. checksums.yaml +4 -4
  2. data/.rubocop.yml +1 -1
  3. data/.travis.yml +36 -5
  4. data/CHANGELOG.md +3 -0
  5. data/Gemfile.lock +48 -31
  6. data/README.md +151 -22
  7. data/Rakefile +1 -1
  8. data/bin/rspec +54 -0
  9. data/gemfiles/with_external_deps.gemfile +7 -0
  10. data/gemfiles/with_external_deps.gemfile.lock +99 -0
  11. data/gemfiles/without_external_deps.gemfile +5 -0
  12. data/gemfiles/without_external_deps.gemfile.lock +97 -0
  13. data/lib/smart_core/initializer.rb +38 -35
  14. data/lib/smart_core/initializer/attribute.rb +12 -4
  15. data/lib/smart_core/initializer/attribute/factory.rb +47 -27
  16. data/lib/smart_core/initializer/attribute/parameters.rb +12 -4
  17. data/lib/smart_core/initializer/configurable_module.rb +27 -0
  18. data/lib/smart_core/initializer/configuration.rb +33 -0
  19. data/lib/smart_core/initializer/constructor.rb +9 -6
  20. data/lib/smart_core/initializer/constructor/definer.rb +34 -11
  21. data/lib/smart_core/initializer/dsl.rb +23 -6
  22. data/lib/smart_core/initializer/errors.rb +44 -0
  23. data/lib/smart_core/initializer/plugins.rb +17 -0
  24. data/lib/smart_core/initializer/plugins/abstract.rb +55 -0
  25. data/lib/smart_core/initializer/plugins/access_mixin.rb +47 -0
  26. data/lib/smart_core/initializer/plugins/registry.rb +166 -0
  27. data/lib/smart_core/initializer/plugins/registry_interface.rb +77 -0
  28. data/lib/smart_core/initializer/plugins/thy_types.rb +30 -0
  29. data/lib/smart_core/initializer/plugins/thy_types/errors.rb +11 -0
  30. data/lib/smart_core/initializer/plugins/thy_types/thy_types.rb +23 -0
  31. data/lib/smart_core/initializer/plugins/thy_types/thy_types/abstract_factory.rb +78 -0
  32. data/lib/smart_core/initializer/plugins/thy_types/thy_types/operation.rb +12 -0
  33. data/lib/smart_core/initializer/plugins/thy_types/thy_types/operation/base.rb +9 -0
  34. data/lib/smart_core/initializer/plugins/thy_types/thy_types/operation/cast.rb +21 -0
  35. data/lib/smart_core/initializer/plugins/thy_types/thy_types/operation/valid.rb +16 -0
  36. data/lib/smart_core/initializer/plugins/thy_types/thy_types/operation/validate.rb +19 -0
  37. data/lib/smart_core/initializer/settings.rb +49 -0
  38. data/lib/smart_core/initializer/settings/duplicator.rb +20 -0
  39. data/lib/smart_core/initializer/settings/type_system.rb +69 -0
  40. data/lib/smart_core/initializer/type_system.rb +16 -0
  41. data/lib/smart_core/initializer/type_system/interop.rb +103 -0
  42. data/lib/smart_core/initializer/type_system/interop/abstract_factory.rb +70 -0
  43. data/lib/smart_core/initializer/type_system/interop/aliasing.rb +72 -0
  44. data/lib/smart_core/initializer/type_system/interop/aliasing/alias_list.rb +141 -0
  45. data/lib/smart_core/initializer/type_system/interop/operation.rb +30 -0
  46. data/lib/smart_core/initializer/type_system/registry.rb +174 -0
  47. data/lib/smart_core/initializer/type_system/registry_interface.rb +102 -0
  48. data/lib/smart_core/initializer/type_system/smart_types.rb +48 -0
  49. data/lib/smart_core/initializer/type_system/smart_types/abstract_factory.rb +72 -0
  50. data/lib/smart_core/initializer/type_system/smart_types/operation.rb +13 -0
  51. data/lib/smart_core/initializer/type_system/smart_types/operation/base.rb +8 -0
  52. data/lib/smart_core/initializer/type_system/smart_types/operation/cast.rb +16 -0
  53. data/lib/smart_core/initializer/type_system/smart_types/operation/valid.rb +16 -0
  54. data/lib/smart_core/initializer/type_system/smart_types/operation/validate.rb +16 -0
  55. data/lib/smart_core/initializer/version.rb +1 -1
  56. data/smart_initializer.gemspec +13 -9
  57. metadata +67 -17
  58. data/lib/smart_core/initializer/attribute/init_extension.rb +0 -4
  59. data/lib/smart_core/initializer/type_aliasing.rb +0 -50
  60. data/lib/smart_core/initializer/type_aliasing/alias_list.rb +0 -101
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SmartCore::Initializer::TypeSystem
4
+ # @abstract
5
+ # @api private
6
+ # @since 0.1.0
7
+ class ThyTypes::Operation::Base < Interop::Operation
8
+ end
9
+ end
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SmartCore::Initializer::TypeSystem::ThyTypes::Operation
4
+ # @api private
5
+ # @since 0.1.0
6
+ class Cast < Base
7
+ # @param value [Any]
8
+ # @return [void]
9
+ #
10
+ # @raise [SmartCore::Initializer::TypeCastingUnsupportedError]
11
+ #
12
+ # @api private
13
+ # @since 0.1.0
14
+ def call(value)
15
+ raise(
16
+ SmartCore::Initializer::TypeCastingUnsupportedError,
17
+ 'ThyTypes type system has no support for type casting.'
18
+ )
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SmartCore::Initializer::TypeSystem::ThyTypes::Operation
4
+ # @api private
5
+ # @since 0.1.0
6
+ class Valid < Base
7
+ # @param value [Any]
8
+ # @return [Boolean]
9
+ #
10
+ # @api private
11
+ # @since 0.1.0
12
+ def call(value)
13
+ type.check(value).success?
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SmartCore::Initializer::TypeSystem::ThyTypes::Operation
4
+ # @api private
5
+ # @since 0.1.0
6
+ class Validate < Base
7
+ # @param value [Any]
8
+ # @return [void]
9
+ #
10
+ # @api private
11
+ # @since 0.1.0
12
+ def call(value)
13
+ raise(
14
+ SmartCore::Initializer::ThyTypeValidationError,
15
+ "Thy::Types validation error: (get #{value.inspect} for type #{type.inspect}"
16
+ ) unless type.check(value).success?
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,49 @@
1
+ # frozen_string_literal: true
2
+
3
+ # @api private
4
+ # @since 0.1.0
5
+ class SmartCore::Initializer::Settings
6
+ require_relative 'settings/type_system'
7
+ require_relative 'settings/duplicator'
8
+
9
+ # @return [void]
10
+ #
11
+ # @api private
12
+ # @since 0.1.0
13
+ def initialize
14
+ @type_system = TypeSystem.new
15
+ end
16
+
17
+ # @return [Any]
18
+ #
19
+ # @api private
20
+ # @since 0.1.0
21
+ def generic_type_object
22
+ @type_system.generic_type_object
23
+ end
24
+
25
+ # @return [Symbol]
26
+ #
27
+ # @api private
28
+ # @since 0.1.0
29
+ def type_system
30
+ @type_system.resolve
31
+ end
32
+
33
+ # @param value [String, System]
34
+ # @return [void]
35
+ #
36
+ # @api private
37
+ # @since 0.1.0
38
+ def type_system=(value)
39
+ @type_system.assign(value)
40
+ end
41
+
42
+ # @return [SmartCore::Initializer::Settings]
43
+ #
44
+ # @api private
45
+ # @since 0.1.0
46
+ def dup
47
+ Duplicator.duplicate(self)
48
+ end
49
+ end
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+
3
+ # @api private
4
+ # @since 0.1.0
5
+ module SmartCore::Initializer::Settings::Duplicator
6
+ class << self
7
+ # @param settings [SmartCore::Initializer::Settings]
8
+ # @return [SmartCore::Initializer::Settings]
9
+ #
10
+ # @api private
11
+ # @since 0.1.0
12
+ def duplicate(settings)
13
+ SmartCore::Initializer::Settings.new.tap do |new_instance|
14
+ type_system = settings.instance_variable_get(:@type_system).dup
15
+
16
+ new_instance.instance_variable_set(:@type_system, type_system)
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,69 @@
1
+ # frozen_string_literal: true
2
+
3
+ # @api private
4
+ # @since 0.1.0
5
+ class SmartCore::Initializer::Settings::TypeSystem
6
+ # @return [void]
7
+ #
8
+ # @api private
9
+ # @since 0.1.0
10
+ def initialize
11
+ @type_system = nil
12
+ @lock = SmartCore::Engine::Lock.new
13
+ end
14
+
15
+ # @return [Any]
16
+ #
17
+ # @api private
18
+ # @since 0.1.0
19
+ def generic_type_object
20
+ thread_safe do
21
+ SmartCore::Initializer::TypeSystem.resolve(resolve).generic_type_object
22
+ end
23
+ end
24
+
25
+ # @return [Symbol]
26
+ #
27
+ # @api private
28
+ # @since 0.1.0
29
+ def resolve
30
+ thread_safe do
31
+ @type_system || SmartCore::Initializer::Configuration.config[:default_type_system]
32
+ end
33
+ end
34
+
35
+ # @param value [String, Symbol]
36
+ # @return [void]
37
+ #
38
+ # @api private
39
+ # @since 0.1.0
40
+ def assign(value)
41
+ thread_safe do
42
+ SmartCore::Initializer::TypeSystem.resolve(value) # NOTE: type system existence validation
43
+ @type_system = value
44
+ end
45
+ end
46
+
47
+ # @return [SmartCore::Initializer::Settings::TypeSystem]
48
+ #
49
+ # @api private
50
+ # @since 0.1.0
51
+ def dup
52
+ thread_safe do
53
+ self.class.new.tap do |duplicate|
54
+ duplicate.instance_variable_set(:@type_system, @type_system)
55
+ end
56
+ end
57
+ end
58
+
59
+ private
60
+
61
+ # @param block [Block]
62
+ # @return [Any]
63
+ #
64
+ # @api private
65
+ # @since 0.1.0
66
+ def thread_safe(&block)
67
+ @lock.synchronize(&block)
68
+ end
69
+ end
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ # @api public
4
+ # @since 0.1.0
5
+ module SmartCore::Initializer::TypeSystem
6
+ require_relative 'type_system/interop'
7
+ require_relative 'type_system/registry'
8
+ require_relative 'type_system/smart_types'
9
+ require_relative 'type_system/registry_interface'
10
+
11
+ # @since 0.1.0
12
+ extend SmartCore::Initializer::TypeSystem::RegistryInterface
13
+
14
+ # @since 0.1.0
15
+ register(:smart_types, SmartCore::Initializer::TypeSystem::SmartTypes)
16
+ end
@@ -0,0 +1,103 @@
1
+ # frozen_string_literal: true
2
+
3
+ # @abstract
4
+ # @api private
5
+ # @since 0.1.0
6
+ class SmartCore::Initializer::TypeSystem::Interop
7
+ require_relative 'interop/operation'
8
+ require_relative 'interop/abstract_factory'
9
+ require_relative 'interop/aliasing'
10
+
11
+ # @since 0.1.0
12
+ include SmartCore::Initializer::TypeSystem::Interop::Aliasing
13
+
14
+ class << self
15
+ # @param type_object [Any]
16
+ # @return [SmartCore::Initializer::TypeSystem::Interop]
17
+ #
18
+ # @api private
19
+ # @since 0.1.0
20
+ def create(type_object)
21
+ self::AbstractFactory.create(type_object)
22
+ end
23
+
24
+ # @return [SmartCore::Initialiezr::TypeSystem::Interop]
25
+ #
26
+ # @api private
27
+ # @since 0.1.0
28
+ def generic_type_object
29
+ self::AbstractFactory.generic_type_object
30
+ end
31
+
32
+ # @param type_object [Any]
33
+ # @return [void]
34
+ #
35
+ # @raise [SmartCore::Initializer::IncorrectTypeObjectError]
36
+ #
37
+ # @api private
38
+ # @since 0.1.0
39
+ def prevent_incompatible_type!(type_object)
40
+ self::AbstractFactory.prevent_incompatible_type!(type_object)
41
+ end
42
+ end
43
+
44
+ # @param valid_op [SmartCore::Initializer::TypeSystem::Interop::Operation]
45
+ # @param validate_op [SmartCore::Initializer::TypeSystem::Interop::Operation]
46
+ # @param cast_op [SmartCore::Initializer::TypeSystem::Interop::Operation]
47
+ # @return [void]
48
+ #
49
+ # @api private
50
+ # @since 0.1.0
51
+ def initialize(valid_op, validate_op, cast_op)
52
+ @valid_op = valid_op
53
+ @validate_op = validate_op
54
+ @cast_op = cast_op
55
+ end
56
+
57
+ # @param value [Any]
58
+ # @return [Boolean]
59
+ #
60
+ # @api private
61
+ # @since 0.1.0
62
+ def valid?(value)
63
+ valid_op.call(value)
64
+ end
65
+
66
+ # @param value [Any]
67
+ # @return [void]
68
+ #
69
+ # @api private
70
+ # @since 0.1.0
71
+ def validate!(value)
72
+ validate_op.call(value)
73
+ end
74
+
75
+ # @param value [Any]
76
+ # @return [Any]
77
+ #
78
+ # @api private
79
+ # @since 0.1.0
80
+ def cast(value)
81
+ cast_op.call(value)
82
+ end
83
+
84
+ private
85
+
86
+ # @return [SmartCore::Initializer::TypeSystem::Interop::Operation]
87
+ #
88
+ # @api private
89
+ # @since 0.1.0
90
+ attr_reader :valid_op
91
+
92
+ # @return [SmartCore::Initializer::TypeSystem::Interop::Operation]
93
+ #
94
+ # @api private
95
+ # @since 0.1.0
96
+ attr_reader :validate_op
97
+
98
+ # @return [SmartCore::Initializer::TypeSystem::Interop::Operation]
99
+ #
100
+ # @api private
101
+ # @since 0.1.0
102
+ attr_reader :cast_op
103
+ end
@@ -0,0 +1,70 @@
1
+ # frozen_string_literal: true
2
+
3
+ # @abstract
4
+ # @api private
5
+ # @since 0.1.0
6
+ class SmartCore::Initializer::TypeSystem::Interop::AbstractFactory
7
+ class << self
8
+ # @param type [Any]
9
+ # @return [SmartCore::Initializer::TypeSystem::Interop]
10
+ #
11
+ # @api private
12
+ # @since 0.1.0
13
+ def create(type)
14
+ prevent_incompatible_type!(type)
15
+
16
+ valid_op = build_valid_operation(type)
17
+ validate_op = build_validate_operation(type)
18
+ cast_op = build_cast_operation(type)
19
+
20
+ build_interop(valid_op, validate_op, cast_op)
21
+ end
22
+
23
+ # @return [Any]
24
+ #
25
+ # @api private
26
+ # @since 0.1.0
27
+ def generic_type_object; end
28
+
29
+ # @param type [Any]
30
+ # @return [void]
31
+ #
32
+ # @raise [SmartCore::Initializer::IncorrectTypeObjectError]
33
+ #
34
+ # @api private
35
+ # @since 0.1.0
36
+ def prevent_incompatible_type!(type); end
37
+
38
+ private
39
+
40
+ # @param type [Any]
41
+ # @return [SmartCore::Initializer::TypeSystem::Interop::Operation]
42
+ #
43
+ # @api private
44
+ # @since 0.1.0
45
+ def build_valid_operation(type); end
46
+
47
+ # @param type [Any]
48
+ # @return [SmartCore::Initializer::TypeSystem::Interop::Operation]
49
+ #
50
+ # @api private
51
+ # @since 0.1.0
52
+ def build_validate_operation(type); end
53
+
54
+ # @param type [Any]
55
+ # @return [SmartCore::Initializer::TypeSystem::Interop::Operation]
56
+ #
57
+ # @api private
58
+ # @since 0.1.0
59
+ def build_cast_operation(type); end
60
+
61
+ # @param valid_op [SmartCore::Initializer::TypeSystem::Interop::Operation]
62
+ # @param validate_op [SmartCore::Initializer::TypeSystem::Interop::Operation]
63
+ # @param cast_op [SmartCore::Initializer::TypeSystem::Interop::Operation]
64
+ # @return [SmartCore::Initializer::TypeSystem::Interop]
65
+ #
66
+ # @api private
67
+ # @since 0.1.0
68
+ def build_interop(valid_op, validate_op, cast_op); end
69
+ end
70
+ end
@@ -0,0 +1,72 @@
1
+ # frozen_string_literal: true
2
+
3
+ # @api private
4
+ # @since 0.1.0
5
+ module SmartCore::Initializer::TypeSystem::Interop::Aliasing
6
+ require_relative 'aliasing/alias_list'
7
+
8
+ class << self
9
+ # @param base_klass [Class]
10
+ # @return [void]
11
+ #
12
+ # @api private
13
+ # @since 0.1.0
14
+ def included(base_klass)
15
+ base_klass.extend(ClassMethods)
16
+ base_klass.singleton_class.prepend(ClassInheritance)
17
+ end
18
+ end
19
+
20
+ # @api private
21
+ # @since 0.1.0
22
+ module ClassInheritance
23
+ # @param child_klass [Class<SmartCore::Initializer::TypeSystem::Interop>]
24
+ # @return [void]
25
+ #
26
+ # @api private
27
+ # @since 0.1.0
28
+ def inherited(child_klass)
29
+ child_klass.instance_variable_set(:@__type_aliases__, AliasList.new(child_klass))
30
+ super
31
+ end
32
+ end
33
+
34
+ # @api private
35
+ # @since 0.1.0
36
+ module ClassMethods
37
+ # @return [SmartCore::Initializer::TypeSystem::Interop::Aliasing::AliasList]
38
+ #
39
+ # @api private
40
+ # @since 0.1.0
41
+ def __type_aliases__
42
+ @__type_aliases__
43
+ end
44
+
45
+ # @return [Array<String>]
46
+ #
47
+ # @api public
48
+ # @since 0.1.0
49
+ def type_aliases
50
+ __type_aliases__.keys
51
+ end
52
+
53
+ # @param alias_name [String, Symbol]
54
+ # @param type [Any]
55
+ # @return [void]
56
+ #
57
+ # @api public
58
+ # @since 0.1.0
59
+ def type_alias(alias_name, type)
60
+ __type_aliases__.associate(alias_name, type)
61
+ end
62
+
63
+ # @param alias_name [String, Symbol]
64
+ # @return [Any]
65
+ #
66
+ # @api public
67
+ # @since 0.1.0
68
+ def type_from_alias(alias_name)
69
+ __type_aliases__.resolve(alias_name)
70
+ end
71
+ end
72
+ end