Class: Micon::Metadata

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

Overview

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(registry) ⇒ Metadata

Returns a new instance of Metadata.



5
6
7
8
# File 'lib/micon/metadata.rb', line 5

def initialize registry
  @registry = registry
  @before, @after, @before_scope, @after_scope, @initializers = {}, {}, {}, {}, {}
end

Instance Attribute Details

#afterObject

Returns the value of attribute after.



3
4
5
# File 'lib/micon/metadata.rb', line 3

def after
  @after
end

#beforeObject

Returns the value of attribute before.



3
4
5
# File 'lib/micon/metadata.rb', line 3

def before
  @before
end

#initializersObject

Returns the value of attribute initializers.



3
4
5
# File 'lib/micon/metadata.rb', line 3

def initializers
  @initializers
end

#registryObject

Returns the value of attribute registry.



3
4
5
# File 'lib/micon/metadata.rb', line 3

def registry
  @registry
end

Instance Method Details

#[](key) ⇒ Object

Registry.



41
# File 'lib/micon/metadata.rb', line 41

def [] key; @registry[key] end

#call_after(key, object) ⇒ Object



62
63
64
65
66
# File 'lib/micon/metadata.rb', line 62

def call_after key, object
  if callbacks = @after[key]
    callbacks.each{|c| c.call(object)}
  end
end

#call_after_scope(key, container) ⇒ Object



86
87
88
89
90
# File 'lib/micon/metadata.rb', line 86

def call_after_scope key, container
  if callbacks = @after_scope[key]
    callbacks.each{|c| c.call container}
  end
end

#call_before(key) ⇒ Object



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

def call_before key
  if callbacks = @before[key]
    callbacks.each{|c| c.call}
  end
end

#call_before_scope(key, container) ⇒ Object



80
81
82
83
84
# File 'lib/micon/metadata.rb', line 80

def call_before_scope key, container
  if callbacks = @before_scope[key]
    callbacks.each{|c| c.call container}
  end
end

#clearObject



10
11
12
13
14
15
16
17
# File 'lib/micon/metadata.rb', line 10

def clear
  @registry.clear
  @initializers.clear
  @before.clear
  @after.clear
  @before_scope.clear
  @after_scope.clear
end

#cloneObject Also known as: deep_clone



28
29
30
31
32
33
34
35
# File 'lib/micon/metadata.rb', line 28

def clone
  another = super
  %w(@registry @before @after @before_scope @after_scope @initializers).each do |name|
    value = instance_variable_get name
    another.instance_variable_set name, value.clone
  end
  another
end

#delete(key) ⇒ Object



19
20
21
22
23
24
25
26
# File 'lib/micon/metadata.rb', line 19

def delete key
  @registry.delete key
  @initializers.delete key
  @before.delete key
  @after.delete key
  @before_scope.delete key
  @after_scope.delete key
end

#include?(key) ⇒ Boolean

Returns:

  • (Boolean)


42
# File 'lib/micon/metadata.rb', line 42

def include? key; @registry.include? key end

#register_after(key, &block) ⇒ Object



51
52
53
54
# File 'lib/micon/metadata.rb', line 51

def register_after key, &block
  raise "you should provide block!" unless block
  (@after[key] ||= []) << block
end

#register_after_scope(key, &block) ⇒ Object



75
76
77
78
# File 'lib/micon/metadata.rb', line 75

def register_after_scope key, &block
  raise "you should provide block!" unless block
  (@after_scope[key] ||= []) << block
end

#register_before(key, &block) ⇒ Object

Callbacks.



46
47
48
49
# File 'lib/micon/metadata.rb', line 46

def register_before key, &block
  raise "you should provide block!" unless block
  (@before[key] ||= []) << block
end

#register_before_scope(key, &block) ⇒ Object

Scope callbacks.



70
71
72
73
# File 'lib/micon/metadata.rb', line 70

def register_before_scope key, &block
  raise "you should provide block!" unless block
  (@before_scope[key] ||= []) << block
end

#with_scope_callbacks(key, container, &block) ⇒ Object



92
93
94
95
96
97
# File 'lib/micon/metadata.rb', line 92

def with_scope_callbacks key, container, &block
  call_before_scope key, container
  result = block.call
  call_after_scope key, container
  result
end