Class: GithubBackup::Config
- Inherits:
-
Object
- Object
- GithubBackup::Config
- Defined in:
- lib/github_backup/config.rb
Instance Attribute Summary collapse
-
#override ⇒ Object
Returns the value of attribute override.
Instance Method Summary collapse
-
#[](key) ⇒ Object
Read a value from the config ex: config # => value.
-
#[]=(key, value) ⇒ Object
Set a value in the config ex: config = value.
- #data ⇒ Object
- #data_dir ⇒ Object
- #data_path ⇒ Object
- #defaults ⇒ Object
-
#delete! ⇒ Object
Removes the saved config.
-
#dir ⇒ Object
The user’s preferred config dir.
-
#exists? ⇒ Boolean
Check if the config exists.
-
#home ⇒ Object
The user’s home directory.
-
#initialize(default_path) ⇒ Config
constructor
A new instance of Config.
-
#path ⇒ Object
the path to the config file.
-
#save ⇒ Object
Write config to disk at the user’s config dir.
-
#to_s ⇒ Object
writes the config json to stdout.
- #values ⇒ Object
Constructor Details
#initialize(default_path) ⇒ Config
Returns a new instance of Config.
9 10 11 12 13 |
# File 'lib/github_backup/config.rb', line 9 def initialize(default_path) @override = {} @default_path = default_path puts "Config: #{path}" end |
Instance Attribute Details
#override ⇒ Object
Returns the value of attribute override.
7 8 9 |
# File 'lib/github_backup/config.rb', line 7 def override @override end |
Instance Method Details
#[](key) ⇒ Object
Read a value from the config ex: config # => value
61 62 63 |
# File 'lib/github_backup/config.rb', line 61 def [](key) @override[key] || values[key] || defaults[key] end |
#[]=(key, value) ⇒ Object
Set a value in the config ex: config = value
67 68 69 70 |
# File 'lib/github_backup/config.rb', line 67 def []=(key, value) values[key] = value values.save end |
#data ⇒ Object
27 28 29 |
# File 'lib/github_backup/config.rb', line 27 def data @data ||= JSONStore.new(data_path) end |
#data_dir ⇒ Object
46 47 48 |
# File 'lib/github_backup/config.rb', line 46 def data_dir ENV['XDG_DATA_HOME'] || File.join(home, '.local', 'share') end |
#data_path ⇒ Object
50 51 52 |
# File 'lib/github_backup/config.rb', line 50 def data_path File.join(data_dir, "#{APP}.json") end |
#defaults ⇒ Object
15 16 17 18 19 20 21 |
# File 'lib/github_backup/config.rb', line 15 def defaults if @default_path @defaults ||= JSONStore.new(@default_path) else {} end end |
#delete! ⇒ Object
Removes the saved config
79 80 81 |
# File 'lib/github_backup/config.rb', line 79 def delete! values.delete! end |
#dir ⇒ Object
The user’s preferred config dir
42 43 44 |
# File 'lib/github_backup/config.rb', line 42 def dir ENV['XDG_CONFIG_HOME'] || File.join(home, '.config') end |
#exists? ⇒ Boolean
Check if the config exists
32 33 34 |
# File 'lib/github_backup/config.rb', line 32 def exists? values.exists? end |
#home ⇒ Object
The user’s home directory
37 38 39 |
# File 'lib/github_backup/config.rb', line 37 def home ENV['HOME'] || File.('~') end |
#path ⇒ Object
the path to the config file
55 56 57 |
# File 'lib/github_backup/config.rb', line 55 def path File.join(dir, "#{APP}.json") end |
#save ⇒ Object
Write config to disk at the user’s config dir. Doesn’t save overrides
74 75 76 |
# File 'lib/github_backup/config.rb', line 74 def save values.save end |
#to_s ⇒ Object
writes the config json to stdout
84 85 86 |
# File 'lib/github_backup/config.rb', line 84 def to_s values.to_s end |