Class: Callme::DepFactory

Inherits:
Object
  • Object
show all
Defined in:
lib/callme/dep_factory.rb

Overview

Instantiates deps according to their scopes

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(const_loader, deps_metadata_storage) ⇒ DepFactory

Constructor

Parameters:



7
8
9
10
11
12
13
# File 'lib/callme/dep_factory.rb', line 7

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

Instance Attribute Details

#const_loaderObject (readonly)

Returns the value of attribute const_loader.



3
4
5
# File 'lib/callme/dep_factory.rb', line 3

def const_loader
  @const_loader
end

Instance Method Details

#create_dep_and_save(dep_metadata, deps_storage) ⇒ Object

Create new dep instance according to the specified dep_metadata

Parameters:

Returns:

  • dep instance

Raises:

  • MissingDepError if some of dep dependencies are not found



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
# File 'lib/callme/dep_factory.rb', line 41

def create_dep_and_save(, deps_storage)
  if .dep_class.is_a?(Class)
    dep_class = .dep_class
  else
    dep_class = const_loader.load_const(.dep_class)
    .fetch_attrs!(dep_class)
  end
  dep = .instance ? dep_class.new : dep_class

  if .has_contract?
    contract_validator.validate(dep_class, .contract, const_loader)
  end

  if .has_factory_method?
    set_dep_dependencies(dep, )
    dep = dep.send(.factory_method)
    deps_storage[.name] = dep
  else
    # put to container first to prevent circular dependencies
    deps_storage[.name] = dep
    set_dep_dependencies(dep, )
  end

  dep
end

#delete_dep(name) ⇒ Object

Delete dep from the container by it’s name.

Parameters:

  • dep (Symbol)

    name

Raises:

  • MissingDepError if dep with the specified name is not found



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

def delete_dep(name)
   = @deps_metadata_storage.by_name(name)
  unless 
    raise Callme::Errors::MissingDepError, "Dep with name :#{name} is not defined"
  end
  ().delete_dep()
end

#get_dep(name) ⇒ Object

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

Parameters:

  • dep (Symbol)

    name

Returns:

  • dep instance

Raises:

  • MissingDepError if dep with the specified name is not found



21
22
23
24
25
26
27
# File 'lib/callme/dep_factory.rb', line 21

def get_dep(name)
   = @deps_metadata_storage.by_name(name)
  unless 
    raise Callme::Errors::MissingDepError, "Dep with name :#{name} is not defined"
  end
  ()
end

#get_dep_with_metadata(dep_metadata) ⇒ Object

Get dep by the specified dep metadata

Parameters:

Returns:

  • dep instance



32
33
34
# File 'lib/callme/dep_factory.rb', line 32

def ()
  ().get_dep()
end