Module: Tap::Signals::ClassMethods

Defined in:
lib/tap/signals/class_methods.rb

Constant Summary collapse

SIGNALS_CLASS =
Configurable::ClassMethods::CONFIGURATIONS_CLASS

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#signal_registryObject (readonly)

A hash of (key, Signal) pairs defining signals available to the class.



10
11
12
# File 'lib/tap/signals/class_methods.rb', line 10

def signal_registry
  @signal_registry
end

Class Method Details

.initialize(base) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/tap/signals/class_methods.rb', line 12

def self.initialize(base)
  unless base.instance_variable_defined?(:@signal_registry)
    base.instance_variable_set(:@signal_registry, SIGNALS_CLASS.new)
  end
  
  unless base.instance_variable_defined?(:@signals)
    base.instance_variable_set(:@signals, nil)
  end
  
  unless base.instance_variable_defined?(:@use_signal_constants)
    base.instance_variable_set(:@use_signal_constants, true)
  end
end

Instance Method Details

#cache_signals(on = true) ⇒ Object

Caches the signals hash so as to improve peformance. Call with on set to false to turn off caching.



54
55
56
57
# File 'lib/tap/signals/class_methods.rb', line 54

def cache_signals(on=true)
  @signals = nil
  @signals = self.signals if on
end

#signalsObject

A hash of (key, Signal) pairs representing all signals defined on this class or inherited from ancestors. The signals hash is generated on each call to ensure it accurately reflects any signals added on ancestors. This slows down signal calls through instance.signal.

Call cache_signals after all signals have been declared in order to prevent regeneration of signals and to significantly improve performance.



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/tap/signals/class_methods.rb', line 34

def signals
  return @signals if @signals

  signals = SIGNALS_CLASS.new
  ancestors.reverse.each do |ancestor|
    next unless ancestor.kind_of?(ClassMethods)
    ancestor.signal_registry.each_pair do |key, value|
      if value.nil?
        signals.delete(key)
      else
        signals[key] = value
      end
    end
  end

  signals
end