Class: Dry::Schema::Messages::YAML
- Defined in:
- lib/dry/schema/messages/yaml.rb
Overview
Plain YAML message backend
Constant Summary collapse
- LOCALE_TOKEN =
"%<locale>s"
- TOKEN_REGEXP =
/%{(\w*)}/
- EMPTY_CONTEXT =
Object.new.tap { |ctx| def ctx.context binding end }.freeze.context
Instance Attribute Summary collapse
-
#data ⇒ Hash
readonly
Loaded localized message templates.
-
#t ⇒ Proc
readonly
Translation function.
Class Method Summary collapse
- .build(options = EMPTY_HASH) ⇒ Object private
- .cache ⇒ Object private
-
.flat_hash(hash, path = EMPTY_ARRAY, keys = {}) ⇒ Object
private
rubocop: disable Metrics/PerceivedComplexity.
- .source_cache ⇒ Object private
Instance Method Summary collapse
-
#get(key, options = EMPTY_HASH) ⇒ String
Get a message for the given key and its options.
-
#initialize(data: EMPTY_HASH, config: nil) ⇒ YAML
constructor
private
A new instance of YAML.
- #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
Get an array of looked up paths.
-
#merge(overrides) ⇒ Messages::I18n
Merge messages from an additional path.
- #prepare ⇒ Object private
Methods inherited from Abstract
#call, #default_locale, #filled_lookup_paths, #lookup_paths, #namespaced, #root, #rule, #rule_lookup_paths, setting_names, #translate
Constructor Details
#initialize(data: EMPTY_HASH, config: nil) ⇒ YAML
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.
Returns a new instance of YAML.
84 85 86 87 88 89 |
# File 'lib/dry/schema/messages/yaml.rb', line 84 def initialize(data: EMPTY_HASH, config: nil) super() @data = data @__config__ = config if config @t = proc { |key, locale: default_locale| get("%<locale>s.#{key}", locale: locale) } end |
Instance Attribute Details
#data ⇒ Hash (readonly)
Loaded localized message templates
28 29 30 |
# File 'lib/dry/schema/messages/yaml.rb', line 28 def data @data end |
#t ⇒ Proc (readonly)
Translation function
33 34 35 |
# File 'lib/dry/schema/messages/yaml.rb', line 33 def t @t end |
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.
36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/dry/schema/messages/yaml.rb', line 36 def self.build( = EMPTY_HASH) super do |config| config.default_locale = :en unless config.default_locale config.root = -"%<locale>s.#{config.root}" config.rule_lookup_paths = config.rule_lookup_paths.map { |path| -"%<locale>s.#{path}" } end 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.
72 73 74 75 76 |
# File 'lib/dry/schema/messages/yaml.rb', line 72 def self.cache @cache ||= Concurrent::Map.new do |h, k| h.compute_if_absent(k) { Concurrent::Map.new } end end |
.flat_hash(hash, path = EMPTY_ARRAY, keys = {}) ⇒ 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.
rubocop: disable Metrics/PerceivedComplexity
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/dry/schema/messages/yaml.rb', line 50 def self.flat_hash(hash, path = EMPTY_ARRAY, keys = {}) hash.each do |key, value| flat_hash(value, [*path, key], keys) if value.is_a?(Hash) if value.is_a?(String) && hash["text"] != value keys[[*path, key].join(DOT)] = { text: value, meta: EMPTY_HASH } elsif value.is_a?(Hash) && value["text"].is_a?(String) keys[[*path, key].join(DOT)] = { text: value["text"], meta: value.reject { _1.eql?("text") }.transform_keys(&:to_sym) } end end keys end |
.source_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.
79 80 81 |
# File 'lib/dry/schema/messages/yaml.rb', line 79 def self.source_cache @source_cache ||= Concurrent::Map.new end |
Instance Method Details
#get(key, options = EMPTY_HASH) ⇒ String
Get a message for the given key and its options
111 112 113 |
# File 'lib/dry/schema/messages/yaml.rb', line 111 def get(key, = EMPTY_HASH) data[evaluated_key(key, )] 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.
152 153 154 155 |
# File 'lib/dry/schema/messages/yaml.rb', line 152 def interpolatable_data(key, , **data) tokens = evaluation_context(key, ).fetch(:tokens) data.select { |k,| tokens.include?(k) } 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.
158 159 160 161 |
# File 'lib/dry/schema/messages/yaml.rb', line 158 def interpolate(key, , **data) evaluator = evaluation_context(key, ).fetch(:evaluator) data.empty? ? evaluator.() : evaluator.(**data) end |
#key?(key, options = EMPTY_HASH) ⇒ Boolean
Check if given key is defined
120 121 122 |
# File 'lib/dry/schema/messages/yaml.rb', line 120 def key?(key, = EMPTY_HASH) data.key?(evaluated_key(key, )) end |
#looked_up_paths(predicate, options) ⇒ String
Get an array of looked up paths
99 100 101 |
# File 'lib/dry/schema/messages/yaml.rb', line 99 def looked_up_paths(predicate, ) super.map { |path| path % {locale: [:locale] || default_locale} } end |
#merge(overrides) ⇒ Messages::I18n
Merge messages from an additional path
131 132 133 134 135 136 137 138 139 140 141 142 143 |
# File 'lib/dry/schema/messages/yaml.rb', line 131 def merge(overrides) if overrides.is_a?(Hash) self.class.new( data: data.merge(self.class.flat_hash(overrides)), config: config ) else self.class.new( data: Array(overrides).reduce(data) { |a, e| a.merge(load_translations(e)) }, config: config ) end end |
#prepare ⇒ 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.
146 147 148 149 |
# File 'lib/dry/schema/messages/yaml.rb', line 146 def prepare @data = config.load_paths.map { |path| load_translations(path) }.reduce({}, :merge) self end |