Class: SmartIoC::BeanFactory

Inherits:
Object
  • Object
show all
Includes:
Args, Errors
Defined in:
lib/smart_ioc/bean_factory.rb

Overview

Instantiates beans according to their scopes

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Args

#check_arg, #check_arg_any, #not_nil

Constructor Details

#initialize(bean_definitions_storage, extra_package_contexts) ⇒ BeanFactory

Returns a new instance of BeanFactory.



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

def initialize(bean_definitions_storage, extra_package_contexts)
  @bean_definitions_storage = bean_definitions_storage
  @extra_package_contexts   = extra_package_contexts
  @bean_file_loader         = SmartIoC::BeanFileLoader.new
  @singleton_scope          = SmartIoC::Scopes::Singleton.new
  @prototype_scope          = SmartIoC::Scopes::Prototype.new
  @thread_scope             = SmartIoC::Scopes::Request.new
  @semaphore                = Mutex.new
end

Instance Attribute Details

#bean_file_loaderObject (readonly)

Returns the value of attribute bean_file_loader.



6
7
8
# File 'lib/smart_ioc/bean_factory.rb', line 6

def bean_file_loader
  @bean_file_loader
end

Instance Method Details

#clear_scopesObject



18
19
20
# File 'lib/smart_ioc/bean_factory.rb', line 18

def clear_scopes
  all_scopes.each(&:clear)
end

#force_clear_scopesObject



22
23
24
# File 'lib/smart_ioc/bean_factory.rb', line 22

def force_clear_scopes
  all_scopes.each(&:force_clear)
end

#get_bean(bean_name, package: nil, parent_bean_definition: nil, context: nil, parent_bean_name: nil) ⇒ Object

Get bean from the container by it’s name, package, context

Parameters:

  • bean_name (Symbol)

    bean name

  • package (Symbol) (defaults to: nil)

    package name

  • parent_bean_definition (SmartIoC::BeanDefinition) (defaults to: nil)

    parent bean definition

  • context (Symbol) (defaults to: nil)

    context

Returns:

  • bean instance

Raises:

  • (ArgumentError)

    if bean is not found

  • (ArgumentError)

    if ambiguous bean definition was found



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/smart_ioc/bean_factory.rb', line 34

def get_bean(bean_name, package: nil, parent_bean_definition: nil, context: nil, parent_bean_name: nil)
  bean_definition = get_bean_definition(bean_name, package:, parent_bean_definition:, context:, parent_bean_name:)
  scope = get_scope(bean_definition)
  bean = scope.get_bean(bean_definition.klass)

  if !bean
    bean = init_bean(bean_definition)
  end

  scope.save_bean(bean_definition.klass, bean)
  bean
rescue SmartIoC::Errors::AmbiguousBeanDefinition => e
  e.parent_bean_definition = parent_bean_definition
  raise e
end

#get_bean_definition(bean_name, package: nil, context: nil, parent_bean_definition: nil, parent_bean_name: nil) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/smart_ioc/bean_factory.rb', line 50

def get_bean_definition(bean_name, package: nil, context: nil, parent_bean_definition: nil, parent_bean_name: nil)
  check_arg(bean_name, :bean_name, Symbol)
  check_arg(package, :package, Symbol) if package
  check_arg(parent_bean_definition, :parent_bean_definition, SmartIoC::BeanDefinition) if parent_bean_definition
  check_arg(context, :context, Symbol) if context

  @bean_file_loader.require_bean(bean_name)

  parent_package_name = parent_bean_definition ? parent_bean_definition.package : nil
  context = autodetect_context(bean_name, package, parent_package_name, context, parent_bean_name)
  bean_definition = @bean_definitions_storage.find(bean_name, package, context, parent_package_name)
end