class Substation::DSL::Registry

A mutable registry for objects collected with DSL classes

Public Class Methods

coerce_name(name) click to toggle source

Coerce name into a Symbol

@param [#to_sym] name

the name to coerce

@return [Symbol]

@api private

# File lib/substation/dsl/registry.rb, line 20
def self.coerce_name(name)
  name.to_sym
end
new(guard, items = EMPTY_HASH) click to toggle source

Initialize a new instance

@param [Guard] guard

the guard to use for rejecting invalid entries

@param [Hash<Symbol, Object>] items

the items this registry stores

@return [undefined]

@api private

Calls superclass method
# File lib/substation/dsl/registry.rb, line 35
def initialize(guard, items = EMPTY_HASH)
  super(guard, items.dup)
end

Public Instance Methods

[]=(name, object) click to toggle source

Register object by name

@param [#to_sym] name

the name to register object with

@param [Object] object

the object to register by +name+

@raise [AlreadyRegisteredError]

if +object+ is already registered by the same +name+

@raise [ReservedNameError]

if +object+ should be registered using a reserved +name+

@return [Object]

the registered object

@api private

# File lib/substation/dsl/registry.rb, line 76
def []=(name, object)
  coerced_name = coerce_name(name)
  guard.call(coerced_name, items)
  items[coerced_name] = object
end
fetch(name, &block) click to toggle source

Return the object registered by name or the value returned from block

@param [#to_sym] name

the name of the object to fetch

@param [Proc] block

the block to invoke if no object is registered by +name+

@return [Object]

@api private

# File lib/substation/dsl/registry.rb, line 106
def fetch(name, &block)
  items.fetch(coerce_name(name), &block)
end
include?(name) click to toggle source

Test wether an object is registered by name

@param [#to_sym] name

the name to test

@return [Boolean]

true if an object is registered, false otherwise

@api private

# File lib/substation/dsl/registry.rb, line 91
def include?(name)
  items.include?(coerce_name(name))
end
keys() click to toggle source

Return all names by which objects are registered

@return [Array<Symbol>]

@api private

# File lib/substation/dsl/registry.rb, line 115
def keys
  items.keys
end
merge(other) click to toggle source

Return a new instance with other merged in

@param [Registry] other

the registry to merge

@raise [AlreadyRegisteredError]

if any object in +other+ is already registered by the same
name in +self+

@return [Registry]

the new, merged instance

@api private

# File lib/substation/dsl/registry.rb, line 52
def merge(other)
  other.each_with_object(new) { |(name, object), merged|
    merged[name] = object
  }
end

Private Instance Methods

coerce_name(name) click to toggle source

Coerce name into a Symbol

@param [#to_sym] name

the name to coerce

@return [Symbol]

@api private

# File lib/substation/dsl/registry.rb, line 129
def coerce_name(name)
  self.class.coerce_name(name)
end
new() click to toggle source

Return a new instance with {#guard} and {#entries}

@return [Registry]

@api private

# File lib/substation/dsl/registry.rb, line 138
def new
  self.class.new(guard, items)
end