From 65b5be657592945f249641f0d86f242515e9672c Mon Sep 17 00:00:00 2001 From: Nikita Kononov Date: Sun, 6 Oct 2024 20:34:04 +0000 Subject: [PATCH 1/2] index bean_definitions by name --- .ruby-version | 2 +- lib/smart_ioc/bean_definitions_storage.rb | 65 ++++++++--------------- lib/smart_ioc/container.rb | 6 +-- lib/smart_ioc/iocify.rb | 5 +- 4 files changed, 29 insertions(+), 49 deletions(-) diff --git a/.ruby-version b/.ruby-version index 94ff29c..be94e6f 100644 --- a/.ruby-version +++ b/.ruby-version @@ -1 +1 @@ -3.1.1 +3.2.2 diff --git a/lib/smart_ioc/bean_definitions_storage.rb b/lib/smart_ioc/bean_definitions_storage.rb index e119fc0..7a957e0 100644 --- a/lib/smart_ioc/bean_definitions_storage.rb +++ b/lib/smart_ioc/bean_definitions_storage.rb @@ -2,11 +2,11 @@ class SmartIoC::BeanDefinitionsStorage include SmartIoC::Errors def initialize - @collection = [] + @collection = Hash.new { |h, k| h[k] = [] } end def clear_dependencies - @collection.each do |bd| + @collection.values.flatten.each do |bd| bd.dependencies.each do |dependency| dependency.bean_definition = nil end @@ -15,12 +15,12 @@ def clear_dependencies # @param bean_definition [BeanDefinition] def push(bean_definition) - existing_bd = @collection.detect do |bd| - bd == bean_definition - end + bd_scope = @collection[bean_definition.name] + + existing_bd = bd_scope.detect { |bd| bd == bean_definition } if existing_bd - @collection.reject! { |bd| bd == existing_bd } + bd_scope.reject! { |bd| bd == bean_definition } message = <<~EOF \nReplacing bean definition... @@ -34,22 +34,15 @@ def push(bean_definition) puts message end - @collection.push(bean_definition) + bd_scope.push(bean_definition) end - def delete_by_class(klass) - klass_str = klass.to_s - bean = @collection.detect {|bd| bd.klass.to_s == klass_str} + def delete(bean_definition) + bd_scope = @collection[bean_definition.name] - if bean - @collection.delete(bean) - end - end + bd_scope.delete_if { |bd| bd.klass.to_s == bean_definition.klass.to_s } - # @param klass [Class] bean class - # @return bean definition [BeanDefinition] or nil - def find_by_class(klass) - @collection.detect {|bd| bd.klass == klass} + nil end # Returns bean definition for specific class @@ -58,27 +51,23 @@ def find_by_class(klass) # @param context [Symbol] # @return bean definition [BeanDefinition] or nil def find_bean(bean_name, package, context) - @collection.detect {|bd| bd.name == bean_name && bd.package == package && bd.context == context} + @collection[bean_name].detect do |bd| + bd.name == bean_name && bd.package == package && bd.context == context + end end def filter_by(bean_name, package = nil, context = nil) - bean_definitions = @collection.select do |bd| - bd.name == bean_name - end + bd_scope = @collection[bean_name] if package - bean_definitions = bean_definitions.select do |bd| - bd.package == package - end + bd_scope = bd_scope.select { |bd| bd.package == package } end if context - bean_definitions = bean_definitions.select do |bd| - bd.context == context - end + bd_scope = bean_definitions.select { |bd| bd.context == context } end - bean_definitions + bd_scope end # @bean_name [Symbol] bean name @@ -112,26 +101,18 @@ def find(bean_name, package = nil, context = nil, parent_package = nil) # @package [Symbol, nil] package name # @context [Symbol, nil] context def filter_by_with_drop_to_default_context(bean_name, package = nil, context = nil) - bean_definitions = @collection.select do |bd| - bd.name == bean_name - end + bd_scope = @collection[bean_name] if package - bean_definitions = bean_definitions.select do |bd| - bd.package == package - end + bd_scope = bd_scope.select { |bd| bd.package == package } end if context - context_bean_definitions = bean_definitions.select do |bd| - bd.context == context - end + context_bean_definitions = bd_scope.select { |bd| bd.context == context } - if !context_bean_definitions.empty? - bean_definitions = context_bean_definitions - end + bd_scope = context_bean_definitions if context_bean_definitions.any? end - bean_definitions + bd_scope end end diff --git a/lib/smart_ioc/container.rb b/lib/smart_ioc/container.rb index e45418c..a8defbe 100644 --- a/lib/smart_ioc/container.rb +++ b/lib/smart_ioc/container.rb @@ -19,10 +19,10 @@ def initialize raise ArgumentError, "SmartIoC::Container should not be allocated. Use SmartIoC::Container.get_instance instead" end - # @param klass [Class] bean class name + # @param klass [BeanDefinition] bean class name # @return nil - def unregister_bean(klass) - bean_definitions_storage.delete_by_class(klass) + def unregister_bean(bean_definition) + bean_definitions_storage.delete(bean_definition) clear_scopes nil end diff --git a/lib/smart_ioc/iocify.rb b/lib/smart_ioc/iocify.rb index 6ecd7d1..fdc2ac0 100644 --- a/lib/smart_ioc/iocify.rb +++ b/lib/smart_ioc/iocify.rb @@ -74,9 +74,8 @@ def bean(bean_name, scope: nil, package: nil, instance: true, factory_method: ni if bean_definition.path == file_path # seems that file with bean definition was reloaded # lets clear all scopes so we do not have - container = SmartIoC::Container.get_instance - container.unregister_bean(self) - container.force_clear_scopes + SmartIoC::Container.get_instance.unregister_bean(bean_definition) + SmartIoC::Container.get_instance.force_clear_scopes else raise ArgumentError, "bean with for class #{self.to_s} was already defined in #{bean_definition.path}" end From 9a65b56c69b6c250f557f64f335cf1a54d361980 Mon Sep 17 00:00:00 2001 From: Nikita Kononov Date: Mon, 7 Oct 2024 14:07:50 +0000 Subject: [PATCH 2/2] bump version to 0.5.2 --- Gemfile.lock | 2 +- lib/smart_ioc/version.rb | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Gemfile.lock b/Gemfile.lock index f8ce10a..b6da326 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,7 +1,7 @@ PATH remote: . specs: - smart_ioc (0.5.1) + smart_ioc (0.5.2) GEM remote: https://rubygems.org/ diff --git a/lib/smart_ioc/version.rb b/lib/smart_ioc/version.rb index 03b6418..0e7a521 100644 --- a/lib/smart_ioc/version.rb +++ b/lib/smart_ioc/version.rb @@ -1,3 +1,3 @@ module SmartIoC - VERSION = "0.5.1" + VERSION = "0.5.2" end