lib/dim.rb in dim-1.2.7 vs lib/dim.rb in dim-1.2.8
- old
+ new
@@ -56,13 +56,18 @@
# 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.
def register(name,raise_error_on_duplicate = true,&block)
- if @services[name] && raise_error_on_duplicate
- fail DuplicateServiceError, "Duplicate Service Name '#{name}'"
+ if @services[name]
+ if raise_error_on_duplicate
+ fail DuplicateServiceError, "Duplicate Service Name '#{name}'"
+ else # delete the service from the cache
+ @cache.delete(name)
+ end
end
+
@services[name] = block
self.class.send(:define_method, name) do
self[name]
end
@@ -70,19 +75,10 @@
def override(name,&block)
register(name,false,&block)
end
- # Given a list of services, check to see if they are available, returning true or false.
- def verify_dependencies(*names)
- names.all? do |name|
- respond_to?(name) || service_block(name)
- end
- rescue Dim::MissingServiceError
- false
- end
-
# 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
def register_env(name,default = nil)
if value = ENV[name.to_s.upcase]
register(name) { value }
elsif default
@@ -125,8 +121,29 @@
# 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.
def self.service_block(name)
fail(MissingServiceError, "Unknown Service '#{name}'")
+ end
+
+ # Check to see if a custom method or service has been registered, returning true or false.
+ def service_exists?(name)
+ respond_to?(name) || service_block(name)
+ rescue Dim::MissingServiceError
+ false
+ end
+
+ # Given a list of services, check to see if they are available, returning true or false.
+ def verify_dependencies(*names)
+ names.all? { |name| service_exists?(name) }
+ end
+
+ # Given a list of services, check to see if they are available or raise an exception.
+ def verify_dependencies!(*names)
+ missing_dependencies = names.reject { |name| service_exists?(name) }
+
+ unless missing_dependencies.empty?
+ fail Dim::MissingServiceError, "Missing dependencies #{missing_dependencies.join(", ")}"
+ end
end
end
end