Class: Configurations::Maps::Blocks

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

Defined Under Namespace

Classes: Entry

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Blocks.



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

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

Instance Method Details

#add(block, properties) ⇒ Object



27
28
29
30
31
# File 'lib/configurations/maps/blocks.rb', line 27

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

#add_default(block) ⇒ Object



23
24
25
# File 'lib/configurations/maps/blocks.rb', line 23

def add_default(block)
  @default = Entry.new(block)
end

#add_entry(property, block, subtree) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/configurations/maps/blocks.rb', line 45

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

  subtree
end

#entries_at(path) ⇒ Object



33
34
35
36
# File 'lib/configurations/maps/blocks.rb', line 33

def entries_at(path)
  entries = @reader.read(@map, path) || {}
  entries.dup.keep_if { |_, v| v.is_a?(Entry) }
end

#evaluate!(path, value) ⇒ Object



38
39
40
41
42
43
# File 'lib/configurations/maps/blocks.rb', line 38

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

  entry.evaluate!(value)
end