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.



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

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

#call(*args) ⇒ Template Also known as: []

Retrieve a message template

Returns:



98
99
100
101
102
103
# File 'lib/dry/schema/messages/abstract.rb', line 98

def call(*args)
  cache.fetch_or_store(args.hash) do
    path, opts = lookup(*args)
    Template[get(path, opts)] if path
  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.



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

def default_locale
  :en
end

#hashObject

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 hash
  @hash ||= config.hash
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



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

def lookup(predicate, options = {})
  tokens = options.merge(
    root: options[:not] ? "#{root}.not" : root,
    predicate: predicate,
    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 do |key|
    key?(key, opts) && get(key, opts).is_a?(String)
  end

  [path, opts]
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.



128
129
130
# File 'lib/dry/schema/messages/abstract.rb', line 128

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)


142
143
144
# File 'lib/dry/schema/messages/abstract.rb', line 142

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

#rootPathname

Return root path to messages file

Returns:

  • (Pathname)


151
152
153
# File 'lib/dry/schema/messages/abstract.rb', line 151

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.



86
87
88
89
90
91
# File 'lib/dry/schema/messages/abstract.rb', line 86

def rule(name, options = {})
  tokens = { name: name, locale: options.fetch(:locale, default_locale) }
  path = rule_lookup_paths(tokens).detect { |key| key?(key, options) }

  get(path, options) if path
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.



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

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.



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

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