Class: Dry::Validation::Messages::Resolver
- Inherits:
-
Object
- Object
- Dry::Validation::Messages::Resolver
- Defined in:
- lib/dry/validation/messages/resolver.rb
Overview
Resolve translated messages from failure arguments
Instance Attribute Summary collapse
- #messages ⇒ Object readonly
Instance Method Summary collapse
-
#call(message:, tokens:, path:, meta: EMPTY_HASH) ⇒ Message, Message::Localized
(also: #[])
Resolve Message object from provided args and path.
-
#initialize(messages) ⇒ Resolver
constructor
private
A new instance of Resolver.
-
#message(rule, path:, tokens: EMPTY_HASH, locale: nil, full: false) ⇒ String
Resolve a message.
Constructor Details
#initialize(messages) ⇒ Resolver
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.
Returns a new instance of Resolver.
21 22 23 |
# File 'lib/dry/validation/messages/resolver.rb', line 21 def initialize() @messages = end |
Instance Attribute Details
#messages ⇒ Object (readonly)
18 19 20 |
# File 'lib/dry/validation/messages/resolver.rb', line 18 def @messages end |
Instance Method Details
#call(message:, tokens:, path:, meta: EMPTY_HASH) ⇒ Message, Message::Localized Also known as: []
Resolve Message object from provided args and path
This is used internally by contracts when rules are applied If message argument is a Hash, then it MUST have a :text key, which value will be used as the message value
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/dry/validation/messages/resolver.rb', line 34 def call(message:, tokens:, path:, meta: EMPTY_HASH) case when Symbol Message[->(**opts) { (, path: path, tokens: tokens, **opts) }, path, ] when String Message[->(**opts) { [(, path: path, **opts), ] }, path, ] when Hash = .dup text = .delete(:text) { |key| raise ArgumentError, <<~STR +message+ Hash must contain :#{key} key (#{.inspect} given) STR } call(message: text, tokens: tokens, path: path, meta: ) else raise ArgumentError, <<~STR +message+ must be either a Symbol, String or Hash (#{.inspect} given) STR end end |
#message(rule, path:, tokens: EMPTY_HASH, locale: nil, full: false) ⇒ String
Resolve a message
rubocop:disable Metrics/AbcSize rubocop:disable Metrics/PerceivedComplexity
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/dry/validation/messages/resolver.rb', line 65 def (rule, path:, tokens: EMPTY_HASH, locale: nil, full: false) keys = path.to_a.compact msg_opts = tokens.merge(path: keys, locale: locale || .default_locale) if keys.empty? template, = ["rules.#{rule}", msg_opts] else template, = [rule, msg_opts.merge(path: keys.join(DOT))] template, = [rule, msg_opts.merge(path: keys.last)] unless template end if !template && keys.size > 1 non_index_keys = keys.reject { |k| k.is_a?(Integer) } template, = [rule, msg_opts.merge(path: non_index_keys.join(DOT))] end unless template raise MissingMessageError, <<~STR Message template for #{rule.inspect} under #{keys.join(DOT).inspect} was not found STR end parsed_tokens = parse_tokens(tokens) text = template.(template.data(parsed_tokens)) [(text, path: path, locale: locale, full: full), ] end |