Class: Seasar::Container::S2Container

Inherits:
Object
  • Object
show all
Defined in:
lib/seasar/container/s2container.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeS2Container

  • args

    • none



25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/seasar/container/s2container.rb', line 25

def initialize
  @component_def_list  = []
  @component_def_map  = {}
  @outer_component_def = Seasar::Container::OuterComponentDef.new(self)
  component_def = SimpleComponentDef.new(self, 's2container')
  @component_def_map['container'] = component_def
  @component_def_map[:container] = component_def
  @component_def_map[self.class] = component_def
  @root = self
  @parents  = []
  @children = []
  @namespace = nil
  @inited = false;
end

Instance Attribute Details

#initedObject

Returns the value of attribute inited.



39
40
41
# File 'lib/seasar/container/s2container.rb', line 39

def inited
  @inited
end

#namespaceObject

Returns the value of attribute namespace.



39
40
41
# File 'lib/seasar/container/s2container.rb', line 39

def namespace
  @namespace
end

#parentsObject

Returns the value of attribute parents.



39
40
41
# File 'lib/seasar/container/s2container.rb', line 39

def parents
  @parents
end

#rootObject

Returns the value of attribute root.



39
40
41
# File 'lib/seasar/container/s2container.rb', line 39

def root
  @root
end

Instance Method Details

#destroyObject

  • args

    • none

  • return

    • Boolean



61
62
63
64
65
66
# File 'lib/seasar/container/s2container.rb', line 61

def destroy
  @component_def_list.reverse.each {|component_def| component_def.destroy}
  @children.reverse.each {|child| child.destroy}
  @inited = false
  return @inited
end

#find_component_defs(key) ⇒ Object

  • args

    1. String|Symbol key

  • return

    • Array



271
272
273
274
275
276
277
278
# File 'lib/seasar/container/s2container.rb', line 271

def find_component_defs(key)
  component_def = self.get_component_def(key)
  if component_def.instance_of?(Seasar::Container::TooManyRegistrationComponentDef)
    return component_def.component_defs
  else
    return [component_def]
  end
end

#find_components(key) ⇒ Object

  • args

    1. String|Symbol key

  • return

    • Array



256
257
258
259
260
261
262
263
# File 'lib/seasar/container/s2container.rb', line 256

def find_components(key)
  component_defs = self.find_component_defs(key)
  components = []
  for component_def in component_defs
    components << component_def.component
  end
  return components
end

#get_component(key, &procedure) ⇒ Object Also known as: component, get, []

  • args

    1. String|Symbol key

    2. Proc procedure

  • return

    • Object



147
148
149
150
151
# File 'lib/seasar/container/s2container.rb', line 147

def get_component(key, &procedure)
  cd = self.get_component_def(key)
  cd.onetime_proc = procedure
  return cd.get_component
end

#get_component_def(key) ⇒ Object Also known as: component_def

  • args

    1. String|Symbol key

  • return

    • Seasar::Container::ComponentDef



162
163
164
165
166
167
168
# File 'lib/seasar/container/s2container.rb', line 162

def get_component_def(key)
  component_def = self.get_component_def_internal(key);
  if component_def.nil?
    raise Seasar::Container::Exception::ComponentNotFoundRuntimeException.new(key)
  end
  return component_def
end

#get_component_def_from_children(key, ignore_children = []) ⇒ Object

  • args

    1. String|Symbol key

  • return

    • Seasar::Container::ComponentDef



230
231
232
233
234
235
236
237
# File 'lib/seasar/container/s2container.rb', line 230

def get_component_def_from_children(key, ignore_children = [])
  @children.each {|child_container|
    next if ignore_children.member?(child_container)
    component_def = child_container.get_component_def_internal(key, false)
    return component_def unless component_def.nil?
  }
  return nil
end

#get_component_def_internal(key, search_parent = true, ignore_children = []) ⇒ Object

ComponentDefをkeyで検索して返します。見つからなければnilが返ります。

keyがclassの場合は、自分自身が保持しているかを検索して、すべての子のコンテナ
に問い合わせます。keyが文字列の場合は、「.」区切りになっている場合は、namespace
で指定されたコンテナ以下のすべての子コンテナ内でComponentDefを検索します。
その後、見つからない場合は、自分自身が保持しているすべての子コンテナを検索します。
  • args

    1. String|Symbol key

    2. Boolean search_parent

    3. Array ignore_children

  • return

    • Seasar::Container::ComponentDef



184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
# File 'lib/seasar/container/s2container.rb', line 184

def get_component_def_internal(key, search_parent = true, ignore_children = [])
  if @component_def_map.key?(key)
    return @component_def_map[key]
  elsif key.is_a?(String) && key.match(/\./)
    component_def = self.get_component_def_with_namespace(key)
    return component_def unless component_def.nil?
  else
    component_def = self.get_component_def_from_children(key, ignore_children)
    return component_def unless component_def.nil?
  end
  return nil unless search_parent

  ignore_children << self
  return nil if @parents.size == 0
  @parents.each {|parent|
    next if parent.parents.size == 0 && ignore_children.member?(parent)
    component_def = parent.get_component_def_internal(key, true, ignore_children)
    return component_def unless component_def.nil?
  }
  return nil
