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
# File 'lib/sinject/container.rb', line 9

def initialize(singleton=true)
  @store = {}
  Sinject::Container.instance = self if singleton
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)


86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/sinject/container.rb', line 86

def get(key)
  # get the dependency from the container store for the specified key
  item = @store[key]
  if !item.nil?
    # 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
      item.instance = create_instance(item) if item.instance.nil?

      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 "[#{self.class}] - 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

@deprecated: Use registered? method instead

Returns:

  • (Boolean)


26
27
28
29
# File 'lib/sinject/container.rb', line 26

def is_registered?(key)
  puts "[#{self.class}] - #is_registered? method is deprecated please use #registered? instead."
  registered?(key)
end

#load_groupsObject



109
110
111
112
113
114
115
116
# File 'lib/sinject/container.rb', line 109

def load_groups
  Sinject::DependencyGroup.descendants.sort_by(&:name).each do |g|
    group = g.new
    if (group.respond_to?(:valid?) && group.valid?) || (group.respond_to?(:is_valid?) && 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)


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
# File 'lib/sinject/container.rb', line 43

def register(options = {}, &initialize_block)
  raise Sinject::DependencyRegistrationKeyNotSpecifiedException.new unless options.has_key?(:key)

  raise Sinject::DependencyRegistrationClassNotSpecifiedException.new unless options.has_key?(:class)

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

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

  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

  # Validate the dependency class against the contract if a contract has been specified
  validate_contract(dependency_class_name, contract_class_name) unless contract_class_name.nil?

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

  @store[item.key] = item
  true
end

#registered?(key) ⇒ Boolean

Check if an object has been registered with the container.

Example:

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

Arguments:

key:  (Symbol)

Returns:

  • (Boolean)


22
23
24
# File 'lib/sinject/container.rb', line 22

def registered?(key)
  @store.has_key?(key)
end