Class: Confoog::Settings

Inherits:
Object
  • Object
show all
Defined in:
lib/confoog.rb

Overview

Class : Settings. Provide an encapsulated class to access a YAML configuration file. Readers : .filename = read the config filename for this instance .location = read the config directory for this instance .status = Hash containing assorted status variables, read only.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Settings

Returns a new instance of Settings.



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/confoog.rb', line 45

def initialize(options = {})
  # merge default options to avoid ambiguity
  @options = DEFAULT_OPTIONS.merge(options)
  # set all other unset options to return false instead of Nul.
  @options.default = false

  @status = {}
  @location = @options[:location]
  @filename = @options[:filename]

  @config = {}

  # clear the error condition as default.
  status_set(errors: ERR_NO_ERROR)
  # make sure the file exists or can be created...
  check_exists(options)
end

Instance Attribute Details

#filenameObject

Returns the value of attribute filename.



43
44
45
# File 'lib/confoog.rb', line 43

def filename
  @filename
end

#locationObject

Returns the value of attribute location.



43
44
45
# File 'lib/confoog.rb', line 43

def location
  @location
end

#statusObject (readonly)

Returns the value of attribute status.



43
44
45
# File 'lib/confoog.rb', line 43

def status
  @status
end

Instance Method Details

#[](key) ⇒ Object



112
113
114
# File 'lib/confoog.rb', line 112

def [](key)
  @config[key]
end

#[]=(key, value) ⇒ Object



116
117
118
# File 'lib/confoog.rb', line 116

def []=(key, value)
  @config[key] = value
end

#config_pathObject



120
121
122
# File 'lib/confoog.rb', line 120

def config_path
  File.expand_path(File.join(@location, @filename))
end

#loadObject



81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/confoog.rb', line 81

def load
  @config = YAML.load_file(config_path)
  status_set(errors: INFO_FILE_LOADED)
  if @config == false
    console_output("Configuration file #{config_path} is empty!",
                   OUTPUT_SEVERITY[:WARN])
    status_set(errors: ERR_NOT_LOADING_EMPTY_FILE)
  end
rescue
  console_output("Cannot load configuration data from #{config_path}",
                 OUTPUT_SEVERITY[:ERR])
end

#quietObject



63
64
65
# File 'lib/confoog.rb', line 63

def quiet
  @options[:quiet]
end

#quiet=(quiet) ⇒ Object



67
68
69
# File 'lib/confoog.rb', line 67

def quiet=(quiet)
  @options[:quiet] = quiet
end

#saveObject



71
72
73
74
75
76
77
78
79
# File 'lib/confoog.rb', line 71

def save
  if @config.count > 0
    save_to_yaml
  else
    console_output("Not saving empty configuration data to #{config_path}",
                   OUTPUT_SEVERITY[:WARN])
    status_set(errors: ERR_NOT_WRITING_EMPTY_FILE)
  end
end