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 Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeAbstract

Returns a new instance of Abstract.



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

def initialize
  @config = self.class.config
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



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

def config
  @config
end

Class Method Details

.cacheObject



43
44
45
# File 'lib/dry/validation/messages/abstract.rb', line 43

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

Instance Method Details

#cacheObject



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

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

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



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

def call(*args)
  cache.fetch_or_store(args.hash) do
    path, opts = lookup(*args)
    get(path, opts) if path
  end
end

#default_localeObject



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

def default_locale
  :en
end

#hashObject



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

def hash
  @hash ||= config.hash
end

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



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/dry/validation/messages/abstract.rb', line 70

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



90
91
92
# File 'lib/dry/validation/messages/abstract.rb', line 90

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

#namespaced(namespace) ⇒ Object



94
95
96
# File 'lib/dry/validation/messages/abstract.rb', line 94

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

#rootObject



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

def root
  config.root
end

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



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

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