Class: TranslationDiff::Configuration

Inherits:
Object
  • Object
show all
Includes:
CacheGuardOptions, CacheTtlOption
Defined in:
lib/translation_diff/configuration.rb

Overview

Every declared setting in one place; callable defaults are invoked on read, not at load time.

Defined Under Namespace

Modules: CacheGuardOptions, CacheTtlOption, OptionTable Classes: ProviderOptionOwners

Constant Summary

Constants included from CacheTtlOption

CacheTtlOption::NEVER_ASSIGNED

Constants included from CacheGuardOptions

CacheGuardOptions::CACHE_NAMESPACE_LIMIT

Class Method Summary collapse

Instance Method Summary collapse

Methods included from CacheTtlOption

#cache_ttl, #cache_ttl=, #initialize

Methods included from CacheGuardOptions

#cache_namespace=, #cache_prune_probability=

Class Method Details

.defaults ⇒ Object



26
# File 'lib/translation_diff/configuration.rb', line 26

def defaults = @defaults ||= {}

.option(key, default = nil, invalidates: nil) ⇒ Object

invalidates: names the memoised reader(s) this option feeds; a writer clears exactly those ivars. An option that names none -- logger, instrumenter, the timeouts -- clears nothing, which is also what an option nobody classifies does: invalidation is opt-in, never a guess from the option's name.



7
8
9
10
11
12
13
14
# File 'lib/translation_diff/configuration.rb', line 7

def option(key, default = nil, invalidates: nil)
  key = key.to_sym
  return if options.include?(key)

  define_option_accessors(key, Array(invalidates))
  defaults[key] = default
  options << key
end

.options ⇒ Object



25
# File 'lib/translation_diff/configuration.rb', line 25

def options = @options ||= []

.register_provider_options(declared, provider) ⇒ Object

See ProviderOptionOwners for the conflict rules and the all-or-nothing guarantee. Every provider option invalidates provider_instance, whatever it is named -- the registry, not a remembered list, is what makes the set known.



19
20
21
22
23
# File 'lib/translation_diff/configuration.rb', line 19

def register_provider_options(declared, provider)
  declared = normalise_declarations(declared)
  provider_option_owners.claim(declared.keys, provider)
  declared.each { |key, default| option(key, default, invalidates: :provider_instance) }
end

Instance Method Details

#cache_store ⇒ Object

Unset cache means Redis when a URL is configured, otherwise in-process -- works before anything runs.



85
86
87
# File 'lib/translation_diff/configuration.rb', line 85

def cache_store
  @cache_store ||= resolve(cache || (redis_url ? :redis : :memory), TranslationDiff::Stores)
end

#copy ⇒ Object

Memoised collaborators aren't copied, or a tenant's own cache_namespace leaks its parent's rate limiter.



70
71
72
73
74
75
76
# File 'lib/translation_diff/configuration.rb', line 70

def copy
  self.class.new.tap do |other|
    self.class.options.each do |key|
      other.instance_variable_set(:"@#{key}", instance_variable_get(:"@#{key}"))
    end
  end
end

#inspect ⇒ Object

Credentials are filtered by name; everything else is shown, or an inspect is one nobody reads.



67
# File 'lib/translation_diff/configuration.rb', line 67

def inspect = "#<#{self.class.name} #{TranslationDiff::Redaction.render(self).join(' ')}>"

#provider_instance ⇒ Object

Guarded, unlike cache/segmenter/rate_limiter: only the provider gained a base class to check against.



79
80
81
82
# File 'lib/translation_diff/configuration.rb', line 79

def provider_instance
  @provider_instance ||=
    TranslationDiff::Providers.ensure_provider!(resolve(provider, TranslationDiff::Providers))
end

#rate_limiter_instance ⇒ Object

nil, not a null object: Dispatcher#throttle checks for nil and skips rate-limiting -- costs nothing normally.



94
95
96
97
98
# File 'lib/translation_diff/configuration.rb', line 94

def rate_limiter_instance
  return nil if rate_limiter.nil? && rate_limit.nil?

  @rate_limiter_instance ||= resolve(rate_limiter || :redis, TranslationDiff::RateLimiters)
end

#redis_pool ⇒ Object

One pool shared by the cache store and the rate limiter; callers used to build and pass it by hand.



101
102
103
# File 'lib/translation_diff/configuration.rb', line 101

def redis_pool
  @redis_pool ||= build_redis_pool
end

#segmenter_instance ⇒ Object



89
90
91
# File 'lib/translation_diff/configuration.rb', line 89

def segmenter_instance
  @segmenter_instance ||= resolve(segmenter, TranslationDiff::Segmenters.registry)
end