Class: ObjectForge::Forgeyard
- Inherits:
-
Object
- Object
- ObjectForge::Forgeyard
- Defined in:
- lib/object_forge/forgeyard.rb
Overview
A registry for forges, making them accessible by name.
Instance Attribute Summary collapse
-
#forges ⇒ Concurrent::Map{Symbol => Forge}
readonly
Registered forges.
Instance Method Summary collapse
-
#define(name, forged) {|f| ... } ⇒ Forge
Define and register a forge in one go.
-
#forge(name) {|object| ... } ⇒ Any
(also: #build, #[])
Build an instance using a forge.
-
#initialize ⇒ Forgeyard
constructor
A new instance of Forgeyard.
-
#register(name, forge) ⇒ Forge
Add a forge under a specified name.
Constructor Details
#initialize ⇒ Forgeyard
Returns a new instance of Forgeyard.
15 16 17 |
# File 'lib/object_forge/forgeyard.rb', line 15 def initialize @forges = Concurrent::Map.new end |
Instance Attribute Details
#forges ⇒ Concurrent::Map{Symbol => Forge} (readonly)
Returns registered forges.
13 14 15 |
# File 'lib/object_forge/forgeyard.rb', line 13 def forges @forges end |
Instance Method Details
#define(name, forged) {|f| ... } ⇒ Forge
Define and register a forge in one go.
29 30 31 |
# File 'lib/object_forge/forgeyard.rb', line 29 def define(name, forged, &) register(name, Forge.define(forged, name: name, &)) end |
#forge(name) {|object| ... } ⇒ Any Also known as: build, []
Build an instance using a forge.
60 61 62 |
# File 'lib/object_forge/forgeyard.rb', line 60 def forge(name, ...) @forges.fetch(name).[](...) end |
#register(name, forge) ⇒ Forge
Add a forge under a specified name.
If name was already taken, new forge will be ignored and existing forge will be returned.
43 44 45 46 |
# File 'lib/object_forge/forgeyard.rb', line 43 def register(name, forge) # `put_if_absent` returns `nil` if there was no previous value, hence the `||`. @forges.put_if_absent(name, forge) || forge end |