Class: Configurations::Maps::Types

Inherits:
Object
  • Object
show all
Defined in:
lib/configurations/maps/types.rb

Defined Under Namespace

Classes: Entry

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(reader = Readers::Tolerant.new) ⇒ Types

Returns a new instance of Types.



17
18
19
20
# File 'lib/configurations/maps/types.rb', line 17

def initialize(reader = Readers::Tolerant.new)
  @map = {}
  @reader = reader
end

Instance Attribute Details

#mapObject (readonly)

Returns the value of attribute map.



4
5
6
# File 'lib/configurations/maps/types.rb', line 4

def map
  @map
end

Instance Method Details

#add(type, properties) ⇒ Object



22
23
24
25
26
# File 'lib/configurations/maps/types.rb', line 22

def add(type, properties)
  properties.each do |property|
    add_entry(property, type, @map)
  end
end

#add_entry(property, type, subtree) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/configurations/maps/types.rb', line 39

def add_entry(property, type, subtree)
  if property.is_a?(Hash)
    property.each do |key, val|
      subtree[key] = add_entry(val, type, subtree.fetch(key, {}))
    end
  elsif property.is_a?(Array)
    property.each do |val|
      add_entry(val, type, subtree)
    end
  else
    subtree[property] = Entry.new(type)
  end

  subtree
end

#test!(path, value) ⇒ Object



28
29
30
31
32
33
34
35
36
37
# File 'lib/configurations/maps/types.rb', line 28

def test!(path, value)
  entry = @reader.read(@map, path)
  return unless entry

  fail(
    ConfigurationError,
    "#{path.print} must be configured with #{entry.type} (got #{value})",
    caller
  ) unless entry.valid?(value)
end