Class: SmartIoC::BeanDefinitionsStorage
- Inherits:
-
Object
- Object
- SmartIoC::BeanDefinitionsStorage
- Includes:
- Errors
- Defined in:
- lib/smart_ioc/bean_definitions_storage.rb
Instance Method Summary collapse
- #clear_dependencies ⇒ Object
- #delete_by_class(klass) ⇒ Object
- #filter_by(bean_name, package = nil, context = nil) ⇒ Object
- #filter_by_with_drop_to_default_context(bean_name, package = nil, context = nil) ⇒ Object
- #find(bean_name, package = nil, context = nil, parent_package = nil) ⇒ Object
-
#find_bean(bean_name, package, context) ⇒ Object
Returns bean definition for specific class.
-
#find_by_class(klass) ⇒ Object
Bean definition [BeanDefinition] or nil.
-
#initialize ⇒ BeanDefinitionsStorage
constructor
A new instance of BeanDefinitionsStorage.
- #push(bean_definition) ⇒ Object
Constructor Details
#initialize ⇒ BeanDefinitionsStorage
Returns a new instance of BeanDefinitionsStorage.
4 5 6 |
# File 'lib/smart_ioc/bean_definitions_storage.rb', line 4 def initialize @collection = [] end |
Instance Method Details
#clear_dependencies ⇒ Object
8 9 10 11 12 13 14 |
# File 'lib/smart_ioc/bean_definitions_storage.rb', line 8 def clear_dependencies @collection.each do |bd| bd.dependencies.each do |dependency| dependency.bean_definition = nil end end end |
#delete_by_class(klass) ⇒ Object
40 41 42 43 44 45 46 47 |
# File 'lib/smart_ioc/bean_definitions_storage.rb', line 40 def delete_by_class(klass) klass_str = klass.to_s bean = @collection.detect {|bd| bd.klass.to_s == klass_str} if bean @collection.delete(bean) end end |
#filter_by(bean_name, package = nil, context = nil) ⇒ Object
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/smart_ioc/bean_definitions_storage.rb', line 64 def filter_by(bean_name, package = nil, context = nil) bean_definitions = @collection.select do |bd| bd.name == bean_name end if package bean_definitions = bean_definitions.select do |bd| bd.package == package end end if context bean_definitions = bean_definitions.select do |bd| bd.context == context end end bean_definitions end |
#filter_by_with_drop_to_default_context(bean_name, package = nil, context = nil) ⇒ Object
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 |
# File 'lib/smart_ioc/bean_definitions_storage.rb', line 114 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 if package bean_definitions = bean_definitions.select do |bd| bd.package == package end end if context context_bean_definitions = bean_definitions.select do |bd| bd.context == context end if !context_bean_definitions.empty? bean_definitions = context_bean_definitions end end bean_definitions end |
#find(bean_name, package = nil, context = nil, parent_package = nil) ⇒ Object
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/smart_ioc/bean_definitions_storage.rb', line 89 def find(bean_name, package = nil, context = nil, parent_package = nil) bds = filter_by_with_drop_to_default_context(bean_name, package, context) if bds.size > 1 && parent_package bean_definition = bds.detect do |bd| bd.package == parent_package end if bean_definition bds = [bean_definition] end end if bds.size > 1 raise AmbiguousBeanDefinition.new(bean_name, bds) elsif bds.size == 0 raise BeanNotFound.new(bean_name) end bds.first end |
#find_bean(bean_name, package, context) ⇒ Object
Returns bean definition for specific class
60 61 62 |
# File 'lib/smart_ioc/bean_definitions_storage.rb', line 60 def find_bean(bean_name, package, context) @collection.detect {|bd| bd.name == bean_name && bd.package == package && bd.context == context} end |
#find_by_class(klass) ⇒ Object
Returns bean definition [BeanDefinition] or nil.
51 52 53 |
# File 'lib/smart_ioc/bean_definitions_storage.rb', line 51 def find_by_class(klass) @collection.detect {|bd| bd.klass == klass} end |
#push(bean_definition) ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/smart_ioc/bean_definitions_storage.rb', line 17 def push(bean_definition) existing_bd = @collection.detect do |bd| bd == bean_definition end if existing_bd @collection.reject! { |bd| bd == existing_bd } = <<~EOF \nReplacing bean definition... - New bean details: #{bean_definition.inspect} - Existing bean details: #{existing_bd.inspect}) EOF puts end @collection.push(bean_definition) end |