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
Instance Attribute Summary collapse
-
#config ⇒ Object
readonly
Returns the value of attribute config.
Class Method Summary collapse
Instance Method Summary collapse
- #cache ⇒ Object
- #call(*args) ⇒ Object (also: #[])
- #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.
47 48 49 |
# File 'lib/dry/validation/messages/abstract.rb', line 47 def initialize @config = self.class.config end |
Instance Attribute Details
#config ⇒ Object (readonly)
Returns the value of attribute config.
45 46 47 |
# File 'lib/dry/validation/messages/abstract.rb', line 45 def config @config end |
Class Method Details
.cache ⇒ Object
41 42 43 |
# File 'lib/dry/validation/messages/abstract.rb', line 41 def self.cache @cache ||= Concurrent::Map.new { |h, k| h[k] = Concurrent::Map.new } end |
Instance Method Details
#cache ⇒ Object
100 101 102 |
# File 'lib/dry/validation/messages/abstract.rb', line 100 def cache @cache ||= self.class.cache[self] end |
#call(*args) ⇒ Object Also known as: []
60 61 62 63 64 65 |
# File 'lib/dry/validation/messages/abstract.rb', line 60 def call(*args) cache.fetch_or_store(args.hash) do path, opts = lookup(*args) get(path, opts) if path end end |
#hash ⇒ Object
51 52 53 |
# File 'lib/dry/validation/messages/abstract.rb', line 51 def hash @hash ||= config.hash end |
#lookup(predicate, options = {}) ⇒ Object
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/dry/validation/messages/abstract.rb', line 68 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 = .reject { |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
88 89 90 |
# File 'lib/dry/validation/messages/abstract.rb', line 88 def lookup_paths(tokens) config.lookup_paths.map { |path| path % tokens } end |
#namespaced(namespace) ⇒ Object
92 93 94 |
# File 'lib/dry/validation/messages/abstract.rb', line 92 def namespaced(namespace) Messages::Namespaced.new(namespace, self) end |
#root ⇒ Object
96 97 98 |
# File 'lib/dry/validation/messages/abstract.rb', line 96 def root config.root end |
#rule(name, options = {}) ⇒ Object
55 56 57 58 |
# File 'lib/dry/validation/messages/abstract.rb', line 55 def rule(name, = {}) path = "%{locale}.rules.#{name}" get(path, ) if key?(path, ) end |