Module: Asciidoctor::DocTest::Factory

Included in:
IO
Defined in:
lib/asciidoctor/doctest/factory.rb

Instance Method Summary collapse

Instance Method Details

#create(name, opts = {}) ⇒ Object

Returns a new instance of the class registered under the specified name.

Parameters:

  • name (#to_sym)

    name of the class to create.

  • opts (Hash) (defaults to: {})

    options to be passed into the class' initializer.

Returns:

  • (Object)

    a new instance of the class registered under the specified name.

Raises:

  • ArgumentError if no class was found for the given name.


28
29
30
31
32
33
34
35
36
# File 'lib/asciidoctor/doctest/factory.rb', line 28

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

#register(name, klass, default_opts = {}) ⇒ self

Registers the given class in the factory under the specified name.

Parameters:

  • name (#to_sym)

    the name under which to register the class.

  • klass (Class)

    the class to register.

  • default_opts (Hash) (defaults to: {})

    default options to be passed into the class' initializer. May be overriden by opts passed to #create.

Returns:

  • (self)

15
16
17
18
19
# File 'lib/asciidoctor/doctest/factory.rb', line 15

def register(name, klass, default_opts = {})
  @factory_registry ||= {}
  @factory_registry[name.to_sym] = ->(opts) { klass.new(default_opts.merge(opts)) }
  self
end