class Leather::Satchel::Container
IOC Container
Class
Public Class Methods
Container
constructor @param values [Hash]
# File lib/leather/satchel.rb, line 12 def initialize(values = {}) @keys = {} @values = {} @cached_services = {} @factories = [] values.each do |key, value| self[key] = value end end
Public Instance Methods
Gets a value, service or factory from the container @param key [String|Symbol] The name of the service to look for @return [Object] The stored service value @raise [Error::UnknownIdentifierError] If the container doesn't contain
the requested key
# File lib/leather/satchel.rb, line 43 def [](key) raise Error::UnknownIdentifierError, key unless @keys.key?(key) service = @values[key] return service.call(self) if @factories.include?(service) if service.is_a?(Proc) return @cached_services[key] if @cached_services.key?(key) value = service.call(self) @cached_services[key] = value return value end service end
Inserts a service into the container under the name @key @param key [String|Symbol] The name of the value, service or factory @param value [Object] The object to store against @key
# File lib/leather/satchel.rb, line 33 def []=(key, value) @keys[key] = true @values[key] = value end
Clears all stored services from the container
# File lib/leather/satchel.rb, line 83 def clear @keys.clear @values.clear @cached_services.clear @factories.clear end
Checks if a given @key exists in the container @param key [String|Symbol] The name we are checking @return [Boolean]
# File lib/leather/satchel.rb, line 26 def contain?(key) @keys.key?(key) end
Defines a factory service @param service [Proc] @return [Proc] @raise [Error::InvalidFactoryError] If the provided factory is not a Proc
# File lib/leather/satchel.rb, line 64 def factory(service) raise Error::InvalidFactoryError, service.class unless service.is_a?(Proc) @factories << service service end
Deletes a stored value, service or factory from the container if it exists @param key [String|Symbol]
# File lib/leather/satchel.rb, line 73 def remove(key) return unless @keys.key?(key) service = @values.delete(key) @factories.delete(service) @keys.delete(key) @cached_services.delete(key) end