smart_ioc 0.4.0 → 0.5.1

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: ec0b7417468b3bf40350a297a885266b3067809f6ca074326614b8663ca762bb
4
- data.tar.gz: 6508ed9fdee9191479459541718c63172bd7a9d47fa1de63bdd9ab883737927b
3
+ metadata.gz: 5e5c21e82ade291e0f2349e6d75807764104472899d1369275b8e43be1184f82
4
+ data.tar.gz: 233217c7f8049bfaf40359cc599fc6f944f4a8e773da8a2b2c6059224dd283e4
5
5
  SHA512:
6
- metadata.gz: 2daa5061878b44419bbb0d3e4e2c5c7edcc526259b27e04f1600a3b2c994e06a03722c160e6b6c01a67a587cb99efa2dd385bbc215f275989ec0e622d78eb9a7
7
- data.tar.gz: fb7fbdc1dc05f2462f5aaf399564423798129e9e6ed324dceaa8e47747707083c40857f6528047c0d703b2cad45889900cf60924e33d339e775ef31215c97f66
6
+ metadata.gz: 49f4a74095dc772d17f3e5f2b9f749465c6b58f9fbb60309f1a4a772ecdcd4211200879f4ca7bc169e3b40c5695519273b7b729bfab4683f1f96d920fe41e505
7
+ data.tar.gz: 3aafd25fb01683303a404692d92cf8becc533c1840ba1672eaac6329e0360a59bf44d1820eaab0517ef8c1f1326160f0e2ec000efb1b9933f48de8945ccf8eed
@@ -0,0 +1,34 @@
1
+ # This workflow uses actions that are not certified by GitHub.
2
+ # They are provided by a third-party and are governed by
3
+ # separate terms of service, privacy policy, and support
4
+ # documentation.
5
+ # This workflow will download a prebuilt Ruby version, install dependencies and run tests with Rake
6
+ # For more information see: https://github.com/marketplace/actions/setup-ruby-jruby-and-truffleruby
7
+
8
+ name: Rspec
9
+
10
+ on:
11
+ push:
12
+ branches: [ master ]
13
+ pull_request:
14
+ branches: [ master ]
15
+
16
+ jobs:
17
+ test:
18
+
19
+ runs-on: ubuntu-latest
20
+ strategy:
21
+ matrix:
22
+ ruby-version: ['3.1', '3.3']
23
+
24
+ steps:
25
+ - uses: actions/checkout@v2
26
+ - name: Set up Ruby
27
+ # To automatically get bug fixes and new Ruby versions for ruby/setup-ruby,
28
+ # change this to (see https://github.com/ruby/setup-ruby#versioning):
29
+ uses: ruby/setup-ruby@v1
30
+ with:
31
+ ruby-version: ${{ matrix.ruby-version }}
32
+ bundler-cache: true # runs 'bundle install' and caches installed gems automatically
33
+ - name: Run tests
34
+ run: bundle exec rspec spec/
data/.ruby-version CHANGED
@@ -1 +1 @@
1
- 3.1.1
1
+ 3.2.2
data/Gemfile CHANGED
@@ -6,5 +6,6 @@ gemspec
6
6
  group :test do
7
7
  gem 'rspec'
8
8
  gem 'simplecov'
9
+ gem 'simplecov-html', '0.12.3'
9
10
  gem 'byebug'
10
11
  end
data/Gemfile.lock CHANGED
@@ -1,31 +1,34 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- smart_ioc (0.4.0)
4
+ smart_ioc (0.5.1)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
8
8
  specs:
9
9
  byebug (11.1.3)
10
- codecov (0.6.0)
11
- simplecov (>= 0.15, < 0.22)
12
- diff-lcs (1.5.0)
13
- docile (1.4.0)
14
- rake (13.0.6)
15
- rspec (3.11.0)
16
- rspec-core (~> 3.11.0)
17
- rspec-expectations (~> 3.11.0)
18
- rspec-mocks (~> 3.11.0)
19
- rspec-core (3.11.0)
20
- rspec-support (~> 3.11.0)
21
- rspec-expectations (3.11.0)
10
+ coderay (1.1.3)
11
+ diff-lcs (1.5.1)
12
+ docile (1.4.1)
13
+ method_source (1.1.0)
14
+ pry (0.14.2)
15
+ coderay (~> 1.1)
16
+ method_source (~> 1.0)
17
+ rake (13.2.1)
18
+ rspec (3.13.0)
19
+ rspec-core (~> 3.13.0)
20
+ rspec-expectations (~> 3.13.0)
21
+ rspec-mocks (~> 3.13.0)
22
+ rspec-core (3.13.0)
23
+ rspec-support (~> 3.13.0)
24
+ rspec-expectations (3.13.2)
22
25
  diff-lcs (>= 1.2.0, < 2.0)
