Class: Dry::Schema::Messages::Abstract
- Inherits:
-
Object
- Object
- Dry::Schema::Messages::Abstract
- Includes:
- Configurable
- Defined in:
- lib/dry/schema/messages/abstract.rb
Overview
Abstract class for message backends
Direct Known Subclasses
Class Method Summary collapse
- .build(options = EMPTY_HASH) ⇒ Object private
- .cache ⇒ Object private
Instance Method Summary collapse
- #cache ⇒ Object private
-
#call(*args) ⇒ Template
(also: #[])
Retrieve a message template.
- #default_locale ⇒ Object private
- #hash ⇒ Object private
-
#lookup(predicate, options = {}) ⇒ Object
private
Try to find a message for the given predicate and its options.
- #lookup_paths(tokens) ⇒ Object private
-
#namespaced(namespace) ⇒ Object
Return a new message backend that will look for messages under provided namespace.
-
#root ⇒ Pathname
Return root path to messages file.
- #rule(name, options = {}) ⇒ Object private
- #rule_lookup_paths(tokens) ⇒ Object private
- #translate(key, locale: default_locale) ⇒ Object private
Class Method Details
.build(options = EMPTY_HASH) ⇒ Object
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.
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/dry/schema/messages/abstract.rb', line 55 def self.build( = EMPTY_HASH) = new .configure do |config| .each do |key, value| config.public_send(:"#{key}=", value) end config.root = "#{config.top_namespace}.#{config.root}" config.rule_lookup_paths = config.rule_lookup_paths.map { |path| "#{config.top_namespace}.#{path}" } yield(config) if block_given? end .prepare end |
.cache ⇒ Object
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.
50 51 52 |
# File 'lib/dry/schema/messages/abstract.rb', line 50 def self.cache @cache ||= Concurrent::Map.new { |h, k| h[k] = Concurrent::Map.new } end |
Instance Method Details
#cache ⇒ Object
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.
156 157 158 |
# File 'lib/dry/schema/messages/abstract.rb', line 156 def cache @cache ||= self.class.cache[self] end |
#call(*args) ⇒ Template Also known as: []
Retrieve a message template
98 99 100 101 102 103 |
# File 'lib/dry/schema/messages/abstract.rb', line 98 def call(*args) cache.fetch_or_store(args.hash) do path, opts = lookup(*args) Template[get(path, opts)] if path end end |
#default_locale ⇒ Object
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.
161 162 163 |
# File 'lib/dry/schema/messages/abstract.rb', line 161 def default_locale :en end |
#hash ⇒ Object
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.
76 77 78 |
# File 'lib/dry/schema/messages/abstract.rb', line 76 def hash @hash ||= config.hash end |
#lookup(predicate, options = {}) ⇒ Object
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.
Try to find a message for the given predicate and its options
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
# File 'lib/dry/schema/messages/abstract.rb', line 109 def lookup(predicate, = {}) tokens = .merge( root: [:not] ? "#{root}.not" : root, predicate: predicate, arg_type: config.arg_types[[:arg_type]], val_type: config.val_types[[:val_type]], message_type: [:message_type] || :failure ) 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
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.
128 129 130 |
# File 'lib/dry/schema/messages/abstract.rb', line 128 def lookup_paths(tokens) config.lookup_paths.map { |path| path % tokens } end |
#namespaced(namespace) ⇒ Object
Return a new message backend that will look for messages under provided namespace
142 143 144 |
# File 'lib/dry/schema/messages/abstract.rb', line 142 def namespaced(namespace) Dry::Schema::Messages::Namespaced.new(namespace, self) end |
#root ⇒ Pathname
Return root path to messages file
151 152 153 |
# File 'lib/dry/schema/messages/abstract.rb', line 151 def root config.root end |
#rule(name, options = {}) ⇒ Object
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.
86 87 88 89 90 91 |
# File 'lib/dry/schema/messages/abstract.rb', line 86 def rule(name, = {}) tokens = { name: name, locale: .fetch(:locale, default_locale) } path = rule_lookup_paths(tokens).detect { |key| key?(key, ) } get(path, ) if path end |
#rule_lookup_paths(tokens) ⇒ Object
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.
133 134 135 |
# File 'lib/dry/schema/messages/abstract.rb', line 133 def rule_lookup_paths(tokens) config.rule_lookup_paths.map { |key| key % tokens } end |
#translate(key, locale: default_locale) ⇒ Object
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.
81 82 83 |
# File 'lib/dry/schema/messages/abstract.rb', line 81 def translate(key, locale: default_locale) t["#{config.top_namespace}.#{key}", locale: locale] end |