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 = []
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.each do |bd|
    bd.dependencies.each do |dependency|
      dependency.bean_definition = nil
    end
  end
end

#delete_by_class(klass) ⇒ Object



37
38
39
40
41
42
43
44
# File 'lib/smart_ioc/bean_definitions_storage.rb', line 37

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



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

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



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/smart_ioc/bean_definitions_storage.rb', line 92

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) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
# File 'lib/smart_ioc/bean_definitions_storage.rb', line 77

def find(bean_name, package = nil, context = nil)
  bds = filter_by_with_drop_to_default_context(bean_name, package, context)

  if bds.size > 1
    raise AmbiguousBeanDefinition.new(bean_name, bds)
  elsif bds.size == 0
    raise BeanNotFound.new(bean_name)
  end

  bds.first
end

#find_by_class(klass) ⇒ Object

Returns bean definition [BeanDefinition] or nil.

Parameters:

  • klass (Class)

    bean class

Returns:

  • bean definition [BeanDefinition] or nil



48
49
50
51
# File 'lib/smart_ioc/bean_definitions_storage.rb', line 48

def find_by_class(klass)
  klass_str = klass.to_s
  @collection.detect {|bd| bd.klass.to_s == klass_str}
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
# 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
    error_msg =
%Q(Not able to add bean to definitions storage.
Bean definition already exists.
New bean details:
#{bean_definition.inspect}
Existing bean details:
#{existing_bd.inspect})

    raise ArgumentError, error_msg
  end

  @collection.push(bean_definition)
end