Class: Dry::Schema::Messages::Abstract

Inherits:
Object
  • Object
show all
Includes:
Configurable
Defined in:
lib/dry/schema/messages/abstract.rb

Overview

Abstract class for message backends

Direct Known Subclasses

I18n, Namespaced, YAML

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.build(options = EMPTY_HASH) ⇒ Object

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.



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/dry/schema/messages/abstract.rb', line 55

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

.cacheObject

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.



50
51
52
# File 'lib/dry/schema/messages/abstract.rb', line 50

def self.cache
  @cache ||= Concurrent::Map.new { |h, k| h[k] = Concurrent::Map.new }
end

Instance Method Details

#cacheObject

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.



161
162
163
# File 'lib/dry/schema/messages/abstract.rb', line 161

def cache
  @cache ||= self.class.cache[self]
end

#call(predicate, options) ⇒ Template Also known as: []

Retrieve a message template

Returns:



94
95
96
97
98
99
# File 'lib/dry/schema/messages/abstract.rb', line 94

def call(predicate, options)
  cache.fetch_or_store([predicate, options.reject { |k,| k.equal?(:input) }]) do
    text, meta = lookup(predicate, options)
    [Template[text], meta] if text
  end
end

#default_localeObject

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.



166
167
168
# File 'lib/dry/schema/messages/abstract.rb', line 166

def default_locale
  config.default_locale
end

#lookup(predicate, options) ⇒ Object

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.

Try to find a message for the given predicate and its options

rubocop:disable Metrics/AbcSize



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/dry/schema/messages/abstract.rb', line 107

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

#lookup_paths(tokens) ⇒ Object

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.



133
134
135
# File 'lib/dry/schema/messages/abstract.rb', line 133

def lookup_paths(tokens)
  config.lookup_paths.map { |path| path % tokens }
end

#namespaced(namespace) ⇒ Object

Return a new message backend that will look for messages under provided namespace

Parameters:

  • namespace (Symbol, String)


147
148
149
# File 'lib/dry/schema/messages/abstract.rb', line 147

def namespaced(namespace)
  Dry::Schema::Messages::Namespaced.new(namespace, self)
end

#rootPathname

Return root path to messages file

Returns:

  • (Pathname)


156
157
158
# File 'lib/dry/schema/messages/abstract.rb', line 156

def root
  config.root
end

#rule(name, options = {}) ⇒ Object

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.



81
82
83
84
85
86
87
# File 'lib/dry/schema/messages/abstract.rb', line 81

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

#rule_lookup_paths(tokens) ⇒ Object

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.



138
139
140
# File 'lib/dry/schema/messages/abstract.rb', line 138

def rule_lookup_paths(tokens)
  config.rule_lookup_paths.map { |key| key % tokens }
end

#translate(key, locale: default_locale) ⇒ Object

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.



76
77
78
# File 'lib/dry/schema/messages/abstract.rb', line 76

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