Class: Confoog::Settings
- Inherits:
-
Object
- Object
- Confoog::Settings
- 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
-
#filename ⇒ Object
Returns the value of attribute filename.
-
#location ⇒ Object
Returns the value of attribute location.
-
#status ⇒ Object
readonly
Returns the value of attribute status.
Instance Method Summary collapse
- #[](key) ⇒ Object
- #[]=(key, value) ⇒ Object
- #config_path ⇒ Object
-
#initialize(options = {}) ⇒ Settings
constructor
A new instance of Settings.
- #load ⇒ Object
- #quiet ⇒ Object
- #quiet=(quiet) ⇒ Object
- #save ⇒ Object
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( = {}) # merge default options to avoid ambiguity @options = DEFAULT_OPTIONS.merge() # 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() end |
Instance Attribute Details
#filename ⇒ Object
Returns the value of attribute filename.
43 44 45 |
# File 'lib/confoog.rb', line 43 def filename @filename end |
#location ⇒ Object
Returns the value of attribute location.
43 44 45 |
# File 'lib/confoog.rb', line 43 def location @location end |
#status ⇒ Object (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_path ⇒ Object
120 121 122 |
# File 'lib/confoog.rb', line 120 def config_path File.(File.join(@location, @filename)) end |
#load ⇒ Object
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 |
#quiet ⇒ Object
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 |
#save ⇒ Object
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 |