lib/micon/micon.rb in ruby-ext-0.4.1 vs lib/micon/micon.rb in ruby-ext-0.4.2
- old
+ new
@@ -13,24 +13,27 @@
class << self
#
# Scope Management
#
def activate scope, container, &block
+
raise_without_self "Only custom scopes can be activated!" if scope == :application or scope == :instance
container.must_be.a Hash
scope_with_prefix = add_prefix(scope)
- raise_without_self "Scope '#{scope}' already active!" if Thread.current[scope_with_prefix]
+ raise_without_self "Scope '#{remove_prefix(scope)}' already active!" if !block and Thread.current[scope_with_prefix]
if block
- begin
+ begin
+ outer_container_or_nil = Thread.current[scope_with_prefix]
Thread.current[scope_with_prefix] = container
block.call
ensure
- deactivate scope
+ Thread.current[scope_with_prefix] = outer_container_or_nil
end
else
+ # not support nested scopes without block
Thread.current[scope_with_prefix] = container
end
end
def deactivate scope
@@ -56,14 +59,42 @@
Thread.current.keys.each do |key|
Thread.current[key] = nil if key.to_s =~ /^mc_/
end
end
+ def empty?
+ return false unless APPLILCATION_SYNC.synchronize{@application.empty?}
+ Thread.current.keys.each do |key|
+ return false if key.to_s =~ /^mc_/
+ end
+ return true
+ end
+
#
# Object Management
#
+ def include? key
+ scope, initializer = nil
+ REGISTRY_SYNC.synchronize{scope, initializer = @registry[key]}
+
+ case scope
+ when nil
+ raise_without_self "The '#{key}' Name is not Managed!", Micon
+ when :instance
+ true
+ when :application
+ APPLILCATION_SYNC.synchronize do
+ @application.include? key
+ end
+ else # custom
+ container = Thread.current[scope]
+ return false unless container
+ container.include? key
+ end
+ end
+
def [] key
scope, initializer = nil
REGISTRY_SYNC.synchronize{scope, initializer = @registry[key]}
case scope
@@ -80,11 +111,11 @@
end
return o
end
else # custom
container = Thread.current[scope]
- raise_without_self "Scope '#{scope}' not started!" unless container
+ raise_without_self "Scope '#{remove_prefix(scope)}' not started!" unless container
o = container[key]
unless o
o = initializer.call
container[key] = o
end
@@ -103,11 +134,11 @@
raise_without_self "You can't outject variable with the 'instance' scope!"
when :application
APPLILCATION_SYNC.synchronize{@application[key] = value}
else # Custom
container = Thread.current[scope]
- raise_without_self "Scope '#{scope}' not started!" unless container
+ raise_without_self "Scope '#{remove_prefix(scope)}' not started!" unless container
container[key] = value
end
end
@@ -120,28 +151,34 @@
def register key, scope, &initializer
key.must_not_be.nil
scope.must_not_be.nil
scope = add_prefix(scope) unless scope == :application or scope == :instance
- REGISTRY_SYNC.synchronize do
- @registry[key] = [scope, (initializer || lambda{nil})]
- end
+ registry_set key, [scope, (initializer || lambda{nil})]
end
def unregister key
REGISTRY_SYNC.synchronize{@registry.delete key}
end
def registry_get key
REGISTRY_SYNC.synchronize{@registry[key]}
end
- def include? key
+ def registry_set key, value
+ REGISTRY_SYNC.synchronize{@registry[key] = value}
+ end
+
+ def registry_include? key
REGISTRY_SYNC.synchronize{@registry.include? key}
end
protected
def add_prefix scope
:"mc_#{scope}"
+ end
+
+ def remove_prefix scope
+ scope.to_s.gsub(/^mc_/, '')
end
end
end
\ No newline at end of file