class Dry::Schema::Messages::Abstract

@api public
Abstract class for message backends

def self.build(options = EMPTY_HASH)

Other tags:
    Api: - private
def self.build(options = EMPTY_HASH)
  messages = new
  messages.configure do |config|
    options.each do |key, value|
      config.public_send(:"#{key}=", value)
    end
    config.root = "#{config.top_namespace}.#{config.root}"
    config.rule_lookup_paths = config.rule_lookup_paths.map { |path|
      "#{config.top_namespace}.#{path}"
    }
    yield(config) if block_given?
  end
  messages.prepare
end

def self.cache

Other tags:
    Api: - private
def self.cache
  @cache ||= Concurrent::Map.new { |h, k| h[k] = Concurrent::Map.new }
end

def cache

Other tags:
    Api: - private
def cache
  @cache ||= self.class.cache[self]
end

def call(*args)

Other tags:
    Api: - public

Returns:
  • (Template) -
def call(*args)
  cache.fetch_or_store(args.hash) do
    text, meta = lookup(*args)
    [Template[text], meta] if text
  end
end

def custom_top_namespace?(path)

Other tags:
    Api: - private
def custom_top_namespace?(path)
  path.to_s == DEFAULT_MESSAGES_PATH.to_s && config.top_namespace != DEFAULT_MESSAGES_ROOT
end

def default_locale

Other tags:
    Api: - private
def default_locale
  :en
end

def hash

Other tags:
    Api: - private
def hash
  @hash ||= config.hash
end

def lookup(predicate, options)

Other tags:
    Api: - private
def lookup(predicate, options)
  tokens = options.merge(
    predicate: predicate,
    root: options[:not] ? "#{root}.not" : root,
    arg_type: config.arg_types[options[:arg_type]],
    val_type: config.val_types[options[:val_type]],
    message_type: options[:message_type] || :failure
  )
  opts = options.reject { |k, _| config.lookup_options.include?(k) }
  path = lookup_paths(tokens).detect { |key| key?(key, opts) }
  return unless path
  text = get(path, opts)
  if text.is_a?(Hash)
    text.values_at(:text, :meta)
  else
    [text, EMPTY_HASH]
  end
end

def lookup_paths(tokens)

Other tags:
    Api: - private
def lookup_paths(tokens)
  config.lookup_paths.map { |path| path % tokens }
end

def namespaced(namespace)

Other tags:
    Api: - public

Parameters:
  • namespace (Symbol, String) --
def namespaced(namespace)
  Dry::Schema::Messages::Namespaced.new(namespace, self)
end

def root

Other tags:
    Api: - public

Returns:
  • (Pathname) -
def root
  config.root
end

def rule(name, options = {})

Other tags:
    Api: - private
def rule(name, options = {})
  tokens = { name: name, locale: options.fetch(:locale, default_locale) }
  path = rule_lookup_paths(tokens).detect { |key| key?(key, options) }
  rule = get(path, options) if path
  rule.is_a?(Hash) ? rule[:text] : rule
end

def rule_lookup_paths(tokens)

Other tags:
    Api: - private
def rule_lookup_paths(tokens)
  config.rule_lookup_paths.map { |key| key % tokens }
end

def translate(key, locale: default_locale)

Other tags:
    Api: - private
def translate(key, locale: default_locale)
  t["#{config.top_namespace}.#{key}", locale: locale]
end