Class: Micon

Inherits:
Object show all
Defined in:
lib/micon/micon.rb,
lib/micon/managed.rb

Overview

inherit Micon::Managed

scope [:session | :thread | :instance | :application | :<custom>

inject :attribute => Session

inject :attribute => [:scope, :variable]

Defined Under Namespace

Modules: Managed

Constant Summary collapse

REGISTRY_SYNC =
Monitor.new
APPLILCATION_SYNC =
Monitor.new

Class Method Summary collapse

Class Method Details

.[](key) ⇒ Object

Object Management



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/micon/micon.rb', line 65

def [] 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
    return initializer.call
  when :application
    APPLILCATION_SYNC.synchronize do
      o = @application[key]
      unless o
        o = initializer.call
        @application[key] = o
      end
      return o
    end
  else # custom
    container = Thread.current[scope]
    raise_without_self "Scope '#{scope}' not started!" unless container
    o = container[key]
    unless o
      o = initializer.call
      container[key] = o
    end
    return o
  end
end

.[]=(key, value) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/micon/micon.rb', line 95

def []= key, value
  scope = nil
  REGISTRY_SYNC.synchronize{scope, initializer = @registry[key]}
  
  case scope
  when nil
    raise_without_self "The '#{key}' Name is not Managed!", Micon
  when :instance
    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
    container[key] = value
  end
end

.activate(scope, container, &block) ⇒ Object

Scope Management



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/micon/micon.rb', line 17

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]
  
  if block
    begin          
      Thread.current[scope_with_prefix] = container
      block.call
    ensure
      deactivate scope
    end
  else        
    Thread.current[scope_with_prefix] = container
  end
end

.active?(scope) ⇒ Boolean

Returns:

  • (Boolean)


46
47
48
49
50
51
52
# File 'lib/micon/micon.rb', line 46

def active? scope
  if scope == :application or scope == :instance
    true
  else
    Thread.current.key?(add_prefix(scope))
  end      
end

.clearObject



54
55
56
57
58
59
# File 'lib/micon/micon.rb', line 54

def clear      
  APPLILCATION_SYNC.synchronize{@application.clear}
  Thread.current.keys.each do |key|
    Thread.current[key] = nil if key.to_s =~ /^mc_/
  end
end

.clear_registryObject

Registry



117
118
119
# File 'lib/micon/micon.rb', line 117

def clear_registry
  REGISTRY_SYNC.synchronize{@registry.clear}
end

.deactivate(scope) ⇒ Object



36
37
38
39
40
41
42
43
44
# File 'lib/micon/micon.rb', line 36

def deactivate scope
  raise_without_self "Only custom scopes can be deactivated!" if scope == :application or scope == :instance
  
  scope_with_prefix = add_prefix(scope)      
  raise_without_self "Scope '#{scope}' not active!" unless container = Thread.current[scope_with_prefix]
  
  Thread.current[scope_with_prefix] = nil      
  container
end

.include?(key) ⇒ Boolean

Returns:

  • (Boolean)


138
139
140
# File 'lib/micon/micon.rb', line 138

def include? key
  REGISTRY_SYNC.synchronize{@registry.include? key}
end

.register(key, scope, &initializer) ⇒ Object



121
122
123
124
125
126
127
128
# File 'lib/micon/micon.rb', line 121

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
end

.registry_get(key) ⇒ Object



134
135
136
# File 'lib/micon/micon.rb', line 134

def registry_get key
  REGISTRY_SYNC.synchronize{@registry[key]}
end

.unregister(key) ⇒ Object



130
131
132
# File 'lib/micon/micon.rb', line 130

def unregister key
  REGISTRY_SYNC.synchronize{@registry.delete key}
end