Class: ObjectForge::Forgeyard

Inherits:
Object
  • Object
show all
Defined in:
lib/object_forge/forgeyard.rb

Overview

A registry for forges, making them accessible by name.

Since:

  • 0.1.0

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeForgeyard

Returns a new instance of Forgeyard.

Since:

  • 0.1.0



15
16
17
# File 'lib/object_forge/forgeyard.rb', line 15

def initialize
  @forges = Concurrent::Map.new
end

Instance Attribute Details

#forgesConcurrent::Map{Symbol => Forge} (readonly)

Returns registered forges.

Returns:

  • (Concurrent::Map{Symbol => Forge})

    registered forges

Since:

  • 0.1.0



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.

Parameters:

  • name (Symbol)

    name to register forge under

  • forged (Class, Any)

    class or object to forge

Yield Parameters:

Yield Returns:

  • (void)

Returns:

See Also:

Since:

  • 0.1.0



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.

Parameters:

  • name (Symbol)

    name of the forge

  • traits (Array<Symbol>)

    traits to apply

  • overrides (Hash{Symbol => Any})

    attribute overrides

Yield Parameters:

  • object (Any)

    forged instance

Yield Returns:

  • (void)

Returns:

  • (Any)

    built instance

Raises:

  • (KeyError)

    if forge with the specified name is not registered

See Also:

Since:

  • 0.1.0



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.

Parameters:

  • name (Symbol)

    name to register forge under

  • forge (Forge)

    forge to register

Returns:

  • (Forge)

    actually registered forge

Since:

  • 0.1.0



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