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: #[])
- #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.
49 50 51 |
# File 'lib/dry/validation/messages/abstract.rb', line 49 def initialize @config = self.class.config end |
Instance Attribute Details
#config ⇒ Object (readonly)
Returns the value of attribute config.
47 48 49 |
# File 'lib/dry/validation/messages/abstract.rb', line 47 def config @config end |
Class Method Details
.cache ⇒ Object
43 44 45 |
# File 'lib/dry/validation/messages/abstract.rb', line 43 def self.cache @cache ||= Concurrent::Map.new { |h, k| h[k] = Concurrent::Map.new } end |
Instance Method Details
#cache ⇒ Object
102 103 104 |
# File 'lib/dry/validation/messages/abstract.rb', line 102 def cache @cache ||= self.class.cache[self] end |
#call(*args) ⇒ Object Also known as: []
62 63 64 65 66 67 |
# File 'lib/dry/validation/messages/abstract.rb', line 62 def call(*args) cache.fetch_or_store(args.hash) do path, opts = lookup(*args) get(path, opts) if path end end |
#default_locale ⇒ Object
106 107 108 |
# File 'lib/dry/validation/messages/abstract.rb', line 106 def default_locale :en end |
#hash ⇒ Object
53 54 55 |
# File 'lib/dry/validation/messages/abstract.rb', line 53 def hash @hash ||= config.hash end |
#lookup(predicate, options = {}) ⇒ Object
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/dry/validation/messages/abstract.rb', line 70 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
90 91 92 |
# File 'lib/dry/validation/messages/abstract.rb', line 90 def lookup_paths(tokens) config.lookup_paths.map { |path| path % tokens } end |
#namespaced(namespace) ⇒ Object
94 95 96 |
# File 'lib/dry/validation/messages/abstract.rb', line 94 def namespaced(namespace) Messages::Namespaced.new(namespace, self) end |
#root ⇒ Object
98 99 100 |
# File 'lib/dry/validation/messages/abstract.rb', line 98 def root config.root end |
#rule(name, options = {}) ⇒ Object
57 58 59 60 |
# File 'lib/dry/validation/messages/abstract.rb', line 57 def rule(name, = {}) path = "%{locale}.rules.#{name}" get(path, ) if key?(path, ) end |