end

#get_component_def_sizeObject

  • args

    • none

  • return

    • Integer



136
137
138
# File 'lib/seasar/container/s2container.rb', line 136

def get_component_def_size
  return @component_def_list.length
end

#get_component_def_with_namespace(key) ⇒ Object

  • args

    1. String|Symbol key

  • return

    • Seasar::Container::ComponentDef



212
213
214
215
216
217
218
219
220
221
222
# File 'lib/seasar/container/s2container.rb', line 212

def get_component_def_with_namespace(key)
  if /^(.+?)\.(.+)$/ =~ key
    component_def = self.get_component_def_internal($1, false)
    unless component_def.nil?
      child_container = component_def.get_component()
      component_def = child_container.get_component_def_internal($2, false)
      return component_def unless component_def.nil?
    end
  end
  return nil
end

#has_component_def(key) ⇒ Object Also known as: component_def?

  • args

    1. String|Symbol key

  • return

    • Boolean



245
246
247
# File 'lib/seasar/container/s2container.rb', line 245

def has_component_def(key)
  return self.get_component_def_internal(key).nil? == false
end

#include(child_container) ⇒ Object

  • args

    1. Seasar::Container::S2Container child_container

  • return

    • none



286
287
288
289
290
291
292
293
294
# File 'lib/seasar/container/s2container.rb', line 286

def include(child_container)
  child_container.root = @root
  child_container.parents << self
  @children << child_container
  namespace = child_container.namespace
  if not namespace.nil?
    self.register_map(namespace, S2ContainerComponentDef.new(child_container, namespace))
  end
end

#initObject

  • args

    • none

  • return

    • Boolean



47
48
49
50
51
52
53
# File 'lib/seasar/container/s2container.rb', line 47

def init
  return if @inited
  @children.each {|child| child.init}
  @component_def_list.each {|component_def| component_def.init}
  @inited = true
  return @inited
end

#inject(instance) ⇒ Object

  • args

    1. Object instance

  • return

    • Seasar::Container::Assembler::AbstractAssembler



313
314
315
316
# File 'lib/seasar/container/s2container.rb', line 313

def inject(instance)
  @outer_component_def.instance = instance
  return @outer_component_def.component
end

#process_toomany_registration(key, new_component_def) ⇒ Object

  • args

    1. String|Symbol key

    2. Seasar::Container::ComponentDef component_def

  • return

    • none



325
326
327
328
329
330
331
332
333
334
335
# File 'lib/seasar/container/s2container.rb', line 325

def process_toomany_registration(key, new_component_def)
  component_def = @component_def_map[key]
  if component_def.instance_of?(Seasar::Container::TooManyRegistrationComponentDef)
    component_def.add_component_def(new_component_def)
  else
    tmrcf = Seasar::Container::TooManyRegistrationComponentDef.new(key)
    tmrcf.add_component_def(component_def)
    tmrcf.add_component_def(new_component_def)
    @component_def_map[key] = tmrcf
  end
end

#register(item, name = nil) ⇒ Object

  • args

    1. Object|Seasar::Container::ComponentDef item

    2. String|Symbol name

  • return

    • none



75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/seasar/container/s2container.rb', line 75

def register(item, name = nil)
  if item.is_a?(ComponentDef)
    component_def = item
  elsif item.class == Class
    return self.register(ComponentDef.new(item, name))
  else
    component_def = SimpleComponentDef.new(item)
  end

  component_def.container = self if component_def.container.nil?
  @component_def_list << component_def
  self.register_by_name(component_def)
  self.register_by_class(component_def)
end

#register_by_class(component_def) ⇒ Object

  • args

    1. Seasar::Container::ComponentDef component_def

  • return

    • none



96
97
98
99
100
101
# File 'lib/seasar/container/s2container.rb', line 96

def register_by_class(component_def)
  component_def.component_class.ancestors.each {|base|
    next if (base == Object or base == Kernel)
    self.register_map(base, component_def)
  }
end

#register_by_name(component_def) ⇒ Object

  • args

    1. Seasar::Container::ComponentDef component_def

  • return

    • none



109
110
111
112
113
# File 'lib/seasar/container/s2container.rb', line 109

def register_by_name(component_def)
  if not component_def.component_name.nil?
    self.register_map(component_def.component_name, component_def)
  end
end

#register_map(key, component_def) ⇒ Object

  • args

    1. String|Symbol key

    2. Seasar::Container::ComponentDef component_def

  • return

    • none



122
123
124
125
126
127
128
# File 'lib/seasar/container/s2container.rb', line 122

def register_map(key, component_def)
  if @component_def_map.key?(key)
    self.process_toomany_registration(key, component_def)
  else
    @component_def_map[key] = component_def
  end
end