Class: SmartIoC::BeanDefinitionsStorage

Inherits:
Object
  • Object
show all
Includes:
Errors
Defined in:
lib/smart_ioc/bean_definitions_storage.rb

Instance Method Summary collapse

Constructor Details

#initializeBeanDefinitionsStorage

Returns a new instance of BeanDefinitionsStorage.



4
5
6
# File 'lib/smart_ioc/bean_definitions_storage.rb', line 4

def initialize
  @collection = Hash.new { |h, k| h[k] = [] }
end

Instance Method Details

#clear_dependenciesObject



8
9
10
11
12
13
14
# File 'lib/smart_ioc/bean_definitions_storage.rb', line 8

def clear_dependencies
  @collection.values.flatten.each do |bd|
    bd.dependencies.each do |dependency|
      dependency.bean_definition = nil
    end
  end
end

#delete(bean_definition) ⇒ Object



40
41
42
43
44
45
46
# File 'lib/smart_ioc/bean_definitions_storage.rb', line 40

def delete(bean_definition)
  bd_scope = @collection[bean_definition.name]

  bd_scope.delete_if { |bd| bd.klass.to_s == bean_definition.klass.to_s }

  nil
end

#filter_by(bean_name, package = nil, context = nil) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/smart_ioc/bean_definitions_storage.rb', line 59

def filter_by(bean_name, package = nil, context = nil)
  bd_scope = @collection[bean_name]

  if package
    bd_scope = bd_scope.select { |bd| bd.package == package }
  end

  if context
    bd_scope = bean_definitions.select { |bd| bd.context == context }
  end

  bd_scope
end

#filter_by_with_drop_to_default_context(bean_name, package = nil, context = nil) ⇒ Object



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/smart_ioc/bean_definitions_storage.rb', line 103

def filter_by_with_drop_to_default_context(bean_name, package = nil, context = nil)
  bd_scope = @collection[bean_name]

  if package
    bd_scope = bd_scope.select { |bd| bd.package == package }
  end

  if context
    context_bean_definitions = bd_scope.select { |bd| bd.context == context }

    bd_scope = context_bean_definitions if context_bean_definitions.any?
  end

  bd_scope
end

#find(bean_name, package = nil, context = nil, parent_package = nil) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/smart_ioc/bean_definitions_storage.rb', line 78

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

Parameters:

  • bean_name (Symbol)
  • package (Symbol)
  • context (Symbol)

Returns:

  • bean definition [BeanDefinition] or nil



53
54
55
56
57
# File 'lib/smart_ioc/bean_definitions_storage.rb', line 53

def find_bean(bean_name, package, context)
  @collection[bean_name].detect do |bd|
    bd.name == bean_name && bd.package == package && bd.context == context
  end
end

#push(bean_definition) ⇒ Object

Parameters:



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)
  bd_scope = @collection[bean_definition.name]

  existing_bd = bd_scope.detect { |bd| bd == bean_definition }

  if existing_bd
    bd_scope.reject! { |bd| bd == bean_definition }

    message = <<~EOF
      \nReplacing bean definition...
        - New bean details:
      #{bean_definition.inspect}
        - Existing bean details:
      #{existing_bd.inspect})

    EOF

    puts message
  end

  bd_scope.push(bean_definition)
end