Class: Dry::Validation::Messages::Abstract
- Inherits:
-
Object
- Object
- Dry::Validation::Messages::Abstract
- Extended by:
- Configurable
- Defined in:
- lib/dry/validation/messages/abstract.rb
Direct Known Subclasses
Constant Summary collapse
- DEFAULT_PATH =
Pathname(__dir__).join('../../../../config/errors.yml').realpath.freeze
- CACHE_KEYS =
%i[path message_type val_type arg_type locale].freeze
Instance Attribute Summary collapse
-
#config ⇒ Object
readonly
Returns the value of attribute config.
Class Method Summary collapse
Instance Method Summary collapse
- #cache ⇒ Object
- #call(predicate, options = EMPTY_HASH) ⇒ Object (also: #[])
- #default_locale ⇒ Object
- #hash ⇒ Object
-
#initialize ⇒ Abstract
constructor
A new instance of Abstract.
- #lookup(predicate, options = {}) ⇒ Object
- #lookup_paths(tokens) ⇒ Object
- #namespaced(namespace) ⇒ Object
- #root ⇒ Object
- #rule(name, options = {}) ⇒ Object
Constructor Details
#initialize ⇒ Abstract
Returns a new instance of Abstract.
54 55 56 |
# File 'lib/dry/validation/messages/abstract.rb', line 54 def initialize @config = self.class.config end |
Instance Attribute Details
#config ⇒ Object (readonly)
Returns the value of attribute config.
52 53 54 |
# File 'lib/dry/validation/messages/abstract.rb', line 52 def config @config end |
Class Method Details
.cache ⇒ Object
48 49 50 |
# File 'lib/dry/validation/messages/abstract.rb', line 48 def self.cache @cache ||= Concurrent::Map.new { |h, k| h[k] = Concurrent::Map.new } end |
Instance Method Details
#cache ⇒ Object
109 110 111 |
# File 'lib/dry/validation/messages/abstract.rb', line 109 def cache @cache ||= self.class.cache[self] end |
#call(predicate, options = EMPTY_HASH) ⇒ Object Also known as: []
67 68 69 70 71 72 73 74 |
# File 'lib/dry/validation/messages/abstract.rb', line 67 def call(predicate, = EMPTY_HASH) cache.fetch_or_store([predicate, .reject { |k,| k.equal?(:input) }]) do path, opts = lookup(predicate, ) return unless path text = yield(path, opts) Template[text] end end |
#default_locale ⇒ Object
113 114 115 |
# File 'lib/dry/validation/messages/abstract.rb', line 113 def default_locale :en end |
#hash ⇒ Object
58 59 60 |
# File 'lib/dry/validation/messages/abstract.rb', line 58 def hash @hash ||= config.hash end |
#lookup(predicate, options = {}) ⇒ Object
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/dry/validation/messages/abstract.rb', line 77 def lookup(predicate, = {}) tokens = .merge( root: root, predicate: predicate, arg_type: config.arg_types[[:arg_type]], val_type: config.val_types[[:val_type]], message_type: [:message_type] || :failure ) tokens[:rule] = predicate unless tokens.key?(:rule) opts = .select { |k, _| !config..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
97 98 99 |
# File 'lib/dry/validation/messages/abstract.rb', line 97 def lookup_paths(tokens) config.lookup_paths.map { |path| path % tokens } end |
#namespaced(namespace) ⇒ Object
101 102 103 |
# File 'lib/dry/validation/messages/abstract.rb', line 101 def namespaced(namespace) Messages::Namespaced.new(namespace, self) end |
#root ⇒ Object
105 106 107 |
# File 'lib/dry/validation/messages/abstract.rb', line 105 def root config.root end |
#rule(name, options = {}) ⇒ Object
62 63 64 65 |
# File 'lib/dry/validation/messages/abstract.rb', line 62 def rule(name, = {}) path = "%{locale}.rules.#{name}" get(path, ) if key?(path, ) end |