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

Instance Method Summary collapse

Instance Method Details

#cacheObject



72
73
74
# File 'lib/dry/validation/messages/abstract.rb', line 72

def cache
  @cache ||= ThreadSafe::Cache.new
end

#call(*args) ⇒ Object Also known as: []



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

def call(*args)
  cache.fetch_or_store(args.hash) { get(*lookup(*args)) }
end

#configObject



68
69
70
# File 'lib/dry/validation/messages/abstract.rb', line 68

def config
  self.class.config
end

#lookup(predicate, options) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/dry/validation/messages/abstract.rb', line 42

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]]
  )

  opts = options.reject { |k, _| config.lookup_options.include?(k) }
  path = lookup_paths(tokens).detect { |key| key?(key, opts) && get(key).is_a?(String) }

  [path, opts]
end

#lookup_paths(tokens) ⇒ Object



56
57
58
# File 'lib/dry/validation/messages/abstract.rb', line 56

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

#namespaced(namespace) ⇒ Object



60
61
62
# File 'lib/dry/validation/messages/abstract.rb', line 60

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

#rootObject



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

def root
  config.root
end