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.

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.


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