Class: Substation::DSL::Registry

Inherits:
Object
  • Object
show all
Defined in:
lib/substation/dsl/registry.rb

Overview

A mutable registry for objects collected with DSL classes

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(guard, items = EMPTY_HASH) ⇒ undefined

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Initialize a new instance

Parameters:

  • guard (Guard)

    the guard to use for rejecting invalid entries

  • items (Hash<Symbol, Object>) (defaults to: EMPTY_HASH)

    the items this registry stores



35
36
37
# File 'lib/substation/dsl/registry.rb', line 35

def initialize(guard, items = EMPTY_HASH)
  super(guard, items.dup)
end

Class Method Details

.coerce_name(name) ⇒ Symbol

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Coerce name into a Symbol

Parameters:

  • name (#to_sym)

    the name to coerce

Returns:

  • (Symbol)


20
21
22
# File 'lib/substation/dsl/registry.rb', line 20

def self.coerce_name(name)
  name.to_sym
end

Instance Method Details

#[]=(name, object) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Register object by name

Parameters:

  • name (#to_sym)

    the name to register object with

  • object (Object)

    the object to register by name

Returns:

  • (Object)

    the registered object

Raises:



76
77
78
79
80
# 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) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

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

Parameters:

  • name (#to_sym)

    the name of the object to fetch

  • block (Proc)

    the block to invoke if no object is registered by name

Returns:

  • (Object)


106
107
108
# File 'lib/substation/dsl/registry.rb', line 106

def fetch(name, &block)
  items.fetch(coerce_name(name), &block)
end

#include?(name) ⇒ Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Test wether an object is registered by name

Parameters:

  • name (#to_sym)

    the name to test

Returns:

  • (Boolean)

    true if an object is registered, false otherwise



91
92
93
# File 'lib/substation/dsl/registry.rb', line 91

def include?(name)
  items.include?(coerce_name(name))
end

#keysArray<Symbol>

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Return all names by which objects are registered

Returns:

  • (Array<Symbol>)


115
116
117
# File 'lib/substation/dsl/registry.rb', line 115

def keys
  items.keys
end

#merge(other) ⇒ Registry

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Return a new instance with other merged in

Parameters:

  • other (Registry)

    the registry to merge

Returns:

  • (Registry)

    the new, merged instance

Raises:



52
53
54
55
56
# File 'lib/substation/dsl/registry.rb', line 52

def merge(other)
  other.each_with_object(new) { |(name, object), merged|
    merged[name] = object
  }
end