Class: Dry::Schema::Messages::YAML
- Defined in:
- lib/dry/schema/messages/yaml.rb
Overview
Plain YAML message backend
Constant Summary
Constants inherited from Abstract
Instance Attribute Summary collapse
- #data ⇒ Object readonly
Attributes inherited from Abstract
Class Method Summary collapse
- .flat_hash(h, f = [], g = {}) ⇒ Object private
- .load(paths = config.paths) ⇒ Object private
- .load_file(path) ⇒ Object private
Instance Method Summary collapse
-
#get(key, options = {}) ⇒ String
Get a message for the given key and its options.
-
#initialize(data) ⇒ YAML
constructor
private
A new instance of YAML.
-
#key?(key, options = {}) ⇒ Boolean
Check if given key is defined.
-
#merge(overrides) ⇒ Messages::I18n
Merge messages from an additional path.
Methods inherited from Abstract
cache, #cache, #call, #default_locale, #hash, #lookup, #lookup_paths, #namespaced, #root, #rule
Constructor Details
#initialize(data) ⇒ 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.
40 41 42 43 |
# File 'lib/dry/schema/messages/yaml.rb', line 40 def initialize(data) super() @data = data end |
Instance Attribute Details
#data ⇒ Object (readonly)
15 16 17 |
# File 'lib/dry/schema/messages/yaml.rb', line 15 def data @data end |
Class Method Details
.flat_hash(h, f = [], g = {}) ⇒ 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.
33 34 35 36 37 |
# File 'lib/dry/schema/messages/yaml.rb', line 33 def self.flat_hash(h, f = [], g = {}) return g.update(f.join('.'.freeze) => h) unless h.is_a? Hash h.each { |k, r| flat_hash(r, f + [k], g) } g end |
.load(paths = config.paths) ⇒ 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.
23 24 25 |
# File 'lib/dry/schema/messages/yaml.rb', line 23 def self.load(paths = config.paths) new(paths.map { |path| load_file(path) }.reduce(:merge)) end |
.load_file(path) ⇒ 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.
28 29 30 |
# File 'lib/dry/schema/messages/yaml.rb', line 28 def self.load_file(path) flat_hash(YAML.load_file(path)) end |
Instance Method Details
#get(key, options = {}) ⇒ String
Get a message for the given key and its options
53 54 55 |
# File 'lib/dry/schema/messages/yaml.rb', line 53 def get(key, = {}) data[key % { locale: .fetch(:locale, default_locale) }] end |
#key?(key, options = {}) ⇒ Boolean
Check if given key is defined
62 63 64 |
# File 'lib/dry/schema/messages/yaml.rb', line 62 def key?(key, = {}) data.key?(key % { locale: .fetch(:locale, default_locale) }) end |
#merge(overrides) ⇒ Messages::I18n
Merge messages from an additional path
73 74 75 76 77 78 79 |
# File 'lib/dry/schema/messages/yaml.rb', line 73 def merge(overrides) if overrides.is_a?(Hash) self.class.new(data.merge(self.class.flat_hash(overrides))) else self.class.new(data.merge(Messages::YAML.load_file(overrides))) end end |