Module: RConfig::Callbacks

Included in:
RConfig
Defined in:
lib/rconfig/callbacks.rb

Instance Method Summary collapse

Instance Method Details

#fire_on_load(name) ⇒ Object

Executes all of the reload callbacks registered to the specified config name, and all of the callbacks registered to run on any config, as specified by the :ANY symbol.



38
39
40
41
42
43
# File 'lib/rconfig/callbacks.rb', line 38

def fire_on_load(name)
  procs = (self.callbacks['ANY'] || RConfig::EMPTY_ARRAY) + (self.callbacks[name] || RConfig::EMPTY_ARRAY)
  procs.uniq!
  logger.debug "fire_on_load(#{name.inspect}): callbacks[#{procs.inspect}]" unless procs.empty?
  procs.each { |proc| proc.call() }
end

#on_load(*args, &blk) ⇒ Object

Register a callback when a config has been reloaded. If no config name is specified, the callback will be registered under the name :ANY. The name :ANY will register a callback for any config file change.

Example:

class MyClass
  self.my_config = { }
  RConfig.on_load(:cache) do
    self.my_config = { }
  end
  def my_config
    self.my_config ||= something_expensive_thing_on_config(RConfig.cache.memory_limit)
  end
end


21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/rconfig/callbacks.rb', line 21

def on_load(*args, &blk)
  args << :ANY if args.empty?
  proc = blk.to_proc

  # Call proc on registration.
  proc.call()

  # Register callback proc.
  args.each do |name|
    (self.callbacks[name.to_s] ||= []) << proc
  end
end