Class: ObjectForge::Forge

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

Overview

Object instantitation forge.

Since:

  • 0.1.0

Defined Under Namespace

Classes: Parameters

Constant Summary collapse

MOLD_MOLD =

Since:

  • 0.1.0

Molds::MoldMold.new.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(forged, parameters, name: nil) ⇒ Forge

Returns a new instance of Forge.

Parameters:

  • forged (Class, Any)

    class or object to forge

  • parameters (Parameters, ForgeDSL)

    forge parameters

  • name (Symbol, nil) (defaults to: nil)

    forge name

Since:

  • 0.1.0



59
60
61
62
63
64
# File 'lib/object_forge/forge.rb', line 59

def initialize(forged, parameters, name: nil)
  @name = name
  @forged = forged
  @parameters = parameters
  @mold = parameters.mold || MOLD_MOLD.call(forged: forged)
end

Instance Attribute Details

#forgedClass, Any (readonly)

Returns class or object to forge.

Returns:

  • (Class, Any)

    class or object to forge

Since:

  • 0.1.0



51
52
53
# File 'lib/object_forge/forge.rb', line 51

def forged
  @forged
end

#nameSymbol? (readonly)

Returns forge name.

Returns:

  • (Symbol, nil)

    forge name

Since:

  • 0.1.0



48
49
50
# File 'lib/object_forge/forge.rb', line 48

def name
  @name
end

#parametersParameters, ForgeDSL (readonly)

Returns forge parameters.

Returns:

Since:

  • 0.1.0



54
55
56
# File 'lib/object_forge/forge.rb', line 54

def parameters
  @parameters
end

Class Method Details

.define(forged, name: nil) {|f| ... } ⇒ Forge

Define (and create) a forge using DSL.

Parameters:

  • forged (Class, Any)

    class or object to forge

  • name (Symbol, nil) (defaults to: nil)

    forge name

Yield Parameters:

Yield Returns:

  • (void)

Returns:

See Also:

Since:

  • 0.1.0



43
44
45
# File 'lib/object_forge/forge.rb', line 43

def self.define(forged, name: nil, &)
  new(forged, ForgeDSL.new(&), name:)
end

Instance Method Details

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

Forge a new instance.

Positional arguments are taken as trait names, keyword arguments as attribute overrides, unless there are exactly two positional arguments: an array and a hash.

All traits and overrides are applied in argument order, with overrides always applied after traits.

If a block is given, forged instance is yielded to it after being built.

Parameters:

  • 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

Since:

  • 0.1.0



87
88
89
90
91
92
# File 'lib/object_forge/forge.rb', line 87

def forge(*traits, **overrides)
  resolved_attributes = resolve_attributes(traits, overrides)
  instance = @mold.call(forged: @forged, attributes: resolved_attributes)
  yield instance if block_given?
  instance
end