Module: Micon

Defined in:
lib/micon/micon.rb,
lib/micon/managed.rb,
lib/micon/metadata.rb

Overview

This class intentially made using “wired and not clear code”, to provide better performance.

Defined Under Namespace

Modules: Managed Classes: Metadata

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.metadataObject

Returns the value of attribute metadata.



17
18
19
# File 'lib/micon/micon.rb', line 17

def 
  @metadata
end

Class Method Details

.[](key) ⇒ Object



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/micon/micon.rb', line 101

def [] key
  scope = MSYNC.synchronize{@_r[key]}

  case scope
  when nil
    raise_without_self "'#{key}' component not managed!", Micon        
  when :instance        
    return create_object(key)
  when :application
    SYNC.synchronize do
      o = @application[key]          
      unless o
        return create_object(key, @application)
      else
        return o
      end
    end
  else # custom        
    container = Thread.current[scope]
    raise_without_self "Scope '#{remove_prefix(scope)}' not started!" unless container
    o = container[key]
    unless o
      return create_object(key, container) 
    else
      return o
    end
  end
end

.[]=(key, value) ⇒ Object



130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/micon/micon.rb', line 130

def []= key, value
  scope = MSYNC.synchronize{@_r[key]}
  
  case scope
  when nil
    raise_without_self "'#{key}' component not managed!", Micon
  when :instance
    raise_without_self "You can't outject variable with the 'instance' scope!"
  when :application
    SYNC.synchronize{@application[key] = value}
  else # Custom
    container = Thread.current[scope]
    raise_without_self "Scope '#{remove_prefix(scope)}' not started!" unless container
    container[key] = value
  end
end

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

Scope Management



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/micon/micon.rb', line 22

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 '#{remove_prefix(scope)}' already active!" if !block and Thread.current[scope_with_prefix]
  
  if block
    begin
      outer_container_or_nil = Thread.current[scope_with_prefix]
      Thread.current[scope_with_prefix] = container
      @metadata.with_scope_callbacks scope, &block
    ensure
      Thread.current[scope_with_prefix] = outer_container_or_nil
    end
  else        
    # not support nested scopes without block
    Thread.current[scope_with_prefix] = container
    @metadata.call_before_scope scope
  end
end

.active?(scope) ⇒ Boolean

Returns:

  • (Boolean)


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

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

.after(scope_or_component, &block) ⇒ Object



176
177
178
# File 'lib/micon/micon.rb', line 176

def after scope_or_component, &block
  @metadata.register_after scope_or_component, &block
end

.before(scope_or_component, &block) ⇒ Object



172
173
174
# File 'lib/micon/micon.rb', line 172

def before scope_or_component, &block
  @metadata.register_before scope_or_component, &block
end

.clearObject



63
64
65
66
67
68
# File 'lib/micon/micon.rb', line 63

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

.deactivate(scope) ⇒ Object



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

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]
  
  @metadata.call_after_scope scope
  Thread.current[scope_with_prefix] = nil
  container
end

.empty?Boolean

Returns:

  • (Boolean)


70
71
72
73
74
75
76
# File 'lib/micon/micon.rb', line 70

def empty?
  return false unless SYNC.synchronize{@application.empty?}
  Thread.current.keys.each do |key|
    return false if key.to_s =~ /^mc_/
  end
  return true
end

.include?(key) ⇒ Boolean

Object Management

Returns:

  • (Boolean)


82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/micon/micon.rb', line 82

def include? key
  scope = MSYNC.synchronize{@_r[key]}
  
  case scope
  when nil
    raise_without_self "'#{key}' component not managed!", Micon        
  when :instance
    true
  when :application
    SYNC.synchronize do
      @application.include? key
    end
  else # custom
    container = Thread.current[scope]
    return false unless container
    container.include? key
  end
end

.register(key, options = {}, &initializer) ⇒ Object

Metadata



151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/micon/micon.rb', line 151

def register key, options = {}, &initializer
  key.must_not_be.nil
  options = options.symbolize_keys      
  
  scope = options.delete(:scope) || :application
  scope = Micon.add_prefix(scope) unless scope == :application or scope == :instance
  dependencies = Array.wrap(options.delete(:require) || options.delete(:depends_on))
  
  options.each{|key| raise "Unknown option :#{key}!"}
  
  MSYNC.synchronize do
    @_r.object_id.must == @metadata.registry.object_id
    @metadata.registry[key] = scope
    @metadata.initializers[key] = [(initializer || lambda{nil}), dependencies]
  end
end

.swap_metadata(metadata = nil) ⇒ Object

handy method, usually for test purposes



181
182
183
184
185
186
187
188
189
# File 'lib/micon/micon.rb', line 181

def   = nil
   ||= Metadata.new({}, MSYNC)
  old = self.
  
  self. = 
  @_r = .registry
  
  old
end

.unregister(key) ⇒ Object



168
169
170
# File 'lib/micon/micon.rb', line 168

def unregister key
  @metadata.delete key
end