Module: Factrey

Defined in:
lib/factrey.rb,
lib/factrey/dsl.rb,
lib/factrey/ref.rb,
lib/factrey/proxy.rb,
lib/factrey/version.rb,
lib/factrey/blueprint.rb,
lib/factrey/ref/defer.rb,
lib/factrey/ref/builder.rb,
lib/factrey/ref/resolver.rb,
lib/factrey/blueprint/node.rb,
lib/factrey/blueprint/type.rb,
lib/factrey/ref/shorthand_methods.rb,
lib/factrey/blueprint/instantiator.rb

Overview

Factrey provides a declarative DSL to represent the creation plan of objects.

Defined Under Namespace

Classes: Blueprint, DSL, Proxy, Ref

Constant Summary collapse

VERSION =
"0.4.0"

Class Method Summary collapse

Class Method Details

.blueprint(blueprint = nil, ext: nil, dsl: DSL) { ... } ⇒ Blueprint

Entry point to build or extend a Blueprint.

Examples:

bp =
  Factrey.blueprint do
    let.blog do
      article(title: "Article 1", body: "...")
      article(title: "Article 2", body: "...")
      article(title: "Article 3", body: "...") do
        comment(name: "John", body: "...")
        comment(name: "Doe", body: "...")
      end
    end
  end

instance = bp.instantiate
# This creates...
# - a blog (can be accessed by `instance[:blog]`)
# - with three articles
# - and two comments to the last article

Parameters:

  • blueprint (Blueprint, nil) (defaults to: nil)

    to extend an existing blueprint

  • ext (Object) (defaults to: nil)

    an external object that can be accessed using Factrey::DSL#ext in the DSL

  • dsl (Class<DSL>) (defaults to: DSL)

    which DSL is used

Yields:

  • Write Blueprint DSL code here. See DSL methods for DSL details

Returns:

  • (Blueprint)

    the built or extended blueprint

Raises:

  • (TypeError)


36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/factrey.rb', line 36

def blueprint(blueprint = nil, ext: nil, dsl: DSL, &)
  raise TypeError, "blueprint must be a Blueprint" if blueprint && !blueprint.is_a?(Blueprint)
  raise TypeError, "dsl must be a subclass of DSL" unless dsl <= DSL

  is_extending = !blueprint.nil?
  blueprint ||= Blueprint.new

  result = block_given? ? dsl.new(blueprint:, ext:).instance_eval(&) : nil
  blueprint.add_node(Blueprint::Node.computed(Blueprint::Node::RESULT_NAME, result)) unless is_extending

  blueprint
end