Module: ObjectForge

Defined in:
lib/object_forge.rb,
lib/object_forge/forge.rb,
lib/object_forge/molds.rb,
lib/object_forge/version.rb,
lib/object_forge/crucible.rb,
lib/object_forge/sequence.rb,
lib/object_forge/forge_dsl.rb,
lib/object_forge/forgeyard.rb,
lib/object_forge/molds/hash_mold.rb,
lib/object_forge/molds/mold_mold.rb,
lib/object_forge/un_basic_object.rb,
lib/object_forge/molds/struct_mold.rb,
lib/object_forge/molds/wrapped_mold.rb,
lib/object_forge/molds/keywords_mold.rb,
lib/object_forge/molds/single_argument_mold.rb

Overview

A simple all-purpose factory library with minimal assumptions.

These are the main classes you should be aware of:

  • Forgeyard is a registry of named related Forges. A Forgeyard allows to Forgeyard#define a Forge, and Forgeyard#forge a new object using a defined Forge.

  • Forge is a factory for objects. Usually created through Forgeyard#define/Forge.define in a manner similar to FactoryBot, Forges can be used standalone, or as a part of a Forgeyard.

  • Sequence is a representation of a sequence of values. They are usually used implicitly through ForgeDSL#sequence, but can be created explicitly to be shared (or used outside of ObjectForge).

Additionally, successful use may depend on understanding these:

  • ForgeDSL is a block-based DSL inspired by FactoryBot and ROM::Factory. It allows defining arbitrary attributes (possibly using sequences), with support for traits (collections of attributes with non-default values).

  • Crucible is used to resolve attributes.

Examples:

Quick example

Frobinator = Struct.new(:frob, :inator, keyword_init: true)
# Forge's name and forged class are completely independent.
ObjectForge.define(:frobber, Frobinator) do |f|
  f.frob { "Frob" + inator.call }
  f.inator { -> { "inator" } }
  f.trait :static do |tf|
    tf.frob { "Static" }
  end
end
# These methods are aliases:
ObjectForge.forge(:frobber)
# => #<struct Frobinator frob="Frobinator", inator=#<Proc:...>>
ObjectForge.build(:frobber, frob: -> { "Frob" + inator }, inator: "orn")
# => #<struct Frobinator frob="Froborn", inator="orn">
ObjectForge[:frobber, :static, inator: "Value"]
# => #<struct Frobinator frob="Static", inator="Value">

Defined Under Namespace

Modules: Molds Classes: Crucible, DSLError, Error, Forge, ForgeDSL, Forgeyard, Sequence, UnBasicObject

Constant Summary collapse

DEFAULT_YARD =
Note:

Default forgeyard is intended to be useful for non-shareable code, like simple application tests and specs. It should not be used in application code, and never in gems.

Default Forgeyard that is used by define and forge.

Since:

  • 0.1.0

Forgeyard.new
VERSION =

Current version

"0.2.0"

Class Method Summary collapse

Class Method Details

.define(name, forged, &) ⇒ Forge

Note:

Default forgeyard is intended to be useful for non-shareable code, like simple application tests and specs. It should not be used in application code, and never in gems.

Define and create a forge in DEFAULT_YARD.

Parameters:

  • name (Symbol)

    forge name

  • forged (Class, Any)

    class or object to forge

Yield Parameters:

Yield Returns:

  • (void)

Returns:

See Also:

Since:

  • 0.1.0



85
86
87
# File 'lib/object_forge.rb', line 85

def self.define(...)
  DEFAULT_YARD.define(...)
end

.forge(name, *traits, **overrides, &) ⇒ Any Also known as: build, []

Note:

Default forgeyard is intended to be useful for non-shareable code, like simple application tests and specs. It should not be used in application code, and never in gems.

Build an instance using a forge from DEFAULT_YARD.

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

See Also:

Since:

  • 0.1.0



102
103
104
# File 'lib/object_forge.rb', line 102

def self.forge(...)
  DEFAULT_YARD.forge(...)
end

.sequence(initial) ⇒ Sequence

Create a sequence, to be used wherever it needs to be.

Parameters:

Returns:

See Also:

Since:

  • 0.1.0



69
70
71
# File 'lib/object_forge.rb', line 69

def self.sequence(...)
  Sequence.new(...)
end