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 Method Summary collapse
- #cache ⇒ Object
- #call(*args) ⇒ Object (also: #[])
- #config ⇒ Object
- #lookup(predicate, options) ⇒ Object
- #lookup_paths(tokens) ⇒ Object
- #namespaced(namespace) ⇒ Object
- #root ⇒ Object
Instance Method Details
#cache ⇒ Object
72 73 74 |
# File 'lib/dry/validation/messages/abstract.rb', line 72 def cache @cache ||= ThreadSafe::Cache.new end |
#call(*args) ⇒ Object Also known as: []
37 38 39 |
# File 'lib/dry/validation/messages/abstract.rb', line 37 def call(*args) cache.fetch_or_store(args.hash) { get(*lookup(*args)) } end |
#config ⇒ Object
68 69 70 |
# File 'lib/dry/validation/messages/abstract.rb', line 68 def config self.class.config end |
#lookup(predicate, options) ⇒ Object
42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/dry/validation/messages/abstract.rb', line 42 def lookup(predicate, ) tokens = .merge( root: root, predicate: predicate, arg_type: config.arg_types[[:arg_type]], val_type: config.val_types[[:val_type]] ) opts = .reject { |k, _| config..include?(k) } path = lookup_paths(tokens).detect { |key| key?(key, opts) && get(key).is_a?(String) } [path, opts] end |
#lookup_paths(tokens) ⇒ Object
56 57 58 |
# File 'lib/dry/validation/messages/abstract.rb', line 56 def lookup_paths(tokens) config.lookup_paths.map { |path| path % tokens } end |
#namespaced(namespace) ⇒ Object
60 61 62 |
# File 'lib/dry/validation/messages/abstract.rb', line 60 def namespaced(namespace) Messages::Namespaced.new(namespace, self) end |
#root ⇒ Object
64 65 66 |
# File 'lib/dry/validation/messages/abstract.rb', line 64 def root config.root end |