Class: PDK::Config::IniFile

Inherits:
Namespace show all
Defined in:
lib/pdk/config/ini_file.rb

Overview

Represents a configuration file using the INI file format

Defined Under Namespace

Classes: IniFileImpl

Instance Attribute Summary

Attributes inherited from Namespace

#file, #name, #parent

Instance Method Summary collapse

Methods inherited from Namespace

#[], #[]=, #child_namespace?, #fetch, #include_in_parent?, #initialize, #mount, #namespace, #read_only!, #resolve, #setting, #to_h

Constructor Details

This class inherits a constructor from PDK::Config::Namespace

Instance Method Details

#default_setting_classObject

Ini Files have very strict valdiation rules which are set in the IniFileSetting class

See Also:

  • Namespace.default_setting_class


9
10
11
# File 'lib/pdk/config/ini_file.rb', line 9

def default_setting_class
  PDK::Config::IniFileSetting
end

#parse_file(filename) ⇒ Object

Parses an IniFile document.

See Also:

  • Namespace.parse_file


16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/pdk/config/ini_file.rb', line 16

def parse_file(filename)
  raise unless block_given?

  data = load_data(filename)
  return if data.nil? || data.empty?

  ini_file = IniFileImpl.parse(data)
  ini_file.to_hash.each do |name, value|
    begin
      new_setting = PDK::Config::IniFileSetting.new(name, self, value)
    rescue StandardError
      # We just ignore invalid initial settings
      new_setting = PDK::Config::IniFileSetting.new(name, self, nil)
    end

    yield name, new_setting
  end
end

#serialize_data(data) ⇒ Object

Serializes object data into an INI file string.

See Also:

  • Namespace.serialize_data


38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/pdk/config/ini_file.rb', line 38

def serialize_data(data)
  default_lines = ''
  lines = ''
  data.each do |name, value|
    next if value.nil?

    if value.is_a?(Hash)
      # Hashes are an INI section
      lines += "\n[#{name}]\n"
      value.each do |child_name, child_value|
        next if child_value.nil?

        lines += "#{child_name} = #{munge_serialized_value(child_value)}\n"
      end
    else
      default_lines += "#{name} = #{munge_serialized_value(value)}\n"
    end
  end

  default_lines + lines
end