Sha256: 039d3f7d8f7ede1ded26521b17f1292b03ebf351bc8683258d7a28190b7b727c

Contents?: true

Size: 1.63 KB

Versions: 2

Compression:

Stored size: 1.63 KB

Contents

# frozen_string_literal: true

require_relative "factrey/version"
require_relative "factrey/ref"
require_relative "factrey/blueprint"
require_relative "factrey/dsl"

# Factrey provides a declarative DSL to represent the creation plan of objects.
module Factrey
  class << self
    # Entry point to build or extend a {Blueprint}.
    # @param blueprint [Blueprint, nil] to extend an existing blueprint
    # @param ext [Object] an external object that can be accessed using {DSL#ext} in the DSL
    # @param dsl [Class<DSL>] which DSL is used
    # @yield Write Blueprint DSL code here. See {DSL} methods for DSL details
    # @return [Blueprint] the built or extended blueprint
    # @example
    #   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
    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

      blueprint ||= Blueprint.new
      blueprint.define_result dsl.new(blueprint:, ext:).instance_exec(&) if block_given?
      blueprint
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
factrey-0.3.0 lib/factrey.rb
factrey-0.2.0 lib/factrey.rb