Class: Sinject::Container

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

Overview

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

Class Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(singleton = true) ⇒ Container

Returns a new instance of Container.



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

def initialize(singleton=true)
  @store = []
  if singleton
    Sinject::Container.instance = self
  end
end

Class Attribute Details

.instanceObject

Returns the value of attribute instance.



6
7
8
# File 'lib/sinject/container.rb', line 6

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:

>> Sinject::Container.instance.get :object_key

Arguments:

key:  (Symbol)


92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/sinject/container.rb', line 92

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
    puts "[Sinject] - WARNING: No registered dependency could be found for key: #{key}. Attempting to load class: #{class_name}."
    Object.const_get(class_name).new
  end
end

#is_registered?(key) ⇒ Boolean

Check if an object has been registered with the container.

Example:

>> Sinject::Container.instance.is_registered? :object_key
=> true

Arguments:

key:  (Symbol)

Returns:

  • (Boolean)


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

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

#load_groupsObject



116
117
118
119
120
121
122
123
# File 'lib/sinject/container.rb', line 116

def load_groups
  Sinject::DependencyGroup.descendants.each do |g|
    group = g.new
    if group.is_valid?
      group.register(self)
    end
  end
end

#register(options = {}, &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:

>> Sinject::Container.instance.register { :key => :object_key, :class => ClassName, :singleton => true, :contract => 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/sinject/container.rb', line 40

def register(options = {}, &initialize_block)

  if(!options.has_key?(:key))
    raise Sinject::DependencyRegistrationKeyNotSpecifiedException.new
  end

  if(!options.has_key?(:class))
    raise Sinject::DependencyRegistrationClassNotSpecifiedException.new
  end

  key = options[:key]
  dependency_class_name = options[:class]

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

  single_instance = false
  contract_class_name = nil

  if options != nil && options[:singleton] == true
    single_instance = true
  end

  if options != nil && options[:contract] != nil
    contract_class_name = options[:contract]
  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 = Sinject::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