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
- .setting_names ⇒ Object private
Instance Method Summary collapse
-
#call(predicate, options) ⇒ Template
(also: #[])
Retrieve a message template.
- #default_locale ⇒ Object private
- #filled_lookup_paths(tokens) ⇒ Object private
- #interpolatable_data(_key, _options, **_data) ⇒ Object private
- #interpolate(_key, _options, **_data) ⇒ Object private
-
#key?(_key, _options = EMPTY_HASH) ⇒ Boolean
Check if given key is defined.
-
#looked_up_paths(predicate, options) ⇒ String
Retrieve an array of looked up paths.
- #lookup_paths(predicate, options) ⇒ 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.
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/dry/schema/messages/abstract.rb', line 58 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 |
.setting_names ⇒ 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.
47 48 49 50 51 52 53 54 55 |
# File 'lib/dry/schema/messages/abstract.rb', line 47 def self.setting_names # TODO: this is compatibility tweak to make it work with dry-configurable < 0.16.1 # and should be removed in dry-schema 2.0.0 @setting_names ||= settings .map { |setting| setting.respond_to?(:name) ? setting.name : setting } .map(&:to_sym) end |
Instance Method Details
#call(predicate, options) ⇒ Template Also known as: []
Retrieve a message template
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/dry/schema/messages/abstract.rb', line 97 def call(predicate, ) = {locale: default_locale, **} opts = .reject { |k,| config..include?(k) } path = lookup_paths(predicate, ).detect { |key| key?(key, opts) } return unless path result = get(path, opts) [ Template.new( messages: self, key: path, options: opts ), result[:meta] ] 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.
175 176 177 |
# File 'lib/dry/schema/messages/abstract.rb', line 175 def default_locale config.default_locale end |
#filled_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.
147 148 149 |
# File 'lib/dry/schema/messages/abstract.rb', line 147 def filled_lookup_paths(tokens) config.lookup_paths.map { |path| path % tokens } end |
#interpolatable_data(_key, _options, **_data) ⇒ 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.
180 181 182 |
# File 'lib/dry/schema/messages/abstract.rb', line 180 def interpolatable_data(_key, , **_data) raise NotImplementedError end |
#interpolate(_key, _options, **_data) ⇒ 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.
185 186 187 |
# File 'lib/dry/schema/messages/abstract.rb', line 185 def interpolate(_key, , **_data) raise NotImplementedError end |
#key?(_key, _options = EMPTY_HASH) ⇒ Boolean
Check if given key is defined
123 124 125 |
# File 'lib/dry/schema/messages/abstract.rb', line 123 def key?(_key, = EMPTY_HASH) raise NotImplementedError end |
#looked_up_paths(predicate, options) ⇒ String
Retrieve an array of looked up paths
135 136 137 138 |
# File 'lib/dry/schema/messages/abstract.rb', line 135 def looked_up_paths(predicate, ) tokens = lookup_tokens(predicate, ) filled_lookup_paths(tokens) end |
#lookup_paths(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.
141 142 143 144 |
# File 'lib/dry/schema/messages/abstract.rb', line 141 def lookup_paths(predicate, ) tokens = lookup_tokens(predicate, ) filled_lookup_paths(tokens) end |
#namespaced(namespace) ⇒ Object
Return a new message backend that will look for messages under provided namespace
161 162 163 |
# File 'lib/dry/schema/messages/abstract.rb', line 161 def namespaced(namespace) Dry::Schema::Messages::Namespaced.new(namespace, self) end |
#root ⇒ Pathname
Return root path to messages file
170 171 172 |
# File 'lib/dry/schema/messages/abstract.rb', line 170 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.
84 85 86 87 88 89 90 |
# File 'lib/dry/schema/messages/abstract.rb', line 84 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.
152 153 154 |
# File 'lib/dry/schema/messages/abstract.rb', line 152 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.
79 80 81 |
# File 'lib/dry/schema/messages/abstract.rb', line 79 def translate(key, locale: default_locale) t["#{config.top_namespace}.#{key}", locale: locale] end |