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'
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
- .flat_hash(hash, path = [], keys = {}) ⇒ 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.
-
#key?(key, options = EMPTY_HASH) ⇒ Boolean
Check if given key is defined.
-
#merge(overrides) ⇒ Messages::I18n
Merge messages from an additional path.
- #prepare ⇒ Object private
Methods inherited from Abstract
cache, #cache, #call, #default_locale, #lookup, #lookup_paths, #namespaced, #root, #rule, #rule_lookup_paths, #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.
64 65 66 67 68 69 |
# File 'lib/dry/schema/messages/yaml.rb', line 64 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
23 24 25 |
# File 'lib/dry/schema/messages/yaml.rb', line 23 def data @data end |
#t ⇒ Proc (readonly)
Translation function
28 29 30 |
# File 'lib/dry/schema/messages/yaml.rb', line 28 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.
31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/dry/schema/messages/yaml.rb', line 31 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 |
.flat_hash(hash, path = [], 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.
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/dry/schema/messages/yaml.rb', line 44 def self.flat_hash(hash, path = [], 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.dup.delete_if { |k| k == 'text' }.map { |k, v| [k.to_sym, v] }.to_h } end end keys end |
Instance Method Details
#get(key, options = EMPTY_HASH) ⇒ String
Get a message for the given key and its options
79 80 81 |
# File 'lib/dry/schema/messages/yaml.rb', line 79 def get(key, = EMPTY_HASH) data[evaluated_key(key, )] end |
#key?(key, options = EMPTY_HASH) ⇒ Boolean
Check if given key is defined
88 89 90 |
# File 'lib/dry/schema/messages/yaml.rb', line 88 def key?(key, = EMPTY_HASH) data.key?(evaluated_key(key, )) end |
#merge(overrides) ⇒ Messages::I18n
Merge messages from an additional path
99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/dry/schema/messages/yaml.rb', line 99 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.
114 115 116 117 |
# File 'lib/dry/schema/messages/yaml.rb', line 114 def prepare @data = config.load_paths.map { |path| load_translations(path) }.reduce(:merge) self end |