Class: Importu::Backends

Inherits:
Object
  • Object
show all
Defined in:
lib/importu/backends.rb,
lib/importu/backends/middleware.rb

Overview

Registry for persistence backends.

Backends handle persisting imported records to a data store. The registry allows registering custom backends and auto-detecting which backend to use based on the model class.

Examples:

Registering a custom backend

Importu::Backends.registry.register(:sequel, MySequelBackend)

Looking up a backend

backend_class = Importu::Backends.registry.lookup(:active_record)

Defined Under Namespace

Modules: Middleware Classes: ActiveRecord

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeBackends

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Creates a new backend registry.



40
41
42
43
44
# File 'lib/importu/backends.rb', line 40

def initialize
  @registered = Hash.new do |_hash, key|
    raise Importu::BackendNotRegistered, key
  end
end

Class Method Details

.middlewareArray<Class>

Returns the middleware classes applied to all backends.

Returns:

  • (Array<Class>)

    middleware classes in application order



23
24
25
26
27
28
# File 'lib/importu/backends.rb', line 23

def self.middleware
  [
    Importu::Backends::Middleware::EnforceAllowedActions,
    Importu::Backends::Middleware::DuplicateManagerProxy,
  ]
end

.registryImportu::Backends

Returns the global backend registry instance.

Returns:



33
34
35
# File 'lib/importu/backends.rb', line 33

def self.registry
  @registry ||= new
end

Instance Method Details

#from_config!(name:, model:) ⇒ Class

Resolves a backend class from importer configuration.

Parameters:

  • name (Symbol, nil)

    explicit backend name, :auto, or nil for auto-detect

  • model (Class, String)

    the model class or class name

Returns:

  • (Class)

    the backend class

Raises:



53
54
55
56
57
# File 'lib/importu/backends.rb', line 53

def from_config!(name:, model:, **)
  model = model.is_a?(String) ? self.class.const_get(model) : model
  # Support :auto as an explicit way to request auto-detection (same as nil)
  name && name != :auto ? lookup(name) : detect_from_model(model)
end

#lookup(name) ⇒ Class

Looks up a backend by name.

Parameters:

  • name (Symbol, String)

    the backend name

Returns:

  • (Class)

    the backend class

Raises:



64
65
66
# File 'lib/importu/backends.rb', line 64

def lookup(name)
  @registered[name.to_sym]
end

#namesArray<Symbol>

Returns all registered backend names.

Returns:

  • (Array<Symbol>)

    registered backend names



71
72
73
# File 'lib/importu/backends.rb', line 71

def names
  @registered.keys
end

#register(name, klass) ⇒ Class

Registers a backend class.

Parameters:

  • name (Symbol, String)

    the backend name

  • klass (Class)

    the backend class

Returns:

  • (Class)

    the registered class



80
81
82
# File 'lib/importu/backends.rb', line 80

def register(name, klass)
  @registered[name.to_sym] = klass
end