23
- rspec-support (~> 3.11.0)
24
- rspec-mocks (3.11.0)
26
+ rspec-support (~> 3.13.0)
27
+ rspec-mocks (3.13.1)
25
28
  diff-lcs (>= 1.2.0, < 2.0)
26
- rspec-support (~> 3.11.0)
27
- rspec-support (3.11.0)
28
- simplecov (0.21.2)
29
+ rspec-support (~> 3.13.0)
30
+ rspec-support (3.13.1)
31
+ simplecov (0.22.0)
29
32
  docile (~> 1.1)
30
33
  simplecov-html (~> 0.11)
31
34
  simplecov_json_formatter (~> 0.1)
@@ -34,15 +37,17 @@ GEM
34
37
 
35
38
  PLATFORMS
36
39
  ruby
40
+ x86_64-darwin-23
37
41
 
38
42
  DEPENDENCIES
39
43
  bundler
40
44
  byebug
41
- codecov
45
+ pry
42
46
  rake
43
47
  rspec
44
48
  simplecov
49
+ simplecov-html (= 0.12.3)
45
50
  smart_ioc!
46
51
 
47
52
  BUNDLED WITH
48
- 2.1.4
53
+ 2.5.14
data/README.md CHANGED
@@ -1,8 +1,6 @@
1
1
  # SmartIoC
