Class: Dry::Validation::Messages::I18n

Inherits:
Abstract
  • Object
show all
Defined in:
lib/dry/validation/messages/i18n.rb

Constant Summary

Constants inherited from Abstract

Abstract::CACHE_KEYS, Abstract::DEFAULT_PATH

Instance Attribute Summary collapse

Attributes inherited from Abstract

#config

Instance Method Summary collapse

Methods inherited from Abstract

cache, #cache, #hash, #lookup, #lookup_paths, #namespaced, #root

Constructor Details

#initializeI18n

Returns a new instance of I18n.



11
12
13
14
# File 'lib/dry/validation/messages/i18n.rb', line 11

def initialize
  super
  @t = I18n.method(:t)
end

Instance Attribute Details

#tObject (readonly)

Returns the value of attribute t.



7
8
9
# File 'lib/dry/validation/messages/i18n.rb', line 7

def t
  @t
end

Instance Method Details

#call(predicate, options = EMPTY_HASH) ⇒ Object Also known as: []



16
17
18
19
20
# File 'lib/dry/validation/messages/i18n.rb', line 16

def call(predicate, options = EMPTY_HASH)
  super do |path, opts|
    get(path, opts)
  end
end

#default_localeObject



42
43
44
# File 'lib/dry/validation/messages/i18n.rb', line 42

def default_locale
  I18n.locale || I18n.default_locale || super
end

#get(key, options = {}) ⇒ Object



23
24
25
# File 'lib/dry/validation/messages/i18n.rb', line 23

def get(key, options = {})
  t.(key, locale: options.fetch(:locale, default_locale)) if key
end

#key?(key, options) ⇒ Boolean

Returns:

  • (Boolean)


32
33
34
35
# File 'lib/dry/validation/messages/i18n.rb', line 32

def key?(key, options)
  ::I18n.exists?(key, options.fetch(:locale, default_locale)) ||
  ::I18n.exists?(key, I18n.default_locale)
end

#merge(path) ⇒ Object



37
38
39
40
# File 'lib/dry/validation/messages/i18n.rb', line 37

def merge(path)
  ::I18n.load_path << path
  self
end

#rule(name, options = {}) ⇒ Object



27
28
29
30
# File 'lib/dry/validation/messages/i18n.rb', line 27

def rule(name, options = {})
  path = "rules.#{name}"
  get(path, options) if key?(path, options)
end