Class: SmartIoC::Container

Inherits:
Object
  • Object
show all
Includes:
Args
Defined in:
lib/smart_ioc/container.rb

Overview

SmartIoC::Container is a beans store used for dependency injection

Constant Summary collapse

DEFAULT_CONTEXT =
:default

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Args

#check_arg, #check_arg_any, #not_nil

Constructor Details

#initializeContainer

Returns a new instance of Container.

Raises:

  • (ArgumentError)


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

def initialize
  raise ArgumentError, "SmartIoC::Container should not be allocated. Use SmartIoC::Container.get_instance instead"
end

Class Method Details

.clearObject



13
14
15
# File 'lib/smart_ioc/container.rb', line 13

def clear
  @container = nil
end

.get_instanceObject



9
10
11
# File 'lib/smart_ioc/container.rb', line 9

def get_instance
  @container ||= SmartIoC::Container.allocate
end

Instance Method Details

#clear_scopesObject



134
135
136
# File 'lib/smart_ioc/container.rb', line 134

def clear_scopes
  bean_factory.clear_scopes
end

#find_bean_definition(bean_name, package, context) ⇒ Object

Returns bean definition for specific class return [BeanDefinition]

Parameters:

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


93
94
95
# File 'lib/smart_ioc/container.rb', line 93

def find_bean_definition(bean_name, package, context)
  bean_definitions_storage.find_bean(bean_name, package, context)
end

#force_clear_scopesObject



138
139
140
141
# File 'lib/smart_ioc/container.rb', line 138

def force_clear_scopes
  bean_factory.force_clear_scopes
  bean_factory.bean_file_loader.clear_locations
end

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

Returns bean instance from container.

Parameters:

  • bean_name (Symbol)

    bean name

  • optional

    package [Symbol] package name

  • optional

    parent_bean_definition [SmartIoc::BeanDefinition] bean definition of parent bean

  • optional

    context [Symbol] package context

Returns:

  • bean instance from container



124
125
126
127
128
129
130
131
132
# File 'lib/smart_ioc/container.rb', line 124

def get_bean(bean_name, package: nil, context: nil, parent_bean_definition: nil, parent_bean_name: nil)
  bean_factory.get_bean(
    bean_name,
    package: package,
    parent_bean_definition: parent_bean_definition,
    context: context,
    parent_bean_name: parent_bean_name,
  )
end

#get_bean_definitionObject



97
98
99
# File 'lib/smart_ioc/container.rb', line 97

def get_bean_definition(...)
  bean_factory.get_bean_definition(...)
end

#register_bean(bean_name:, klass:, context:, scope:, path:, after_init:, factory_method: nil, package_name: nil, instance: true) ⇒ SmartIoC::BeanDefinition

Returns bean definition.

Parameters:

  • bean_name (Symbol)

    bean name

  • klass (Class)

    bean class name

  • path (String)

    bean file absolute path

  • scope (Symbol)

    scope value

  • context (Symbol)

    bean context

  • after_init (Symbol)

    name of bean method that will be called after bean initialization

Returns:



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/smart_ioc/container.rb', line 37

def register_bean(bean_name:, klass:, context:, scope:, path:, after_init:,
                  factory_method: nil, package_name: nil, instance: true)
  context ||= DEFAULT_CONTEXT

  check_arg(bean_name, :bean_name, Symbol)
  check_arg(context, :context, Symbol)
  check_arg(klass, :klass, Class)
  check_arg(path, :path, String)
  check_arg(factory_method, :factory_method, Symbol) if factory_method
  check_arg_any(instance, :instance, [TrueClass, FalseClass])

  scope ||= SmartIoC::Scopes::Singleton::VALUE

  allowed_scopes = [
    SmartIoC::Scopes::Prototype::VALUE,
    SmartIoC::Scopes::Singleton::VALUE,
    SmartIoC::Scopes::Request::VALUE
  ]

  if !allowed_scopes.include?(scope)
    raise ArgumentError, "bean scope should be one of #{allowed_scopes.inspect}"
  end

  if !package_name
    raise ArgumentError, %Q(
      Package name should be given for bean :#{bean_name}.
      You should specify package name directly or run

      SmartIoC.find_package_beans(package_name, dir)

      to setup beans before you actually register them.
    )
  end

  bean_definition = SmartIoC::BeanDefinition.new(
    name:           bean_name,
    package:        package_name,
    path:           path,
    klass:          klass,
    instance:       instance,
    factory_method: factory_method,
    context:        context,
    scope:          scope,
    after_init:     after_init,
  )

  bean_definitions_storage.push(bean_definition)

  bean_definition
end

#require_bean(bean_name) ⇒ Any

Parameters:

  • bean_name (Symbol)

    bean name

Returns:

  • (Any)


145
146
147
# File 'lib/smart_ioc/container.rb', line 145

def require_bean(bean_name)
  bean_factory.bean_file_loader.require_bean(bean_name)
end

#set_extra_context_for_package(package_name, context) ⇒ Object

Sets extra context for specific package

Parameters:

  • package_name (Symbol)

    package name

  • context (Symbol)

    context (ex: :test)



114
115
116
117
# File 'lib/smart_ioc/container.rb', line 114

def set_extra_context_for_package(package_name, context)
  extra_package_contexts.set_context(package_name, context)
  bean_definitions_storage.clear_dependencies
end

#set_load_proc(&proc) ⇒ Object

Sets new load proc for those who use active support dependency loader one can use SmartIoC.set_load_proc do |location|

require_dependency(location)

end



107
108
109
# File 'lib/smart_ioc/container.rb', line 107

def set_load_proc(&proc)
  bean_factory.bean_file_loader.set_load_proc(&proc)
end

#unregister_bean(bean_definition) ⇒ Object

Returns nil.

Parameters:

Returns:

  • nil



24
25
26
27
28
# File 'lib/smart_ioc/container.rb', line 24

def unregister_bean(bean_definition)
  bean_definitions_storage.delete(bean_definition)
  clear_scopes
  nil
end