Class: Fones::Config
- Inherits:
-
Object
- Object
- Fones::Config
- Defined in:
- lib/fones/config.rb
Overview
Reads/Writes a configuration file in the user’s home directory
Instance Attribute Summary collapse
-
#config ⇒ Object
Returns the value of attribute config.
Instance Method Summary collapse
-
#[](var) ⇒ Object
Provides access to the config using the Hash square brackets.
-
#[]=(var, value) ⇒ Object
Allows modifying variables through hash square brackets.
-
#config_file ⇒ Object
Returns the path to the user’s configuration file.
-
#initialize ⇒ Config
constructor
A new instance of Config.
-
#read ⇒ Object
Loads config declarations in user’s home dir.
-
#write(options = {}) ⇒ Object
Writes the configuration file.
Constructor Details
#initialize ⇒ Config
Returns a new instance of Config.
12 13 14 15 16 17 18 19 20 |
# File 'lib/fones/config.rb', line 12 def initialize() @config = { :theme => { :author => nil, :author_url => nil, }, :links => [] } end |
Instance Attribute Details
#config ⇒ Object
Returns the value of attribute config.
10 11 12 |
# File 'lib/fones/config.rb', line 10 def config @config end |
Instance Method Details
#[](var) ⇒ Object
Provides access to the config using the Hash square brackets
23 24 25 |
# File 'lib/fones/config.rb', line 23 def [](var) @config[var] end |
#[]=(var, value) ⇒ Object
Allows modifying variables through hash square brackets
28 29 30 |
# File 'lib/fones/config.rb', line 28 def []=(var, value) @config[var] = value end |
#config_file ⇒ Object
Returns the path to the user’s configuration file
33 34 35 |
# File 'lib/fones/config.rb', line 33 def config_file @config_file ||= File.(File.join('~', '.fones', 'config.yml')) end |
#read ⇒ Object
Loads config declarations in user’s home dir
If file does not exist then it will be created
55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/fones/config.rb', line 55 def read return write unless File.exists?(self.config_file) # data = File.open(self.config_file).read @config = YAML::load_file(self.config_file) unless self.config_file.empty? # @config = data.empty? ? {} : JSON.parse(data) # @config = data.empty? ? {} : YAML::parse(data) self end |
#write(options = {}) ⇒ Object
Writes the configuration file
38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/fones/config.rb', line 38 def write(={}) # If we're unit testing then it helps to use a # StringIO object instead of a file buffer io = [:io] || File.open(self.config_file, 'w') # io.write JSON.generate(@config) io.write @config.to_yaml io.close self end |