Class: Sinject::Container
- Inherits:
-
Object
- Object
- Sinject::Container
- Defined in:
- lib/sinject/container.rb
Overview
This is the IOC Container for registering all dependencies an building objects.
Class Attribute Summary collapse
-
.instance ⇒ Object
Returns the value of attribute instance.
Instance Method Summary collapse
-
#get(key) ⇒ Object
Get an object from the container.
-
#initialize(singleton = true) ⇒ Container
constructor
A new instance of Container.
-
#is_registered?(key) ⇒ Boolean
@deprecated: Use registered? method instead.
- #load_groups ⇒ Object
-
#register(options = {}, &initialize_block) ⇒ Object
Register an object with the container.
-
#registered?(key) ⇒ Boolean
Check if an object has been registered with the container.
Constructor Details
Class Attribute Details
.instance ⇒ Object
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
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_groups ⇒ Object
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( = {}, &initialize_block) raise Sinject::DependencyRegistrationKeyNotSpecifiedException.new unless .has_key?(:key) raise Sinject::DependencyRegistrationClassNotSpecifiedException.new unless .has_key?(:class) key = [:key] dependency_class_name = [: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 != nil && [:singleton] == true single_instance = true end if != nil && [:contract] != nil contract_class_name = [: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)
22 23 24 |
# File 'lib/sinject/container.rb', line 22 def registered?(key) @store.has_key?(key) end |