Class: Substation::DSL::Registry

Inherits:
Object
  • Object
show all
Includes:
Enumerable
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, entries = 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

  • entires (Hash<Symbol, Object>)

    the entries this registry stores



51
52
53
# File 'lib/substation/dsl/registry.rb', line 51

def initialize(guard, entries = EMPTY_HASH)
  @guard, @entries = guard, entries.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:



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

def []=(name, object)
  coerced_name = coerce_name(name)
  guard.call(coerced_name, entries)
  entries[coerced_name] = object
end

#each(&block) {|name, object| ... } ⇒ self

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.

Iterate over all entries

Parameters:

  • block (Proc)

    the block passed to ##entries.each

Yields:

  • (name, object)

Yield Parameters:

  • name (Symbol)

    the name of the current entry

  • object (Object)

    the object registered by name

Returns:

  • (self)


71
72
73
74
75
# File 'lib/substation/dsl/registry.rb', line 71

def each(&block)
  return to_enum unless block
  entries.each(&block)
  self
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)


144
145
146
# File 'lib/substation/dsl/registry.rb', line 144

def fetch(name, &block)
  entries.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



129
130
131
# File 'lib/substation/dsl/registry.rb', line 129

def include?(name)
  entries.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>)


153
154
155
# File 'lib/substation/dsl/registry.rb', line 153

def keys
  entries.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:



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

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