Class: NormalizeXml::Config

Inherits:
KtCfg::CfgFile
  • Object
show all
Defined in:
lib/normalizexml/config.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(rootDir = nil) ⇒ Config

Returns a new instance of Config.



22
23
24
25
26
27
28
# File 'lib/normalizexml/config.rb', line 22

def initialize(rootDir=nil)
  $LOG.debug "Config::initialize"
  super
  @cfg = {}

  setDefaults()
end

Instance Attribute Details

#cfgObject

Returns the value of attribute cfg.



18
19
20
# File 'lib/normalizexml/config.rb', line 18

def cfg
  @cfg
end

#cfgFile=(value) ⇒ Object (writeonly)

Sets the attribute cfgFile

Parameters:

  • value

    the value to set the attribute cfgFile to.



19
20
21
# File 'lib/normalizexml/config.rb', line 19

def cfgFile=(value)
  @cfgFile = value
end

Instance Method Details

#loadObject

Load the YAML configuration file.

returns

a hash containing configuration info.



75
76
77
78
79
80
# File 'lib/normalizexml/config.rb', line 75

def load
  $LOG.debug "Config::load"
  tmpCfg = read(@cfgFile)
  @cfg = tmpCfg if !tmpCfg.nil? && !tmpCfg.empty?
  @cfg
end

#saveObject

Save the @cfg hash to a YAML file.



84
85
86
87
# File 'lib/normalizexml/config.rb', line 84

def save
  $LOG.debug "Config::save"
  write(@cfgFile, @cfg)
end

#setDefaultsObject



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/normalizexml/config.rb', line 31

def setDefaults
  $LOG.debug "Config::setDefaults"

  # Notes about APPDATA paths:
  # Local app data should be used when an app's data is too
  # big to move around. Or is specific to the machine running
  # the application.
  #
  # Roaming app data files could be pushed to a server (in a
  # domain environment) and downloaded onto a different work
  # station.
  #
  # LocalLow is used for data that must be sandboxed. Currently
  # it is only used by IE for addons and storing data from
  # untrusted sources (as far as I know).
  #


  appDataPath = ENV["APPDATA"]          # APPDATA returns AppData\Roaming on Vista/W7
  appDataPath ||= ENV["HOME"]          # APPDATA returns AppData\Roaming on Vista/W7
  #appDataPath = ENV["LOCALAPPDATA"]        # LOCALAPPDATA returns AppData\Local on Vista/W7
  appDataPath = File.rubypath(File.join(appDataPath, "normalizexml"))
  @cfg[:appPath] = appDataPath
  @cfg[:version]  = NormalizeXml::VERSION
  @cfg[:logging]  = false

  @cfgFile = "normalizexml.yml"

  # Set the config file path. Default is the 'global' one in APPDATA.
  if( @rootDir.nil? )
    @rootDir = appDataPath
    @cfgFile = "config.yml"

    # Override the gobal config if there is a local (current working dir) version.
    if(File.exists?(File.join(FileUtils.pwd(), "normalizexml.yml")))
      @rootDir = FileUtils.pwd()
      @cfgFile = "normalizexml.yml"
    end
  end
end