Class: Dry::Validation::Messages::YAML

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

Constant Summary

Constants inherited from Abstract

Abstract::DEFAULT_PATH

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Abstract

#cache, #call, #config, #lookup, #lookup_paths, #namespaced, #root

Constructor Details

#initialize(data) ⇒ YAML

Returns a new instance of YAML.



29
30
31
# File 'lib/dry/validation/messages/yaml.rb', line 29

def initialize(data)
  @data = data
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



9
10
11
# File 'lib/dry/validation/messages/yaml.rb', line 9

def data
  @data
end

Class Method Details

.flat_hash(h, f = [], g = {}) ⇒ Object



23
24
25
26
27
# File 'lib/dry/validation/messages/yaml.rb', line 23

def self.flat_hash(h, f = [], g = {})
  return g.update(f.join('.'.freeze) => h) unless h.is_a? Hash
  h.each { |k, r| flat_hash(r, f + [k], g) }
  g
end

.load(paths = config.paths) ⇒ Object



15
16
17
# File 'lib/dry/validation/messages/yaml.rb', line 15

def self.load(paths = config.paths)
  new(paths.map { |path| load_file(path) }.reduce(:merge))
end

.load_file(path) ⇒ Object



19
20
21
# File 'lib/dry/validation/messages/yaml.rb', line 19

def self.load_file(path)
  flat_hash(YAML.load_file(path))
end

Instance Method Details

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



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

def get(key, _options = {})
  data[key]
end

#key?(key, *args) ⇒ Boolean

Returns:

  • (Boolean)


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

def key?(key, *args)
  data.key?(key)
end

#merge(overrides) ⇒ Object



41
42
43
44
45
46
47
# File 'lib/dry/validation/messages/yaml.rb', line 41

def merge(overrides)
  if overrides.is_a?(Hash)
    self.class.new(data.merge(self.class.flat_hash(overrides)))
  else
    self.class.new(data.merge(Messages::YAML.load_file(overrides)))
  end
end