Class: Confrb

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

Overview

Author:

  • gsbhasin84

Class Method Summary collapse

Instance Method Summary collapse

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

Parameters:

  • db (String)

    The name of the configuration directory to create.



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

.access(db) ⇒ Object

Access the given database, prepending ‘.’ to given name. Raises an exception if nonexistent.

Parameters:

  • db (String)

    The name of the configuration directory to access



23
24
25
26
27
28
29
# File 'lib/confrb.rb', line 23

def self.access(db)
  if Dir.exist? '.' + db
    Confrb.new db
  else
    raise "Directory does not exist!"
  end
end

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.

Parameters:

  • cfgname (String)

    Name of config file to write to

  • content (String) (defaults to: '')

    The content to write, defaults to an empty string.

  • opts (Hash) (defaults to: {})

    Optional param hash

Options Hash (opts):

  • :json (boolean)

    If true, serialize content as JSON Default: false

  • :yaml (boolean)

    If true, serialize content as YAML Default: false (json: true takes precedence)

  • :newline (boolean)

    If true, prepends a newline to the beginning of the content. Default: false

Returns:

  • path to configuration file created.



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.

Parameters:

  • nesteddir (String)

    Nested dir(s) to create in dir Make sure path delimiter is OS correct, or use File.join

Returns:

  • (String)

    path to nested directory created.



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

Parameters:

  • cfgname (String)

    Config file name to read

  • opts (Hash) (defaults to: {})

    Optional param hash

Options Hash (opts):

  • :json (boolean)

    If true, deserialize content as JSON (Default false)

  • :yaml (boolean)

    If true, deserialize content as YAML. Default false (json: true takes precedence

Returns:

  • (String|Hash)

    Loaded contents, either a String if not indicated to treat as JSON or YAML, or a data structure deserialized from content string



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

Parameters:

  • cfgname (String)

    Configuration file name, use nested directories here with File.join()

Returns:

  • (String)

    path to file deleted.



104
105
106
107
# File 'lib/confrb.rb', line 104

def rmcfg(cfgname)
  dotdir = "." + @db
  File.delete(File.join(dotdir, cfgname))
end

#rmdbString

Remove a whole database

Returns:

  • (String)

    Path to deleted 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

Parameters:

  • Nested (String)

    directory path, please use File.join() if it’s more than one layer deep.

Returns:

  • (String)

    Path to deleted 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.

Parameters:

  • cfgname (String)

    Name of config file to write to

  • content (String)

    The content to write, defaults to an empty string.

  • opts (Hash) (defaults to: {})

    Optional param hash

Options Hash (opts):

  • :json (boolean)

    If true, serialize content as JSON Default: false

  • :yaml (boolean)

    If true, serialize content as YAML Default: false (json: true takes precedence)

  • :newline (boolean)

    If true, prepend a newline to content. Default: true

Returns:

  • path to configuration file created.



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