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 collapse

LOCALE_TOKEN =
"%<locale>s"
TOKEN_REGEXP =
/%{(\w*)}/.freeze
EMPTY_CONTEXT =
Object.new.tap { |ctx|
  def ctx.context
    binding
  end
}.freeze.context

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Abstract

#call, #default_locale, #filled_lookup_paths, #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.



80
81
82
83
84
85
# File 'lib/dry/schema/messages/yaml.rb', line 80

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

#dataHash (readonly)

Loaded localized message templates

Returns:

  • (Hash)


29
30
31
# File 'lib/dry/schema/messages/yaml.rb', line 29

def data
  @data
end

#tProc (readonly)

Translation function

Returns:

  • (Proc)


34
35
36
# File 'lib/dry/schema/messages/yaml.rb', line 34

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.



37
38
39
40
41
42
43
44
45
46
47
# File 'lib/dry/schema/messages/yaml.rb', line 37

def self.build(options = 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

.cacheObject

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.



70
71
72
# File 'lib/dry/schema/messages/yaml.rb', line 70

def self.cache
  @cache ||= Concurrent::Map.new { |h, k| h[k] = Concurrent::Map.new }
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.



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/dry/schema/messages/yaml.rb', line 50

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

.source_cacheObject

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.



75
76
77
# File 'lib/dry/schema/messages/yaml.rb', line 75

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

Parameters:

  • key (Symbol)
  • options (Hash) (defaults to: EMPTY_HASH)

Returns:

  • (String)


107
108
109
# File 'lib/dry/schema/messages/yaml.rb', line 107

def get(key, options = EMPTY_HASH)
  data[evaluated_key(key, options)]
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.



148
149
150
151
# File 'lib/dry/schema/messages/yaml.rb', line 148

def interpolatable_data(key, options, **data)
  tokens = evaluation_context(key, options).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.



154
155
156
157
# File 'lib/dry/schema/messages/yaml.rb', line 154

def interpolate(key, options, **data)
  evaluator = evaluation_context(key, options).fetch(:evaluator)
  data.empty? ? evaluator.() : evaluator.(**data)
end

#key?(key, options = EMPTY_HASH) ⇒ Boolean

Check if given key is defined

Returns:

  • (Boolean)


116
117
118
# File 'lib/dry/schema/messages/yaml.rb', line 116

def key?(key, options = EMPTY_HASH)
  data.key?(evaluated_key(key, options))
end

#looked_up_paths(predicate, options) ⇒ String

Get an array of looked up paths

Parameters:

  • predicate (Symbol)
  • options (Hash)

Returns:

  • (String)


95
96
97
# File 'lib/dry/schema/messages/yaml.rb', line 95

def looked_up_paths(predicate, options)
  super.map { |path| path % {locale: options[:locale] || default_locale} }
end

#merge(overrides) ⇒ Messages::I18n

Merge messages from an additional path

Parameters:

  • overrides (String)

Returns:



127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/dry/schema/messages/yaml.rb', line 127

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

#prepareObject

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.



142
143
144
145
# File 'lib/dry/schema/messages/yaml.rb', line 142

def prepare
  @data = config.load_paths.map { |path| load_translations(path) }.reduce({}, :merge)
  self
end