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(predicate, options) ⇒ Template
(also: #[])
Retrieve a message template.
- #default_locale ⇒ 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.
161 162 163 |
# File 'lib/dry/schema/messages/abstract.rb', line 161 def cache @cache ||= self.class.cache[self] end |
#call(predicate, options) ⇒ Template Also known as: []
Retrieve a message template
94 95 96 97 98 99 |
# File 'lib/dry/schema/messages/abstract.rb', line 94 def call(predicate, ) cache.fetch_or_store([predicate, .reject { |k,| k.equal?(:input) }]) do text, = lookup(predicate, ) [Template[text], ] if text 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.
166 167 168 |
# File 'lib/dry/schema/messages/abstract.rb', line 166 def default_locale config.default_locale 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
rubocop:disable Metrics/AbcSize
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 |
# File 'lib/dry/schema/messages/abstract.rb', line 107 def lookup(predicate, ) tokens = .merge( predicate: predicate, root: [:not] ? "#{root}.not" : root, 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 { |key| key?(key, opts) } return unless path text = get(path, opts) if text.is_a?(Hash) text.values_at(:text, :meta) else [text, EMPTY_HASH] end 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.
133 134 135 |
# File 'lib/dry/schema/messages/abstract.rb', line 133 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
147 148 149 |
# File 'lib/dry/schema/messages/abstract.rb', line 147 def namespaced(namespace) Dry::Schema::Messages::Namespaced.new(namespace, self) end |
#root ⇒ Pathname
Return root path to messages file
156 157 158 |
# File 'lib/dry/schema/messages/abstract.rb', line 156 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.
81 82 83 84 85 86 87 |
# File 'lib/dry/schema/messages/abstract.rb', line 81 def rule(name, = {}) tokens = { name: name, locale: .fetch(:locale, default_locale) } path = rule_lookup_paths(tokens).detect { |key| key?(key, ) } rule = get(path, ) if path rule.is_a?(Hash) ? rule[:text] : rule 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.
138 139 140 |
# File 'lib/dry/schema/messages/abstract.rb', line 138 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.
76 77 78 |
# File 'lib/dry/schema/messages/abstract.rb', line 76 def translate(key, locale: default_locale) t["#{config.top_namespace}.#{key}", locale: locale] end |