Module: RecordStore::Zone::YamlDefinitions

Includes:
Enumerable
Included in:
RecordStore::Zone
Defined in:
lib/record_store/zone/yaml_definitions.rb

Instance Method Summary collapse

Instance Method Details

#[](name) ⇒ Object



16
17
18
# File 'lib/record_store/zone/yaml_definitions.rb', line 16

def [](name)
  defined.fetch(name)
end

#allObject



6
7
8
# File 'lib/record_store/zone/yaml_definitions.rb', line 6

def all
  defined.values
end

#definedObject



10
11
12
13
14
# File 'lib/record_store/zone/yaml_definitions.rb', line 10

def defined
  @defined ||= yaml_files
    .map { |file| load_yml_zone_definition(file) }
    .index_by { |zone| zone.unrooted_name }
end

#each(&block) ⇒ Object



20
21
22
# File 'lib/record_store/zone/yaml_definitions.rb', line 20

def each(&block)
  defined.each(&block)
end

#find(name) ⇒ Object



24
25
26
# File 'lib/record_store/zone/yaml_definitions.rb', line 24

def find(name)
  defined[name]
end

#resetObject



28
29
30
# File 'lib/record_store/zone/yaml_definitions.rb', line 28

def reset
  @defined = nil
end

#write(name, config:, records:, format: :file) ⇒ Object

Raises:

  • (ArgumentError)


32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/record_store/zone/yaml_definitions.rb', line 32

def write(name, config:, records:, format: :file)
  raise ArgumentError, "format must be :directory or :file" unless i(file directory).include?(format)
  name = name.chomp('.')
  zone_file = "#{RecordStore.zones_path}/#{name}.yml"
  zone = { name => { config: config.to_hash } }
  records = records.map(&:to_hash).sort_by! {|r| [r.fetch(:fqdn), r.fetch(:type), r[:nsdname] || r[:address]]}

  if format == :file
    zone[name][:records] = records
    write_yml_file(zone_file, zone.deep_stringify_keys)
    remove_record_files(name)
  else
    write_yml_file(zone_file, zone.deep_stringify_keys)
    remove_record_files(name)
    write_record_files(name, records)
  end
end