Class: ObjectForge::Forge
- Inherits:
-
Object
- Object
- ObjectForge::Forge
- Defined in:
- lib/object_forge/forge.rb
Overview
Object instantitation forge.
Defined Under Namespace
Classes: Parameters
Constant Summary collapse
Instance Attribute Summary collapse
-
#forged ⇒ Class, Any
readonly
Class or object to forge.
-
#name ⇒ Symbol?
readonly
Forge name.
-
#parameters ⇒ Parameters, ForgeDSL
readonly
Forge parameters.
Class Method Summary collapse
-
.define(forged, name: nil) {|f| ... } ⇒ Forge
Define (and create) a forge using DSL.
Instance Method Summary collapse
-
#forge(*traits, **overrides) {|object| ... } ⇒ Any
(also: #build, #[])
Forge a new instance.
-
#initialize(forged, parameters, name: nil) ⇒ Forge
constructor
A new instance of Forge.
Constructor Details
#initialize(forged, parameters, name: nil) ⇒ Forge
Returns a new instance of Forge.
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
#forged ⇒ Class, Any (readonly)
Returns class or object to forge.
51 52 53 |
# File 'lib/object_forge/forge.rb', line 51 def forged @forged end |
#name ⇒ Symbol? (readonly)
Returns forge name.
48 49 50 |
# File 'lib/object_forge/forge.rb', line 48 def name @name end |
#parameters ⇒ Parameters, ForgeDSL (readonly)
Returns forge parameters.
54 55 56 |
# File 'lib/object_forge/forge.rb', line 54 def parameters @parameters end |
Class Method Details
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.
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 |