class Dry::Schema::Messages::Abstract
Abstract
class for message backends
@api public
Public Class Methods
build(options = EMPTY_HASH) { |config| ... }
click to toggle source
@api private
# File lib/dry/schema/messages/abstract.rb, line 50 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
Public Instance Methods
call(predicate, options)
click to toggle source
Retrieve a message template
@return [Template]
@api public
# File lib/dry/schema/messages/abstract.rb, line 89 def call(predicate, options) options = {locale: default_locale, **options} opts = options.reject { |k,| config.lookup_options.include?(k) } path = lookup_paths(predicate, options).detect { |key| key?(key, opts) } return unless path result = get(path, opts) [ Template.new( messages: self, key: path, options: opts ), result[:meta] ] end
Also aliased as: []
default_locale()
click to toggle source
@api private
# File lib/dry/schema/messages/abstract.rb, line 167 def default_locale config.default_locale end
filled_lookup_paths(tokens)
click to toggle source
@api private
# File lib/dry/schema/messages/abstract.rb, line 139 def filled_lookup_paths(tokens) config.lookup_paths.map { |path| path % tokens } end
interpolatable_data(_key, _options, **_data)
click to toggle source
@api private
# File lib/dry/schema/messages/abstract.rb, line 172 def interpolatable_data(_key, _options, **_data) raise NotImplementedError end
interpolate(_key, _options, **_data)
click to toggle source
@api private
# File lib/dry/schema/messages/abstract.rb, line 177 def interpolate(_key, _options, **_data) raise NotImplementedError end
key?(_key, _options = EMPTY_HASH)
click to toggle source
Check if given key is defined
@return [Boolean]
@api public
# File lib/dry/schema/messages/abstract.rb, line 115 def key?(_key, _options = EMPTY_HASH) raise NotImplementedError end
looked_up_paths(predicate, options)
click to toggle source
Retrieve an array of looked up paths
@param [Symbol] predicate @param [Hash] options
@return [String]
@api public
# File lib/dry/schema/messages/abstract.rb, line 127 def looked_up_paths(predicate, options) tokens = lookup_tokens(predicate, options) filled_lookup_paths(tokens) end
lookup_paths(predicate, options)
click to toggle source
@api private
# File lib/dry/schema/messages/abstract.rb, line 133 def lookup_paths(predicate, options) tokens = lookup_tokens(predicate, options) filled_lookup_paths(tokens) end
namespaced(namespace)
click to toggle source
Return a new message backend that will look for messages under provided namespace
@param [Symbol,String] namespace
@api public
# File lib/dry/schema/messages/abstract.rb, line 153 def namespaced(namespace) Dry::Schema::Messages::Namespaced.new(namespace, self) end
root()
click to toggle source
Return root path to messages file
@return [Pathname]
@api public
# File lib/dry/schema/messages/abstract.rb, line 162 def root config.root end
rule(name, options = {})
click to toggle source
@api private
# File lib/dry/schema/messages/abstract.rb, line 76 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)
click to toggle source
@api private
# File lib/dry/schema/messages/abstract.rb, line 144 def rule_lookup_paths(tokens) config.rule_lookup_paths.map { |key| key % tokens } end
translate(key, locale: default_locale)
click to toggle source
@api private
# File lib/dry/schema/messages/abstract.rb, line 71 def translate(key, locale: default_locale) t["#{config.top_namespace}.#{key}", locale: locale] end
Private Instance Methods
custom_top_namespace?(path)
click to toggle source
@api private
# File lib/dry/schema/messages/abstract.rb, line 195 def custom_top_namespace?(path) path.to_s == DEFAULT_MESSAGES_PATH.to_s && config.top_namespace != DEFAULT_MESSAGES_ROOT end
lookup_tokens(predicate, options)
click to toggle source
@api private
# File lib/dry/schema/messages/abstract.rb, line 184 def lookup_tokens(predicate, options) 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 ) end