Module: TypstRails::Backends::Registry

Defined in:
lib/typst_rails/backends/registry.rb

Overview

Registry of available Typst compilation backends, and resolver that picks the right one to use for a given configuration.

Backends are tried in registration order; the first one whose Base#available? returns true wins. This lets TypstRails prefer the typst gem (fast, in-process) when it's installed, and fall back to shelling out to the typst CLI otherwise.

Examples:

Registering a custom backend

TypstRails::Backends::Registry.register(:my_backend, MyBackend.new)
TypstRails.configure { |c| c.backend = :my_backend }

Class Method Summary collapse

Class Method Details

.backendsHash{Symbol => Base}

Returns:

  • (Hash{Symbol => Base})


21
22
23
# File 'lib/typst_rails/backends/registry.rb', line 21

def backends
  @backends ||= {}
end

.priorityArray<Symbol>

Returns backend names in resolution priority order.

Returns:

  • (Array<Symbol>)

    backend names in resolution priority order



26
27
28
# File 'lib/typst_rails/backends/registry.rb', line 26

def priority
  @priority ||= []
end

.register(name, backend, priority: true) ⇒ void

This method returns an undefined value.

Registers a backend under the given name.

Parameters:

  • name (Symbol)

    the backend's identifier (e.g. :gem, :cli)

  • backend (Base)

    the backend instance

  • priority (Boolean) (defaults to: true)

    whether to append this name to the auto-detection order



36
37
38
39
# File 'lib/typst_rails/backends/registry.rb', line 36

def register(name, backend, priority: true)
  backends[name.to_sym] = backend
  self.priority << name.to_sym if priority && !self.priority.include?(name.to_sym)
end

.reset!void

This method returns an undefined value.

Clears all registered backends. Primarily useful for tests.



56
57
58
59
# File 'lib/typst_rails/backends/registry.rb', line 56

def reset!
  @backends = {}
  @priority = []
end

.resolve(preference = :auto) ⇒ Base

Resolves which backend to use.

Parameters:

  • preference (Symbol, Base, nil) (defaults to: :auto)

    an explicit backend name, a backend instance, or nil/:auto to pick the first available backend in priority order

Returns:

  • (Base)

    the resolved backend

Raises:



47
48
49
50
51
52
# File 'lib/typst_rails/backends/registry.rb', line 47

def resolve(preference = :auto)
  return preference if preference.is_a?(Base)
  return fetch_named_backend(preference) if preference && preference != :auto

  auto_detect
end