class Hookit::Registry

Register components in a single location that can be queried.

This allows certain components (such as guest systems, configuration pieces, etc.) to be registered and queried.

Public Class Methods

new() click to toggle source
# File lib/hookit/registry.rb, line 7
def initialize
  @actions = {}
  @results_cache = {}
end

Public Instance Methods

[](key)
Alias for: get
each() { |key, get(key)| ... } click to toggle source

Iterate over the keyspace.

# File lib/hookit/registry.rb, line 37
def each(&block)
  @actions.each do |key, _|
    yield key, get(key)
  end
end
get(key) click to toggle source

Get an action by the given key.

This will evaluate the block given to `register` and return the resulting action stack.

# File lib/hookit/registry.rb, line 29
def get(key)
  return nil unless @actions.has_key?(key)
  return @results_cache[key] if @results_cache.has_key?(key)
  @results_cache[key] = @actions[key].call
end
Also aliased as: []
register(key, value=nil, &block) click to toggle source

Register a callable by key.

The callable should be given in a block which will be lazily evaluated when the action is needed.

If an action by the given name already exists then it will be overwritten.

# File lib/hookit/registry.rb, line 19
def register(key, value=nil, &block)
  @results_cache.delete key
  block = lambda { value } unless value.nil?
  @actions[key] = block
end
to_hash() click to toggle source

Converts this registry to a hash

# File lib/hookit/registry.rb, line 44
def to_hash
  result = {}
  self.each do |key, value|
    result[key] = value
  end

  result
end