Class: NxtSchema::Validators::ErrorMessages

Inherits:
Object
  • Object
show all
Defined in:
lib/nxt_schema/validators/error_messages.rb

Class Method Summary collapse

Class Method Details

.filesObject



30
31
32
33
34
35
36
37
38
# File 'lib/nxt_schema/validators/error_messages.rb', line 30

def files
  @files ||= begin
    files = Dir.entries(File.expand_path('../error_messages/', __FILE__)).map do |filename|
      File.expand_path("../error_messages/#{filename}", __FILE__)
    end

    files.select { |f| !File.directory? f }
  end
end

.load(paths = files) ⇒ Object



13
14
15
16
17
18
# File 'lib/nxt_schema/validators/error_messages.rb', line 13

def load(paths = files)
  Array(paths).each do |path|
    new_values = YAML.load(ERB.new(File.read(path)).result).with_indifferent_access
    self.values = values.deep_merge!(new_values)
  end
end

.resolve(locale, key, **options) ⇒ Object



20
21
22
23
24
25
26
27
28
# File 'lib/nxt_schema/validators/error_messages.rb', line 20

def resolve(locale, key, **options)
  message = begin
    values.fetch(locale).fetch(key)
  rescue KeyError
    raise "Could not resolve error message for #{locale}->#{key}"
  end

  message % options
end

.valuesObject



5
6
7
# File 'lib/nxt_schema/validators/error_messages.rb', line 5

def values
  @values ||= {}
end

.values=(value) ⇒ Object



9
10
11
# File 'lib/nxt_schema/validators/error_messages.rb', line 9

def values=(value)
  @values = value
end