Class: PythonConfig::ConfigSection
- Inherits:
-
Hash
- Object
- Hash
- PythonConfig::ConfigSection
- Defined in:
- lib/pythonconfig.rb
Overview
ConfigSection
This is a simple section in a config document - treat it exactly like a hash, whose keys are the keys in the config, and whose value are the values in the config.
This is a separate class entirely because it has to handle the magical interpolation that allows this ini file:
[Awards]
output: Congratulations for winning %(prize)!
prize: the lottery
To result in:
config.sections["Awards"]["output"] == "Congratulations for winning the lottery!"
Instance Method Summary collapse
-
#[](key) ⇒ Object
:nodoc:.
-
#initialize(source) ⇒ ConfigSection
constructor
A new instance of ConfigSection.
-
#interpolate(str, cur_depth = 0) ⇒ Object
:nodoc:.
-
#to_s ⇒ Object
:nodoc:.
Constructor Details
#initialize(source) ⇒ ConfigSection
Returns a new instance of ConfigSection.
131 132 133 134 |
# File 'lib/pythonconfig.rb', line 131 def initialize(source) @source_hash = source super(@source_hash) end |
Instance Method Details
#[](key) ⇒ Object
:nodoc:
135 136 137 138 |
# File 'lib/pythonconfig.rb', line 135 def [](key) #:nodoc: str = @source_hash[key] interpolate(str) end |
#interpolate(str, cur_depth = 0) ⇒ Object
:nodoc:
140 141 142 143 144 145 146 |
# File 'lib/pythonconfig.rb', line 140 def interpolate(str, cur_depth=0) #:nodoc: raise InterpolationTooDeepError.new("Interpolation too deep!") if cur_depth > PythonConfig::MAX_INTERPOLATION_DEPTH nextval = str nextval = str.gsub(/\%\((.*)\)/,@source_hash[$1]) if str =~ /%\((.*)\)/ nextval = interpolate(nextval,cur_depth+1) if nextval =~ /%\((.*)\)/ nextval end |
#to_s ⇒ Object
:nodoc:
148 149 150 151 152 153 154 |
# File 'lib/pythonconfig.rb', line 148 def to_s #:nodoc: output = "" @source_hash.each do |k,v| output << "#{k} = #{v.gsub(/\n/,"\n ")}" << "\n" end output end |