Class: Dim::Container

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

Overview

Dim::Container is the central data store for registering services used for dependency injuction. Users register services by providing a name and a block used to create the service. Services may be retrieved by asking for them by name (via the [] operator) or by selector (via the method_missing technique).

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(parent = nil) ⇒ Container

Create a dependency injection container. Specify a parent container to use as a fallback for service lookup.



51
52
53
54
55
# File 'lib/dim.rb', line 51

def initialize(parent=nil)
  @services = {}
  @cache = {}
  @parent = parent || Container
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(sym, *args, &block) ⇒ Object

Lookup a service by message selector. A service with the same name as sym will be returned, or an exception is thrown if no matching service is found.



108
109
110
# File 'lib/dim.rb', line 108

def method_missing(sym, *args, &block)
  self[sym]
end

Instance Attribute Details

#parentObject (readonly)

Returns the value of attribute parent.



47
48
49
# File 'lib/dim.rb', line 47

def parent
  @parent
end

Class Method Details

.service_block(name) ⇒ Object

Searching for a service block only reaches the Container class when all the containers in the hierarchy search chain have no entry for the service. In this case, the only thing to do is signal a failure.



128
129
130
# File 'lib/dim.rb', line 128

def self.service_block(name)
  fail(MissingServiceError, "Unknown Service '#{name}'")
end

Instance Method Details

#[](name) ⇒ Object

Lookup a service by name. Throw an exception if no service is found.



101
102
103
# File 'lib/dim.rb', line 101

def [](name)
  @cache[name] ||= service_block(name).call(self)
end

#clear_cache!Object

Resets the cached services



120
121
122
# File 'lib/dim.rb', line 120

def clear_cache!
  @cache = {}
end

#override(name, &block) ⇒ Object



71
72
73
# File 'lib/dim.rb', line 71

def override(name,&block)
  register(name,false,&block)
end

#register(name, raise_error_on_duplicate = true, &block) ⇒ Object

Register a service named name. The block will be used to create the service on demand. It is recommended that symbols be used as the name of a service.



60
61
62
63
64
65
66
67
68
69
# File 'lib/dim.rb', line 60

def register(name,raise_error_on_duplicate = true,&block)
  if @services[name] && raise_error_on_duplicate
    fail DuplicateServiceError, "Duplicate Service Name '#{name}'"
  end
  @services[name] = block

  self.class.send(:define_method, name) do
    self[name]
  end
end

#register_env(name, default = nil) ⇒ Object

Lookup a service from ENV variables, or use a default if given; fall back to searching the container and its parents for a default value



85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/dim.rb', line 85

def register_env(name,default = nil)
  if value = ENV[name.to_s.upcase]
    register(name) { value }
  elsif default
    register(name) { default }
  else
    begin
      @parent.service_block(name)
    rescue MissingServiceError
      raise EnvironmentVariableNotFound, "Could not find an ENV variable named '#{name.to_s.upcase}' nor could we find a service named #{name} in the parent container"
    end
  end
end

#service_block(name) ⇒ Object

Return the block that creates the named service. Throw an exception if no service creation block of the given name can be found in the container or its parents.



115
116
117
# File 'lib/dim.rb', line 115

def service_block(name)
  @services[name] || @parent.service_block(name)
end

#verify_dependencies(*names) ⇒ Object

Given a list of services, check to see if they are available, returning true or false.



76
77
78
79
80
81
82
# File 'lib/dim.rb', line 76

def verify_dependencies(*names)
  names.all? do |name|
    respond_to?(name) || service_block(name)
  end
rescue Dim::MissingServiceError
  false
end