Class: Configural::Config::SDLFile

Inherits:
FileBase
  • Object
show all
Defined in:
lib/configural/config/sdl_file.rb

Overview

Implementation of FileBase using SDL (Simple Declarative Language) for serialization.

You must have the ‘sdl4r’ library installed to use this format. See: <sdl4r.rubyforge.org/>

Instance Attribute Summary

Attributes inherited from FileBase

#path

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from FileBase

#[], #[]=, #clear, #close, #delete, #each, #exists?, get_format_by_extname, get_format_by_name, inherited, #keys, #load, #load!, #save, #save!, #to_hash

Constructor Details

#initialize(*args) ⇒ SDLFile

Returns a new instance of SDLFile.



49
50
51
52
# File 'lib/configural/config/sdl_file.rb', line 49

def initialize(*args)
  require 'sdl4r'
  super
end

Class Method Details

.extnamesObject



44
45
46
# File 'lib/configural/config/sdl_file.rb', line 44

def self.extnames
  ['.sdl']
end

.formatObject



40
41
42
# File 'lib/configural/config/sdl_file.rb', line 40

def self.format
  'sdl'
end

Instance Method Details

#_loadObject



55
56
57
58
59
60
61
62
63
64
# File 'lib/configural/config/sdl_file.rb', line 55

def _load
  root = File.open(path, 'r'){ |f| SDL4R.read(f) }
  @data = tag_to_hash( root )
rescue Errno::ENOENT
  @data = {}
rescue SDL4R::SdlParseError, Errno::EACCESS => e
  warn( "WARNING: Could not load config file #{path.inspect}:\n" +
        e.inspect + "\nUsing empty dataset instead." )
  @data = {}
end

#_saveObject



67
68
69
70
71
72
73
74
75
76
# File 'lib/configural/config/sdl_file.rb', line 67

def _save
  require 'fileutils'
  FileUtils.mkdir_p( File.dirname(path) )
  File.open(path, 'w') { |f|
    hash_to_tag(@data).write(f)
  }
rescue ArgumentError, Errno::EACCES => e
  warn( "WARNING: Could not save config file #{path.inspect}:\n" +
        e.inspect )
end