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



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

def initialize
  @collection = []
end

Instance Method Details

#delete_by_class(klass) ⇒ Object



29
30
31
32
33
34
35
36
# File 'lib/smart_ioc/bean_definitions_storage.rb', line 29

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



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/smart_ioc/bean_definitions_storage.rb', line 45

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



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/smart_ioc/bean_definitions_storage.rb', line 84

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



69
70
71
72
73
74
75
76
77
78
79
# File 'lib/smart_ioc/bean_definitions_storage.rb', line 69

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



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

def find_by_class(klass)
  klass_str = klass.to_s
  @collection.detect {|bd| bd.klass.to_s == klass_str}
end

#push(bean_definition) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/smart_ioc/bean_definitions_storage.rb', line 9

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