Class: Dry::Validation::Messages::Abstract

Inherits:
Object
  • Object
show all
Extended by:
Configurable
Defined in:
lib/dry/validation/messages/abstract.rb

Direct Known Subclasses

I18n, Namespaced, YAML

Constant Summary collapse

DEFAULT_PATH =
Pathname(__dir__).join('../../../../config/errors.yml').realpath.freeze
CACHE_KEYS =
%i[path message_type val_type arg_type locale].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeAbstract

Returns a new instance of Abstract.



54
55
56
# File 'lib/dry/validation/messages/abstract.rb', line 54

def initialize
  @config = self.class.config
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



52
53
54
# File 'lib/dry/validation/messages/abstract.rb', line 52

def config
  @config
end

Class Method Details

.cacheObject



48
49
50
# File 'lib/dry/validation/messages/abstract.rb', line 48

def self.cache
  @cache ||= Concurrent::Map.new { |h, k| h[k] = Concurrent::Map.new }
end

Instance Method Details

#cacheObject



109
110
111
# File 'lib/dry/validation/messages/abstract.rb', line 109

def cache
  @cache ||= self.class.cache[self]
end

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



67
68
69
70
71
72
73
74
# File 'lib/dry/validation/messages/abstract.rb', line 67

def call(predicate, options = EMPTY_HASH)
  cache.fetch_or_store([predicate, options.reject { |k,| k.equal?(:input) }]) do
    path, opts = lookup(predicate, options)
    return unless path
    text = yield(path, opts)
    Template[text]
  end
end

#default_localeObject



113
114
115
# File 'lib/dry/validation/messages/abstract.rb', line 113

def default_locale
  :en
end

#hashObject



58
59
60
# File 'lib/dry/validation/messages/abstract.rb', line 58

def hash
  @hash ||= config.hash
end

#lookup(predicate, options = {}) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/dry/validation/messages/abstract.rb', line 77

def lookup(predicate, options = {})
  tokens = options.merge(
    root: root,
    predicate: predicate,
    arg_type: config.arg_types[options[:arg_type]],
    val_type: config.val_types[options[:val_type]],
    message_type: options[:message_type] || :failure
  )

  tokens[:rule] = predicate unless tokens.key?(:rule)

  opts = options.select { |k, _| !config.lookup_options.include?(k) }

  path = lookup_paths(tokens).detect do |key|
    key?(key, opts) && get(key, opts).is_a?(String)
  end

  [path, opts]
end

#lookup_paths(tokens) ⇒ Object



97
98
99
# File 'lib/dry/validation/messages/abstract.rb', line 97

def lookup_paths(tokens)
  config.lookup_paths.map { |path| path % tokens }
end

#namespaced(namespace) ⇒ Object



101
102
103
# File 'lib/dry/validation/messages/abstract.rb', line 101

def namespaced(namespace)
  Messages::Namespaced.new(namespace, self)
end

#rootObject



105
106
107
# File 'lib/dry/validation/messages/abstract.rb', line 105

def root
  config.root
end

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



62
63
64
65
# File 'lib/dry/validation/messages/abstract.rb', line 62

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