Class: Substation::DSL::Registry
- Inherits:
-
Object
- Object
- Substation::DSL::Registry
- Includes:
- Enumerable
- Defined in:
- lib/substation/dsl/registry.rb
Overview
A mutable registry for objects collected with DSL classes
Class Method Summary collapse
-
.coerce_name(name) ⇒ Symbol
private
Coerce
name
into a Symbol.
Instance Method Summary collapse
-
#[]=(name, object) ⇒ Object
private
Register
object
byname
. -
#each(&block) {|name, object| ... } ⇒ self
private
Iterate over all entries.
-
#fetch(name, &block) ⇒ Object
private
Return the object registered by
name
or the value returned fromblock
. -
#include?(name) ⇒ Boolean
private
Test wether an object is registered by
name
. -
#initialize(guard, entries = EMPTY_HASH) ⇒ undefined
constructor
private
Initialize a new instance.
-
#keys ⇒ Array<Symbol>
private
Return all names by which objects are registered.
-
#merge(other) ⇒ Registry
private
Return a new instance with
other
merged in.
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
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
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
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
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
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
129 130 131 |
# File 'lib/substation/dsl/registry.rb', line 129 def include?(name) entries.include?(coerce_name(name)) end |
#keys ⇒ Array<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
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
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 |