Class: Dry::Schema::Messages::YAML

Inherits:
Abstract
  • Object
show all
Defined in:
lib/dry/schema/messages/yaml.rb

Overview

Plain YAML message backend

Constant Summary

Constants inherited from Abstract

Abstract::DEFAULT_PATH

Instance Attribute Summary collapse

Attributes inherited from Abstract

#config

Class Method Summary collapse

Instance Method Summary collapse

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

#dataObject (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

Parameters:

  • key (Symbol)
  • options (Hash) (defaults to: {})

Returns:

  • (String)


53
54
55
# File 'lib/dry/schema/messages/yaml.rb', line 53

def get(key, options = {})
  data[key % { locale: options.fetch(:locale, default_locale) }]
end

#key?(key, options = {}) ⇒ Boolean

Check if given key is defined

Returns:

  • (Boolean)


62
63
64
# File 'lib/dry/schema/messages/yaml.rb', line 62

def key?(key, options = {})
  data.key?(key % { locale: options.fetch(:locale, default_locale) })
end

#merge(overrides) ⇒ Messages::I18n

Merge messages from an additional path

Parameters:

  • path (String)

Returns:



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