Class: SinjectContainer

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

Overview

This is the IOC Container for registering all dependencies an building objects.

Class Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeSinjectContainer

Returns a new instance of SinjectContainer.



11
12
13
14
# File 'lib/sinject.rb', line 11

def initialize
  @store = []
  SinjectContainer.instance = self
end

Class Attribute Details

.instanceObject

Returns the value of attribute instance.



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

def instance
  @instance
end

Instance Method Details

#get(key) ⇒ Object

Get an object from the container. This will build the requested object and all its dependencies.

Example:

>> SinjectContainer.instance.get :object_key

Arguments:

key:  (Symbol)


70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/sinject.rb', line 70

def get(key)
  #get the dependency from the container store for the specified key
  items = @store.select { |i| i.key == key}
  if !items.empty?
    item = items.first

    #check if the item has been registered as a single instance item.
    if item.single_instance == true
      #check if the instance needs to be created
      if item.instance == nil
        item.instance = create_instance(item)
      end
      return item.instance
    else
      return create_instance(item)
    end
  else
    #no dependency has been registered for the specified key, attempt to convert the key into a class name and initialize it.
    class_name = "#{key}".split('_').collect(&:capitalize).join
    Object.const_get(class_name).new
  end
end

#is_registered?(key) ⇒ Boolean

Check if an object has been registered with the container.

Example:

>> SinjectContainer.instance.is_registered? :object_key
=> true

Arguments:

key:  (Symbol)

Returns:

  • (Boolean)


24
25
26
# File 'lib/sinject.rb', line 24

def is_registered?(key)
  !@store.select { |i| i.key == key}.empty?
end

#load_groupsObject



93
94
95
96
97
98
99
100
# File 'lib/sinject.rb', line 93

def load_groups
  DependencyGroup.descendants.each do |g|
    group = g.new
    if group.is_valid?
      group.register(SinjectContainer.instance)
    end
  end
end

#register(key, dependency_class_name, single_instance = false, contract_class_name = nil, &initialize_block) ⇒ Object

Register an object with the container. Objects can be registered as either a single instance or a multi instance object. Single instance objects are only initialized once and the same object is returned from the container for every request. Multi instance objects are a new instance that is created for each request.

Example:

>> SinjectContainer.instance.register :object_key, ClassName, true, ContractName

Arguments:

key:  (Symbol)
class_name: (ClassName)
single_instance:  (Boolean)


40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/sinject.rb', line 40

def register(key, dependency_class_name, single_instance = false, contract_class_name = nil, &initialize_block)

  #check if a dependency has already been registered for this key.
  if is_registered?(key)
    raise DependencyRegistrationException.new(key)
  end

  #check if a contract has been specified
  if contract_class_name != nil
    #validate the dependency class against the contract
    validate_contract(dependency_class_name, contract_class_name)
  end

  item = ContainerItem.new
  item.key = key
  item.single_instance = single_instance
  item.class_name = dependency_class_name
  item.initialize_block = initialize_block

  @store.push(item)
end