Class: IocRb::BeanFactory

Inherits:
Object show all
Defined in:
lib/ioc_rb/bean_factory.rb

Overview

Instantiates beans according to their scopes

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(const_loader, beans_metadata_storage) ⇒ BeanFactory

Constructor

Parameters:



12
13
14
15
16
17
18
# File 'lib/ioc_rb/bean_factory.rb', line 12

def initialize(const_loader, )
  @const_loader           = const_loader
  @beans_metadata_storage = 
  @singleton_scope        = IocRb::Scopes::SingletonScope.new(self)
  @prototype_scope        = IocRb::Scopes::PrototypeScope.new(self)
  @request_scope          = IocRb::Scopes::RequestScope.new(self)
end

Instance Attribute Details

#const_loaderObject (readonly)

Returns the value of attribute const_loader.



8
9
10
# File 'lib/ioc_rb/bean_factory.rb', line 8

def const_loader
  @const_loader
end

Instance Method Details

#create_bean_and_save(bean_metadata, beans_storage) ⇒ Object

Create new bean instance according to the specified bean_metadata

Parameters:

Returns:

  • bean instance

Raises:

  • MissingBeanError if some of bean dependencies are not found



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/ioc_rb/bean_factory.rb', line 46

def create_bean_and_save(, beans_storage)
  if .bean_class.is_a?(Class)
    bean_class = .bean_class
  else
    bean_class = const_loader.load_const(.bean_class)
    .fetch_attrs!(bean_class)
  end
  bean = .instance ? bean_class.new : bean_class
  if .has_factory_method?
    set_bean_dependencies(bean, )
    bean = bean.send(.factory_method)
    beans_storage[.name] = bean
  else
    # put to container first to prevent circular dependencies
    beans_storage[.name] = bean
    set_bean_dependencies(bean, )
  end

  bean
end

#delete_bean(name) ⇒ Object

Delete bean from the container by it’s name.

Parameters:

  • bean (Symbol)

    name

Raises:

  • MissingBeanError if bean with the specified name is not found



70
71
72
73
74
75
76
# File 'lib/ioc_rb/bean_factory.rb', line 70

def delete_bean(name)
   = @beans_metadata_storage.by_name(name)
  unless 
    raise IocRb::Errors::MissingBeanError, "Bean with name :#{name} is not defined"
  end
  ().delete_bean()
end

#get_bean(name) ⇒ Object

Get bean from the container by it’s name. According to the bean scope it will be newly created or returned already instantiated bean

Parameters:

  • bean (Symbol)

    name

Returns:

  • bean instance

Raises:

  • MissingBeanError if bean with the specified name is not found



26
27
28
29
30
31
32
# File 'lib/ioc_rb/bean_factory.rb', line 26

def get_bean(name)
   = @beans_metadata_storage.by_name(name)
  unless 
    raise IocRb::Errors::MissingBeanError, "Bean with name :#{name} is not defined"
  end
  ()
end

#get_bean_with_metadata(bean_metadata) ⇒ Object

Get bean by the specified bean metadata

Parameters:

Returns:

  • bean instance



37
38
39
# File 'lib/ioc_rb/bean_factory.rb', line 37

def ()
  ().get_bean()
end