Class: ConfigFile
- Inherits:
-
Object
- Object
- ConfigFile
- Defined in:
- lib/helpers.rb
Instance Attribute Summary collapse
-
#new ⇒ Object
readonly
Returns the value of attribute new.
Instance Method Summary collapse
-
#filename ⇒ Object
Returns the filename (name only!) of the configfile.
-
#initialize(path) ⇒ ConfigFile
constructor
Path should be a String.
- #intro ⇒ Object
- #outro ⇒ Object
- #read ⇒ Object
-
#write ⇒ Object
Puts the Configfile-text into the given file, with all the options as-is.
Constructor Details
#initialize(path) ⇒ ConfigFile
Path should be a String. Creates or opens a new configfile and immediately writes it back again, to add new features etc (just in case).
253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 |
# File 'lib/helpers.rb', line 253 def initialize(path) @path = path @template = ConfigTemplate.new @template.read_template(SMARBS::DATADIR + "/configuration.txt") if File.file?(@path) raise @path + " is not writable!" if not File.writable?(@path) read write else if Directory.writable?(File.dirname(@path)) # Create example file FileUtils.makedirs(File.dirname(@path)) # Create directory write puts "First run:\nConfig file created, edit " + @path + "!" @new = true else raise(File.dirname(@path) + " is not writable!") end end end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args) ⇒ Object (private)
319 320 321 |
# File 'lib/helpers.rb', line 319 def method_missing(name, *args) eval "@template.#{name}(args[0])" end |
Instance Attribute Details
#new ⇒ Object (readonly)
Returns the value of attribute new.
249 250 251 |
# File 'lib/helpers.rb', line 249 def new @new end |
Instance Method Details
#filename ⇒ Object
Returns the filename (name only!) of the configfile.
275 276 277 |
# File 'lib/helpers.rb', line 275 def filename return File.basename(@path) end |
#intro ⇒ Object
288 289 290 291 292 293 294 295 |
# File 'lib/helpers.rb', line 288 def intro txt = nil File.open(SMARBS::DATADIR + "/intro.txt", "r") do |file| txt = file.readlines end txt[0] = txt[0].strip + " #{SMARBS::VERSION} (#{SMARBS::DATE})\n" txt.join end |
#outro ⇒ Object
297 298 299 300 301 302 303 |
# File 'lib/helpers.rb', line 297 def outro txt = nil File.open(SMARBS::DATADIR + "/outro.txt", "r") do |file| txt = file.readlines end txt.join end |
#read ⇒ Object
305 306 307 308 309 310 311 312 313 314 315 |
# File 'lib/helpers.rb', line 305 def read File.open(@path, "r") do |file| lines = file.readlines index1 = lines.index{|string| string.include? "-preferences-"} index2 = lines.index{|string| string.include? "-end of preferences-"} raise "Configfile #{@path} is invalid. Please delete it!" if index1 == nil or index2 == nil @template.clear @template.parse(lines[index1..index2]) end end |
#write ⇒ Object
Puts the Configfile-text into the given file, with all the options as-is.
280 281 282 283 284 285 286 |
# File 'lib/helpers.rb', line 280 def write file=File.open(@path, "w") file.puts intro file.puts @template file.puts outro file.close end |