Module: TranslationDiff::Providers

Defined in:
lib/translation_diff/providers.rb

Overview

Translation providers, by name; registering one also declares its configuration options.

Defined Under Namespace

Classes: Amazon, Azure, DeepL, Google, LibreTranslate, ModernMT, Null

Constant Summary collapse

CONTRACT =

Said once, so a provider refused by name, by class or as an object is refused in the same words.

"The base class supplies the transport, the configuration check and the " \
"capability defaults, so a provider that skips it has none of them.".freeze

Class Method Summary collapse

Class Method Details

.build(name, config) ⇒ Object



33
34
35
# File 'lib/translation_diff/providers.rb', line 33

def build(name, config)
  registry.build(name, config).tap { |provider| provider.name = name.to_sym }
end

.classes ⇒ Object



45
# File 'lib/translation_diff/providers.rb', line 45

def classes = registry.classes

.ensure_provider!(instance) ⇒ Object

Guards the other two ways a provider arrives: assigned to config.provider, or passed as provider:.



25
26
27
28
29
30
31
# File 'lib/translation_diff/providers.rb', line 25

def ensure_provider!(instance)
  return instance if instance.is_a?(TranslationDiff::Provider)

  raise TranslationDiff::InvalidProviderError,
        "#{describe(instance)} cannot be used as a provider: it does not inherit " \
        "TranslationDiff::Provider. #{CONTRACT}"
end

.ensure_provider_class!(klass) ⇒ Object

Guards registration. klass < Provider alone raises NoMethodError for an instance or a non-Module.



16
17
18
19
20
21
22
# File 'lib/translation_diff/providers.rb', line 16

def ensure_provider_class!(klass)
  return klass if klass.is_a?(Class) && klass < TranslationDiff::Provider

  raise TranslationDiff::InvalidProviderError,
        "#{describe(klass)} cannot be registered as a provider: the registry takes a " \
        "class inheriting TranslationDiff::Provider. #{CONTRACT}"
end

.names ⇒ Object



44
# File 'lib/translation_diff/providers.rb', line 44

def names = registry.names

.register(name, klass) ⇒ Object

Options are declared before the registry entry is written, so a name collision raises without replacing.



9
10
11
12
13
# File 'lib/translation_diff/providers.rb', line 9

def register(name, klass)
  ensure_provider_class!(klass)
  TranslationDiff::Configuration.register_provider_options(klass.configuration_options, klass)
  registry.register(name, klass)
end

.registered?(name) ⇒ Boolean

Returns:

  • (Boolean)


43
# File 'lib/translation_diff/providers.rb', line 43

def registered?(name) = registry.registered?(name)

.registry ⇒ Object



46
# File 'lib/translation_diff/providers.rb', line 46

def registry = @registry ||= TranslationDiff::Registry.new("provider")

.resolve(requested, config) ⇒ Object

The one seam Translator and Previewer both resolve a provider through, cache_key guard included.



38
39
40
41
# File 'lib/translation_diff/providers.rb', line 38

def resolve(requested, config)
  provider = requested.nil? ? config.provider_instance : resolve_requested(requested, config)
  ensure_cache_key!(provider)
end