2
- [![Build Status](https://travis-ci.org/ddd-ruby/smart_ioc.png)](https://travis-ci.org/ddd-ruby/smart_ioc)
3
- [![Code Climate](https://codeclimate.com/github/ddd-ruby/smart_ioc/badges/gpa.svg)](https://codeclimate.com/github/ddd-ruby/smart_ioc)
4
- [![codecov](https://codecov.io/gh/ddd-ruby/smart_ioc/branch/master/graph/badge.svg)](https://codecov.io/gh/ddd-ruby/smart_ioc)
5
- [![Dependency Status](https://gemnasium.com/ddd-ruby/smart_ioc.png)](https://gemnasium.com/ddd-ruby/smart_ioc)
2
+ [![Rspec](https://github.com/droidlabs/smart_ioc/workflows/Rspec/badge.svg)](https://github.com/droidlabs/smart_ioc/actions?query=workflow%3ARspec)
3
+ [![Code Climate](https://codeclimate.com/github/ddd-ruby/smart_ioc/badges/gpa.svg)](https://codeclimate.com/github/ddd-ruby/smart_ioc)
6
4
 
7
5
 
8
6
  SmartIoC is a smart and really simple IoC container for Ruby applications.
@@ -10,6 +8,15 @@ SmartIoC is a smart and really simple IoC container for Ruby applications.
10
8
  ## Installation
11
9
  `gem install smart_ioc`
12
10
 
11
+ ## Ruby versions
12
+
13
+ Please install specific smart_ioc version, depending on Ruby version.
14
+
15
+ | Ruby Version | SmartIoC Version |
16
+ | ------------ | ------------ |
17
+ | < 3.0 | 0.3.2 |
18
+ | >= 3.0 | 0.4.0 |
19
+
13
20
  ## Setup
14
21
  Set package name and source package folder with beans. SmartIoC will parse source files and detect bean definitions automatically for you.
15
22
 
data/docs/index.md CHANGED
@@ -1,9 +1,6 @@
1
1
  # SmartIoC
2
- [![Build Status](https://travis-ci.org/ddd-ruby/smart_ioc.png)](https://travis-ci.org/ddd-ruby/smart_ioc)
3
- [![Code Climate](https://codeclimate.com/github/ddd-ruby/smart_ioc/badges/gpa.svg)](https://codeclimate.com/github/ddd-ruby/smart_ioc)
4
- [![codecov](https://codecov.io/gh/ddd-ruby/smart_ioc/branch/master/graph/badge.svg)](https://codecov.io/gh/ddd-ruby/smart_ioc)
5
- [![Dependency Status](https://gemnasium.com/ddd-ruby/smart_ioc.png)](https://gemnasium.com/ddd-ruby/smart_ioc)
6
-
2
+ [![Rspec](https://github.com/droidlabs/smart_ioc/workflows/Rspec/badge.svg)](https://github.com/droidlabs/smart_ioc/actions?query=workflow%3ARspec)
3
+ [![Code Climate](https://codeclimate.com/github/ddd-ruby/smart_ioc/badges/gpa.svg)](https://codeclimate.com/github/ddd-ruby/smart_ioc)
7
4
 
8
5
  SmartIoC is a smart and really simple IoC container for Ruby applications.
9
6
 
@@ -0,0 +1,59 @@
1
+ def bean(bean_name, &proc)
2
+ raise ArgumentError, "name should be a Symbol" if !bean_name.is_a?(Symbol)
3
+ raise ArgumentError, "proc should be provided" if !block_given?
4
+
5
+ klass = Class.new do
6
+ include SmartIoC::Iocify
7
+ end
8
+
9
+ klass.instance_variable_set(:@anonymous_bean, true)
10
+ klass.instance_exec(&proc)
11
+
12
+ str = SmartIoC::StringUtils
13
+ file_path = caller[0].split(':').first
14
+ package = klass.instance_variable_get(:@package) || SmartIoC::BeanLocations.get_bean_package(file_path)
15
+ context = klass.instance_variable_get(:@context) || :default
16
+
17
+ if package.nil?
18
+ raise ArgumentError, "package is not defined for bean :#{bean_name}"
19
+ end
20
+
21
+ package_mod = str.camelize(package)
22
+ context_mod = str.camelize(context || :default)
23
+
24
+ class_name = str.camelize(bean_name)
25
+ klass_name = "#{package_mod}::#{context_mod}::#{class_name}"
26
+
27
+ eval(
28
+ %Q(
29
+ module #{package_mod}
30
+ module #{context_mod}
31
+ if constants.include?(:"#{class_name}")
32
+ remove_const :"#{class_name}"
33
+ end
34
+ end
35
+ end
36
+
37
+ #{klass_name} = klass
38
+ )
39
+ )
40
+
41
+ klass.instance_exec do
42
+ bean(
43
+ bean_name,
44
+ file_path: file_path,
45
+ scope: instance_variable_get(:@scope),
46
+ package: package,
47
+ instance: instance_variable_get(:@instance) || false,
48
+ factory_method: instance_variable_get(:@factory_method),
49
+ context: context,
50
+ after_init: instance_variable_get(:@after_init),
51
+ )
52
+ end
53
+
54
+ (klass.instance_variable_get(:@injects) || []).each do |inject|
55
+ klass.register_inject(inject[:bean_name], ref: inject[:ref], from: inject[:from])
56
+ end
57
+
58
+ klass
59
+ end
@@ -47,7 +47,7 @@ class SmartIoC::BeanDefinition
47
47
  end
48
48
 
49
49
  def ==(bean_definition)
50
- bean_definition.klass == @klass
50
+ bean_definition.name == @name && bean_definition.package == @package && bean_definition.context == @context
51
51
  end
52
52
 
53
53
  def singleton?
@@ -56,7 +56,6 @@ class SmartIoC::BeanDefinition
56
56
 
57
57
  def inspect
58
58
  str = []
59
- str << "class: #{@klass}"
60
59
  str << "name: :#{@name}"
61
60
  str << "package: :#{@package}"
62
61
  str << "context: :#{@context}"
@@ -65,4 +64,17 @@ class SmartIoC::BeanDefinition
65
64
  str << "factory_method: #{@factory_method}"
66
65
  str.join("\n")
67
66
  end
67
+
68
+ def preload
69
+ @dependencies.each do |dep|
70
+ bd = SmartIoC.get_bean_definition(
71
+ dep.ref,
72
+ package: dep.package,
73
+ parent_bean_definition: self,
74
+ parent_bean_name: name,
75
+ )
76
+
77
+ bd.preload
78
+ end
79
+ end
68
80
  end
@@ -20,15 +20,18 @@ class SmartIoC::BeanDefinitionsStorage
20
20
  end
21
21
 
22
22
  if existing_bd
23
- error_msg =
24
- %Q(Not able to add bean to definitions storage.
25
- Bean definition already exists.
26
- New bean details:
27
- #{bean_definition.inspect}
28
- Existing bean details:
29
- #{existing_bd.inspect})
30
-
31
- raise ArgumentError, error_msg
23
+ @collection.reject! { |bd| bd == existing_bd }
24
+
25
+ message = <<~EOF
26
+ \nReplacing bean definition...
27
+ - New bean details:
28
+ #{bean_definition.inspect}
29
+ - Existing bean details:
30
+ #{existing_bd.inspect})
31
+
32
+ EOF
33
+
34
+ puts message
32
35
  end
33
36
 
34
37
  @collection.push(bean_definition)
@@ -43,11 +46,13 @@ Existing bean details:
43
46
  end
44
47
  end
45
48
 
46
- # @param klass [Class] bean class
49
+ # Returns bean definition for specific class
50
+ # @param bean_name [Symbol]
51
+ # @param package [Symbol]
52
+ # @param context [Symbol]
47
53
  # @return bean definition [BeanDefinition] or nil
48
- def find_by_class(klass)
49
- klass_str = klass.to_s
50
- @collection.detect {|bd| bd.klass.to_s == klass_str}
54
+ def find_bean(bean_name, package, context)
55
+ @collection.detect {|bd| bd.name == bean_name && bd.package == package && bd.context == context}
51
56
  end
52
57
 
53
58
  def filter_by(bean_name, package = nil, context = nil)
@@ -31,17 +31,8 @@ class SmartIoC::BeanFactory
31
31
  # @return bean instance
32
32
  # @raise [ArgumentError] if bean is not found
33
33
  # @raise [ArgumentError] if ambiguous bean definition was found
34
- def get_bean(bean_name, package: nil, parent_bean_definition: nil, context: nil)
35
- check_arg(bean_name, :bean_name, Symbol)
36
- check_arg(package, :package, Symbol) if package
37
- check_arg(parent_bean_definition, :parent_bean_definition, SmartIoC::BeanDefinition) if parent_bean_definition
38
- check_arg(context, :context, Symbol) if context
39
-
40
- @bean_file_loader.require_bean(bean_name)
41
-
42
- parent_package_name = parent_bean_definition ? parent_bean_definition.package : nil
43
- context = autodetect_context(bean_name, package, parent_package_name, context)
44
- bean_definition = @bean_definitions_storage.find(bean_name, package, context, parent_package_name)
34
+ def get_bean(bean_name, package: nil, parent_bean_definition: nil, context: nil, parent_bean_name: nil)
35
+ bean_definition = get_bean_definition(bean_name, package:, parent_bean_definition:, context:, parent_bean_name:)
45
36
  scope = get_scope(bean_definition)
46
37
  bean = scope.get_bean(bean_definition.klass)
47
38
 
@@ -56,6 +47,19 @@ class SmartIoC::BeanFactory
56
47
  raise e
57
48
  end
58
49
 
50
+ def get_bean_definition(bean_name, package: nil, context: nil, parent_bean_definition: nil, parent_bean_name: nil)
51
+ check_arg(bean_name, :bean_name, Symbol)
52
+ check_arg(package, :package, Symbol) if package
53
+ check_arg(parent_bean_definition, :parent_bean_definition, SmartIoC::BeanDefinition) if parent_bean_definition
54
+ check_arg(context, :context, Symbol) if context
55
+
56
+ @bean_file_loader.require_bean(bean_name)
57
+
58
+ parent_package_name = parent_bean_definition ? parent_bean_definition.package : nil
59
+ context = autodetect_context(bean_name, package, parent_package_name, context, parent_bean_name)
60
+ bean_definition = @bean_definitions_storage.find(bean_name, package, context, parent_package_name)
61
+ end
62
+
59
63
  private
60
64
 
61
65
  def init_bean(bean_definition)
@@ -76,18 +80,18 @@ class SmartIoC::BeanFactory
76
80
  bean
77
81
  end
78
82
 
79
- def autodetect_context(bean_name, package, parent_bean_package, context)
83
+ def autodetect_context(bean_name, package, parent_bean_package, context, parent_bean_name)
80
84
  return context if context
81
85
 
82
86
  if package
83
87
  @extra_package_contexts.get_context(package)
84
88
  else
85
- bean_definition = autodetect_bean_definition(bean_name, package, parent_bean_package)
89
+ bean_definition = autodetect_bean_definition(bean_name, package, parent_bean_package, parent_bean_name)
86
90
  @extra_package_contexts.get_context(bean_definition.package)
87
91
  end
88
92
  end
89
93
 
90
- def autodetect_bean_definition(bean, package, parent_bean_package)
94
+ def autodetect_bean_definition(bean, package, parent_bean_package, parent_bean_name)
91
95
  if package
92
96
  bean_context = @extra_package_contexts.get_context(package)
93
97
  bds = @bean_definitions_storage.filter_by_with_drop_to_default_context(bean, package, bean_context)
@@ -121,7 +125,7 @@ class SmartIoC::BeanFactory
121
125
 
122
126
  if smart_bds.size > 1
123
127
  raise ArgumentError.new(
124
- %Q(Unable to autodetect bean :#{bean}.
128
+ %Q(Unable to autodetect bean :#{bean}#{parent_bean_name ? " for bean :#{parent_bean_name}" : ''}.
125
129
  Several definitions were found:\n
126
130
  #{smart_bds.map(&:inspect).join("\n\n")}.
127
131
  Set package directly for injected dependency
@@ -34,7 +34,7 @@ class SmartIoC::BeanLocations
34
34
 
35
35
  # @return names of all found beans
36
36
  def all_bean_names
37
- @data.keys
37
+ @data.values.flat_map(&:keys)
38
38
  end
39
39
 
40
40
  def get_bean_by_path(path)
@@ -57,8 +57,6 @@ module SmartIoC
57
57
  raise ArgumentError, "bean scope should be one of #{allowed_scopes.inspect}"
58
58
  end
59
59
 
60
- package_name ||= SmartIoC::BeanLocations.get_bean_package(path)
61
-
62
60
  if !package_name
63
61
  raise ArgumentError, %Q(
64
62
  Package name should be given for bean :#{bean_name}.
@@ -88,10 +86,16 @@ module SmartIoC
88
86
  end
89
87
 
90
88
  # Returns bean definition for specific class
91
- # @param klass [Class] class name
89
+ # @param bean_name [Symbol]
90
+ # @param package [Symbol]
91
+ # @param context [Symbol]
92
92
  # return [BeanDefinition]
93
- def get_bean_definition_by_class(klass)
94
- bean_definitions_storage.find_by_class(klass)
93
+ def find_bean_definition(bean_name, package, context)
94
+ bean_definitions_storage.find_bean(bean_name, package, context)
95
+ end
96
+
97
+ def get_bean_definition(...)
98
+ bean_factory.get_bean_definition(...)
95
99
  end
96
100
 
97
101
  # Sets new load proc
@@ -117,12 +121,13 @@ module SmartIoC
117
121
  # @param optional parent_bean_definition [SmartIoc::BeanDefinition] bean definition of parent bean
118
122
  # @param optional context [Symbol] package context
119
123
  # @return bean instance from container
120
- def get_bean(bean_name, package: nil, context: nil, parent_bean_definition: nil)
124
+ def get_bean(bean_name, package: nil, context: nil, parent_bean_definition: nil, parent_bean_name: nil)
121
125
  bean_factory.get_bean(
122
126
  bean_name,
123
127
  package: package,
124
128
  parent_bean_definition: parent_bean_definition,
125
129
  context: context,
130
+ parent_bean_name: parent_bean_name,
126
131
  )
127
132
  end
128
133
 
@@ -2,6 +2,9 @@
2
2
  # Example of usage:
3
3
  # class Bar
4
4
  # bean :bar
5
+ #
6
+ # def call
7
+ # end
5
8
  # end
6
9
  #
7
10
  # class Foo
@@ -12,6 +15,7 @@
12
15
  # inject :some_bar, ref: bar, from: :repository
13
16
  #
14
17
  # def hello_world
18
+ # some_bar.call
15
19
  # puts 'Hello world'
16
20
  # end
17
21
  # end
@@ -23,6 +27,35 @@ module SmartIoC::Iocify
23
27
  end
24
28
 
25
29
  module ClassMethods
30
+ def package(name)
31
+ raise ArgumentError, "name should be a Symbol" if !name.is_a?(Symbol)
32
+ @package = name
33
+ end
34
+
35
+ def context(name)
36
+ raise ArgumentError, "name should be a Symbol" if !name.is_a?(Symbol)
37
+ @context = name
38
+ end
39
+
40
+ def factory_method(name)
41
+ raise ArgumentError, "name should be a Symbol" if !name.is_a?(Symbol)
42
+ @factory_method = name
43
+ end
44
+
45
+ def scope(name)
46
+ raise ArgumentError, "name should be a Symbol" if !name.is_a?(Symbol)
47
+ @scope = name
48
+ end
49
+
50
+ def instance
51
+ @instance = true
52
+ end
53
+
54
+ def after_init(name)
55
+ raise ArgumentError, "name should be a Symbol" if !name.is_a?(Symbol)
56
+ @after_init = name
57
+ end
58
+
26
59
  # @param bean_name [Symbol] bean name
27
60
  # @param scope [Symbol] bean scope (defaults to :singleton)
28
61
  # @param package [nil or Symbol]
@@ -31,10 +64,11 @@ module SmartIoC::Iocify
31
64
  # @param context [Symbol] set bean context (ex: :test)
32
65
  # @param after_init [Symbol] name of bean method that will be called after bean initialization (ex: :test)
33
66
  # @return nil
34
- def bean(bean_name, scope: nil, package: nil, instance: true, factory_method: nil, context: nil, after_init: nil)
35
- file_path = caller[0].split(':').first
36
-
37
- bean_definition = SmartIoC.get_bean_definition_by_class(self)
67
+ def bean(bean_name, scope: nil, package: nil, instance: true, factory_method: nil, context: nil, after_init: nil, file_path: nil)
68
+ file_path ||= caller[0].split(':').first
69
+ package ||= SmartIoC::BeanLocations.get_bean_package(file_path)
70
+ context ||= SmartIoC::Container::DEFAULT_CONTEXT
71
+ bean_definition = SmartIoC.find_bean_definition(bean_name, package, context)
38
72
 
39
73
  if bean_definition
40
74
  if bean_definition.path == file_path
@@ -68,9 +102,20 @@ module SmartIoC::Iocify
68
102
  )
69
103
  end
70
104
 
105
+ self.instance_variable_set(:@bean_definition, bean_definition)
106
+
71
107
  nil
72
108
  end
73
109
 
110
+ def inject(bean_name, ref: nil, from: nil)
111
+ if @anonymous_bean
112
+ @injects ||= []
113
+ @injects.push({bean_name: bean_name, ref: ref, from: from})
114
+ else
115
+ register_inject(bean_name, ref: ref, from: from)
116
+ end
117
+ end
118
+
74
119
  # @param bean_name [Symbol] injected bean name
75
120
  # @param ref [Symbol] refferece bean to be sef as bean_name
76
121
  # @param from [Symbol] package name
@@ -79,14 +124,14 @@ module SmartIoC::Iocify
79
124
  # @raise [ArgumentError] if ref provided and ref is not a Symbol
80
125
  # @raise [ArgumentError] if from provided and from is not a Symbol
81
126
  # @raise [ArgumentError] if bean with same name was injected before
82
- def inject(bean_name, ref: nil, from: nil)
83
- bean_definition = SmartIoC::Container.get_instance.get_bean_definition_by_class(self)
84
-
85
- if bean_definition.nil?
127
+ def register_inject(bean_name, ref: nil, from: nil)
128
+ if !@bean_definition
86
129
  raise ArgumentError, "#{self.to_s} is not registered as bean. Add `bean :bean_name` declaration"
87
130
  end
88
131
 
89
- bean_definition.add_dependency(
132
+ bd = @bean_definition
133
+
134
+ bd.add_dependency(
90
135
  bean_name: bean_name,
91
136
  ref: ref,
92
137
  package: from
@@ -97,18 +142,18 @@ module SmartIoC::Iocify
97
142
  return bean if bean
98
143
 
99
144
  klass = self.is_a?(Class) ? self : self.class
100
- bean_definition = SmartIoC::Container.get_instance.get_bean_definition_by_class(klass)
101
145
 
102
146
  bean = SmartIoC::Container.get_instance.get_bean(
103
147
  ref || bean_name,
104
148
  package: from,
105
- parent_bean_definition: bean_definition
149
+ parent_bean_definition: bd,
150
+ parent_bean_name: bd.name,
106
151
  )
107
152
 
108
153
  instance_variable_set(:"@#{bean_name}", bean)
109
154
  end
110
155
 
111
- if bean_definition.is_instance?
156
+ if bd.is_instance?
112
157
  define_method bean_name, &bean_method
113
158
  private bean_name
114
159
  else
@@ -0,0 +1,13 @@
1
+ module SmartIoC
2
+ class StringUtils
3
+ class << self
4
+ def camelize(term)
5
+ string = term.to_s
6
+ string = string.sub(/^[a-z\d]*/) { |match| match.capitalize }
7
+ string.gsub!(/(?:_|(\/))([a-z\d]*)/) { "#{$1}#{$2.capitalize}" }
8
+ string.gsub!("/".freeze, "::".freeze)
9
+ string
10
+ end
11
+ end
12
+ end
13
+ end
@@ -1,3 +1,3 @@
1
1
  module SmartIoC
2
- VERSION = "0.4.0"
2
+ VERSION = "0.5.1"
3
3
  end
data/lib/smart_ioc.rb CHANGED
@@ -16,6 +16,7 @@ module SmartIoC
16
16
  autoload :InjectMetadata, 'smart_ioc/inject_metadata'
17
17
  autoload :Iocify, 'smart_ioc/iocify'
18
18
  autoload :Scopes, 'smart_ioc/scopes'
19
+ autoload :StringUtils, 'smart_ioc/string_utils'
19
20
 
20
21
  module Scopes
21
22
  autoload :Bean, 'smart_ioc/scopes/bean'
@@ -78,7 +79,8 @@ module SmartIoC
78
79
 
79
80
  container_methods = [
80
81
  :register_bean,
81
- :get_bean_definition_by_class,
82
+ :get_bean_definition,
83
+ :find_bean_definition,
82
84
  :set_extra_context_for_package,
83
85
  :get_bean,
84
86
  :clear_scopes,
@@ -89,3 +91,5 @@ module SmartIoC
89
91
  def_delegators :container, *container_methods
90
92
  end
91
93
  end
94
+
95
+ require 'smart_ioc/bean'
data/smart_ioc.gemspec CHANGED
@@ -20,5 +20,5 @@ Gem::Specification.new do |spec|
20
20
 
21
21
  spec.add_development_dependency 'bundler'
22
22
  spec.add_development_dependency 'rake'
23
- spec.add_development_dependency "codecov"
23
+ spec.add_development_dependency "pry"
24
24
  end
@@ -16,8 +16,7 @@ describe SmartIoC::BeanDefinition do
16
16
  )
17
17
 
18
18
  str =
19
- "class: Object
20
- name: :test_bean
19
+ "name: :test_bean
21
20
  package: :test_package
22
21
  context: :default
23
22
  path: current_dir
@@ -4,27 +4,42 @@ describe SmartIoC::BeanFactory do
4
4
  before :all do
5
5
  SmartIoC.clear
6
6
 
7
- class Repo
8
- include SmartIoC::Iocify
9
- bean :repo, context: :default, package: :bean_factory
7
+ bean :repo do
8
+ context :default
9
+ package :bean_factory
10
+
11
+ def call
12
+ 'default'
13
+ end
10
14
  end
11
15
 
12
- class TestRepo
13
- include SmartIoC::Iocify
14
- bean :repo, context: :test, package: :bean_factory
16
+ bean :repo do
17
+ context :test
18
+ package :bean_factory
19
+
20
+ inject :dao
21
+
22
+ def call
23
+ dao.call
24
+ 'test'
25
+ end
15
26
  end
16
27
 
17
- class DAO
18
- include SmartIoC::Iocify
19
- bean :dao, context: :default, package: :bean_factory
28
+ bean :dao do
29
+ context :default
30
+ package :bean_factory
31
+
32
+ def call
33
+ 'dao'
34
+ end
20
35
  end
21
36
 
22
37
  class TestObject
23
38
  end
24
39
 
25
- class Factory
26
- include SmartIoC::Iocify
27
- bean :factory, factory_method: :build_bean, package: :bean_factory
40
+ bean :factory do
41
+ factory_method :build_bean
42
+ package :bean_factory
28
43
 
29
44
  def build_bean
30
45
  TestObject.new
@@ -56,17 +71,17 @@ describe SmartIoC::BeanFactory do
56
71
 
57
72
  it 'returns proper bean for test context' do
58
73
  SmartIoC.set_extra_context_for_package(:bean_factory, :test)
59
- expect(SmartIoC.get_bean(:repo)).to be_a(TestRepo)
74
+ expect(SmartIoC.get_bean(:repo).call).to eq('test')
60
75
  end
61
76
 
62
77
  it 'returns proper bean for default context' do
63
78
  SmartIoC.set_extra_context_for_package(:bean_factory, :default)
64
- expect(SmartIoC.get_bean(:repo)).to be_a(Repo)
79
+ expect(SmartIoC.get_bean(:repo).call).to eq('default')
65
80
  end
66
81
 
67
82
  it 'returns proper bean for test context with fallback to default context' do
68
83
  SmartIoC.set_extra_context_for_package(:bean_factory, :test)
69
- expect(SmartIoC.get_bean(:dao)).to be_a(DAO)
84
+ expect(SmartIoC.get_bean(:dao).call).to eq('dao')
70
85
  end
71
86
 
72
87
  it 'updates dependencies' do
@@ -154,7 +169,7 @@ describe SmartIoC::BeanFactory do
154
169
  end
155
170
 
156
171
  it 'injects prototype beans with different object id' do
157
- prototype_bean = SmartIoC.get_bean(:prototype_bean)
172
+ prototype_bean = SmartIoC.get_bean(:prototype_bean, package: :prototype)
158
173
  repo1_object_id = prototype_bean.prototype_service1.prototype_repo.object_id
159
174
  repo2_object_id = prototype_bean.prototype_service2.prototype_repo.object_id
160
175
 
@@ -162,7 +177,7 @@ describe SmartIoC::BeanFactory do
162
177
  end
163
178
 
164
179
  it 'injects singleton beans with same object id' do
165
- prototype_bean = SmartIoC.get_bean(:prototype_bean)
180
+ prototype_bean = SmartIoC.get_bean(:prototype_bean, package: :prototype)
166
181
  repo1_object_id = prototype_bean.prototype_service1.singleton_repo.object_id
167
182
  repo2_object_id = prototype_bean.prototype_service2.singleton_repo.object_id
168
183
 
@@ -1,7 +1,5 @@
1
- class AdminsDAO
2
- include SmartIoC::Iocify
3
-
4
- bean :dao, instance: false, after_init: :setup
1
+ bean :dao do
2
+ after_init :setup
5
3
 
6
4
  inject :config
7
5
 
@@ -1,13 +1,7 @@
1
- class AdminsRepository
2
- include SmartIoC::Iocify
3
-
4
- bean :repository
5
-
1
+ bean :repository do
6
2
  inject :dao
7
3
  inject :users_creator
8
4
 
9
- public :users_creator
10
-
11
5
  def put(user)
12
6
  dao.insert(user)
13
7
  end
@@ -32,7 +32,7 @@ describe Object do
32
32
  factory_method: :my_method, context: :test
33
33
  end
34
34
 
35
- @bean_definition = SmartIoC.get_bean_definition_by_class(BeanClass)
35
+ @bean_definition = SmartIoC.find_bean_definition(:my_bean, :my_package, :test)
36
36
  end
37
37
 
38
38
  it { expect(@bean_definition.name).to eq(:my_bean) }
@@ -18,7 +18,7 @@ describe SmartIoC::Container do
18
18
 
19
19
  it 'loads recursive beans' do
20
20
  users_creator = @container.get_bean(:users_creator)
21
- uc2 = users_creator.send(:repository).users_creator
21
+ uc2 = users_creator.send(:repository).send(:users_creator)
22
22
  expect(users_creator).to eq(uc2)
23
23
  end
24
24
  end
@@ -27,14 +27,16 @@ describe SmartIoC do
27
27
  repository = @container.get_bean(:repository, package: :admins)
28
28
 
29
29
  expect(repository.get(1)).to be_a(User)
30
- expect(users_creator.send(:repository)).to be_a(AdminsRepository)
30
+ expect(users_creator.send(:repository)).to be_a(Class)
31
+ expect(users_creator.send(:repository).methods).to include(:put)
32
+ expect(users_creator.send(:repository).methods).to include(:get)
31
33
  expect(users_creator.send(:logger)).to be_a(LoggerFactory::SmartIoCLogger)
32
34
  end
33
35
 
34
36
  it 'sets beans with extra package context' do
35
37
  SmartIoC.set_extra_context_for_package(:admins, :test)
36
38
  SmartIoC.force_clear_scopes
37
-
39
+
38
40
  users_creator = @container.get_bean(:users_creator)
39
41
  users_creator.create(1, 'test@test.com')
40
42
 
data/spec/spec_helper.rb CHANGED
@@ -1,18 +1,13 @@
1
1
  require 'rubygems'
2
2
  require 'bundler/setup'
3
3
  require 'simplecov'
4
+ require 'pry'
4
5
 
5
6
  SimpleCov.start do
6
7
  add_filter "/spec/"
7
8
  add_filter "/.direnv/"
8
9
  end
9
10
 
10
- if ENV['CI']=='true'
11
- require 'codecov'
12
-
13
- SimpleCov.formatter = SimpleCov::Formatter::Codecov
14
- end
15
-
16
11
  require 'smart_ioc'
17
12
 
18
13
  RSpec.configure do |config|
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: smart_ioc
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.5.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ruslan Gatiyatov
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-03-18 00:00:00.000000000 Z
11
+ date: 2024-10-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -39,7 +39,7 @@ dependencies:
39
39
  - !ruby/object:Gem::Version
40
40
  version: '0'
41
41
  - !ruby/object:Gem::Dependency
42
- name: codecov
42
+ name: pry
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
45
  - - ">="
@@ -59,10 +59,10 @@ executables: []
59
59
  extensions: []
60
60
  extra_rdoc_files: []
61
61
  files:
62
+ - ".github/workflows/rspec.yml"
62
63
  - ".gitignore"
63
64
  - ".rspec"
64
65
  - ".ruby-version"
65
- - ".travis.yml"
66
66
  - ".yardops"
67
67
  - Gemfile
68
68
  - Gemfile.lock
@@ -73,6 +73,7 @@ files:
73
73
  - docs/index.md
74
74
  - lib/smart_ioc.rb
75
75
  - lib/smart_ioc/args.rb
76
+ - lib/smart_ioc/bean.rb
76
77
  - lib/smart_ioc/bean_definition.rb
77
78
  - lib/smart_ioc/bean_definitions_storage.rb
78
79
  - lib/smart_ioc/bean_dependency.rb
@@ -90,6 +91,7 @@ files:
90
91
  - lib/smart_ioc/scopes/prototype.rb
91
92
  - lib/smart_ioc/scopes/request.rb
92
93
  - lib/smart_ioc/scopes/singleton.rb
94
+ - lib/smart_ioc/string_utils.rb
93
95
  - lib/smart_ioc/version.rb
94
96
  - smart_ioc.gemspec
95
97
  - spec/smart_ioc/bean_definition_spec.rb
@@ -133,7 +135,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
133
135
  - !ruby/object:Gem::Version
134
136
  version: '0'
135
137
  requirements: []
136
- rubygems_version: 3.3.7
138
+ rubygems_version: 3.5.10
137
139
  signing_key:
138
140
  specification_version: 4
139
141
  summary: Inversion of Control Container
data/.travis.yml DELETED
@@ -1,7 +0,0 @@
1
- rvm:
2
- - 2.2.0
3
- - 2.3.0
4
- - 2.4.0
5
- script:
6
- - "gem install bundler"
7
- - "bundle exec rspec spec/"