Skip to content

index bean_definitions by name #8

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Oct 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
smart_ioc (0.5.1)
smart_ioc (0.5.2)

GEM
remote: https://rubygems.org/
Expand Down
61 changes: 24 additions & 37 deletions lib/smart_ioc/bean_definitions_storage.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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...
Expand All @@ -34,16 +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
bd_scope.delete_if { |bd| bd.klass.to_s == bean_definition.klass.to_s }

nil
end

# Returns bean definition for specific class
Expand All @@ -52,27 +51,23 @@ def delete_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
Expand Down Expand Up @@ -106,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
6 changes: 3 additions & 3 deletions lib/smart_ioc/container.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 2 additions & 3 deletions lib/smart_ioc/iocify.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/smart_ioc/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module SmartIoC
VERSION = "0.5.1"
VERSION = "0.5.2"
end
Loading