Sha256: f58d3d0a1dd450592ac3749ae992efdd68f6a1973c8c445a04d99a127c38a11d

Contents?: true

Size: 1.18 KB

Versions: 3

Compression:

Stored size: 1.18 KB

Contents

# frozen_string_literal: true

module Asciidoctor::DocTest
  module Factory

    ##
    # Registers the given class in the factory under the specified name.
    #
    # @param name [#to_sym] the name under which to register the class.
    # @param klass [Class] the class to register.
    # @param default_opts [Hash] default options to be passed into the class'
    #        initializer. May be overriden by +opts+ passed to {.create}.
    # @return [self]
    #
    def register(name, klass, default_opts = {})
      @factory_registry ||= {}
      @factory_registry[name.to_sym] = ->(opts) { klass.new(default_opts.merge(opts)) }
      self
    end

    ##
    # @param name [#to_sym] name of the class to create.
    # @param opts [Hash] options to be passed into the class' initializer.
    # @return [Object] a new instance of the class registered under the
    #   specified name.
    # @raise ArgumentError if no class was found for the given name.
    #
    def create(name, opts = {})
      @factory_registry ||= {}

      if (obj = @factory_registry[name.to_sym])
        obj.call(opts)
      else
        fail ArgumentError, "No class registered with name: #{name}"
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
asciidoctor-doctest-2.0.0.beta.6 lib/asciidoctor/doctest/factory.rb
asciidoctor-doctest-2.0.0.beta.5 lib/asciidoctor/doctest/factory.rb
asciidoctor-doctest-2.0.0.beta.4 lib/asciidoctor/doctest/factory.rb