Class: Configural::Config::YAMLFile

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

Overview

Implementation of FileBase using YAML for serialization.

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) ⇒ YAMLFile

Returns a new instance of YAMLFile.



45
46
47
48
# File 'lib/configural/config/yaml_file.rb', line 45

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

Class Method Details

.extnamesObject



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

def self.extnames
  ['.yml', '.yaml']
end

.formatObject



36
37
38
# File 'lib/configural/config/yaml_file.rb', line 36

def self.format
  'yaml'
end

Instance Method Details

#_loadObject



50
51
52
53
54
55
56
57
58
# File 'lib/configural/config/yaml_file.rb', line 50

def _load
  @data = YAML.load_file(path) || {}
rescue Errno::ENOENT
  @data = {}
rescue ArgumentError, Errno::EACCESS => e
  warn( "WARNING: Could not load config file #{path.inspect}:\n" +
        e.inspect + "\nUsing empty dataset instead." )
  @data = {}
end

#_saveObject



60
61
62
63
64
65
66
67
68
69
# File 'lib/configural/config/yaml_file.rb', line 60

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