Class: PDK::Config::IniFile::IniFileImpl
- Inherits:
-
Object
- Object
- PDK::Config::IniFile::IniFileImpl
show all
- Defined in:
- lib/pdk/config/ini_file.rb
Overview
Defined Under Namespace
Modules: LineNumber
Classes: DefaultSection, Line, SectionLine, SettingLine
Constant Summary
collapse
- DEFAULT_SECTION_NAME =
'default_section_name'.freeze
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(lines = []) ⇒ IniFileImpl
Returns a new instance of IniFileImpl.
92
93
94
|
# File 'lib/pdk/config/ini_file.rb', line 92
def initialize(lines = [])
@lines = lines
end
|
Class Method Details
.parse(config_fh) ⇒ Object
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
|
# File 'lib/pdk/config/ini_file.rb', line 76
def self.parse(config_fh)
config = new([DefaultSection.new])
config_fh.each_line do |line|
case line.chomp
when /^(\s*)\[([[:word:]]+)\](\s*)$/
config.append(SectionLine.new($1, $2, $3))
when /^(\s*)([[:word:]]+)(\s*=\s*)(.*?)(\s*)$/
config.append(SettingLine.new($1, $2, $3, $4, $5))
else
config.append(Line.new(line))
end
end
config
end
|
Instance Method Details
#append(line) ⇒ Object
123
124
125
126
|
# File 'lib/pdk/config/ini_file.rb', line 123
def append(line)
line.previous = @lines.last
@lines << line
end
|
#munge_value(value) ⇒ Object
116
117
118
119
120
121
|
# File 'lib/pdk/config/ini_file.rb', line 116
def munge_value(value)
value = value.to_s unless value.is_a?(String)
value = value.slice(1...-1) if value.start_with?('"') && value.end_with?('"')
value
end
|
#to_hash ⇒ Object
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
|
# File 'lib/pdk/config/ini_file.rb', line 96
def to_hash
result = {}
current_section_name = nil
@lines.each do |line|
if line.instance_of?(SectionLine)
current_section_name = line.name
result[current_section_name] = {}
elsif line.instance_of?(SettingLine)
if current_section_name.nil?
result[line.name] = munge_value(line.value)
else
result[current_section_name][line.name] = munge_value(line.value)
end
end
end
result
end
|