Class: Confrb
- Inherits:
-
Object
- Object
- Confrb
- Defined in:
- lib/confrb.rb
Overview
Class Method Summary collapse
-
.access(db) ⇒ Object
Access the given database, prepending ‘.’ to given name.
Instance Method Summary collapse
-
#initialize(db) ⇒ Confrb
constructor
Auto-create config storage directory, prepending ‘.’ to given name.
-
#mkcfg(cfgname, content = '', opts = {}) ⇒ Object
Create/overwrite a config file and write data to it.
-
#mknested(nesteddir) ⇒ String
Creates nested dir(s) inside a database.
-
#readcfg(cfgname, opts = {}) ⇒ String|Hash
Read a config file from disk.
-
#rmcfg(cfgname) ⇒ String
Remove a configuration file.
-
#rmdb ⇒ String
Remove a whole database.
-
#rmnested(nested) ⇒ String
Remove a nested directory.
-
#writecfg(cfgname, content, opts = {}) ⇒ Object
Create/append to a config file and write data to it.
Constructor Details
#initialize(db) ⇒ Confrb
Auto-create config storage directory, prepending ‘.’ to given name. Use .new() on this class to run this! Can create subdirs recursively, but only root directory gets ‘.’ prepended
14 15 16 17 18 |
# File 'lib/confrb.rb', line 14 def initialize(db) @db = db dotname = "." + @db FileUtils.mkdir_p(dotname) end |
Class Method Details
Instance Method Details
#mkcfg(cfgname, content = '', opts = {}) ⇒ Object
Create/overwrite a config file and write data to it. Can serialize data as yaml or json, or write a string.
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/confrb.rb', line 43 def mkcfg(cfgname, content='', opts = {}) as_json = opts.fetch(:json, false) as_yaml = opts.fetch(:yaml, false) newline = opts.fetch(:newline, false) content = "\n" + content if newline = true dotdir = "." + @db cfgpath = File.join(dotdir, cfgname) FileUtils.touch(cfgpath) if as_json == true content = content.to_json File.write(cfgpath, content) elsif as_yaml == true content = content.to_yaml File.write(cfgpath, content) else File.write(cfgpath, content) end end |
#mknested(nesteddir) ⇒ String
Creates nested dir(s) inside a database.
68 69 70 71 |
# File 'lib/confrb.rb', line 68 def mknested(nesteddir) dotdir = "." + @db FileUtils.mkdir_p(File.join(dotdir, nesteddir)) end |
#readcfg(cfgname, opts = {}) ⇒ String|Hash
Read a config file from disk
85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/confrb.rb', line 85 def readcfg(cfgname, opts = {}) dotdir = "." + @db as_json = opts.fetch(:json, false) as_yaml = opts.fetch(:yaml, false) if as_json == true content = JSON::load(File.read(File.join(dotdir, cfgname))) return content elsif as_yaml == true content = YAML::load(File.read(File.join(dotdir, cfgname))) return content else File.read(File.join(dotdir, cfgname)) end end |
#rmcfg(cfgname) ⇒ String
Remove a configuration file
104 105 106 107 |
# File 'lib/confrb.rb', line 104 def rmcfg(cfgname) dotdir = "." + @db File.delete(File.join(dotdir, cfgname)) end |
#rmdb ⇒ String
Remove a whole database
112 113 114 115 |
# File 'lib/confrb.rb', line 112 def rmdb() dotdir = "." + @db FileUtils.rm_rf(dotdir) end |
#rmnested(nested) ⇒ String
Remove a nested directory
120 121 122 123 |
# File 'lib/confrb.rb', line 120 def rmnested(nested) dotdir = File.join("." + @db, nested) FileUtils.rm_rf(dotdir) end |
#writecfg(cfgname, content, opts = {}) ⇒ Object
Create/append to a config file and write data to it. Can serialize data as yaml or json, or write a string. Writes a newline at the beginning.
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 |
# File 'lib/confrb.rb', line 137 def writecfg(cfgname, content, opts = {}) as_json = opts.fetch(:json, false) as_yaml = opts.fetch(:yaml, false) newline = opts.fetch(:newline, true) dotdir = "." + @db cfgpath = File.join(dotdir, cfgname) FileUtils.touch(cfgpath) content = "\n" + content if newline == true if as_json == true content = content.to_json IO.write(cfgpath, content, mode: "a") elsif as_yaml == true content = content.to_yaml IO.write(cfgpath, content, mode: "a") else IO.write(cfgpath, content, mode: "a") end end |