confrb 1.0 → 1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/confrb.rb +61 -6
  3. metadata +1 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: dce20723dba46522772c29be5d17f8895830c498b151b21e61c1987766fa073e
4
- data.tar.gz: bb4c5e003e265873d9b637ffe7e56acf5549fd3a61cd7702ab5edb928fc94d93
3
+ metadata.gz: 8669ce1e7a01579da2df989086b283994ee2b968d11825f1d0b17671d793ecff
4
+ data.tar.gz: a7454823e9cdcf8ef7394140c8c2e60f1b1dbe99b3d65a4f29ac27df3de0b8de
5
5
  SHA512:
6
- metadata.gz: df3255415f4668326196449c68bf71d9ea52c4199ee45e406f7b2a39488b7172e67e61243a1136c22d7e5cf44107b19f4be196ca0df13521a99dce3b9fdfd658
7
- data.tar.gz: 763d109b26b7bff6c31c92a114f3402b346232ea4acb3d98412ba9ee89b3d5282d9c60a466c63754195d37a718c3026152a5de90e1119623a72eb893ae593f16
6
+ metadata.gz: 215e76892ad6659844edb61cf38693eda6f61128a6a9343418fdff520813c76460bf978d6c4664bf04bb65438963719078ac80a148cd4b689712d5fc1744f127
7
+ data.tar.gz: ce1104e3b5980a62a6b4e895ace5ea22ce800cb245cffe86cc9b2425643fd8bfe256aaa8e246faffa03e72732039b15b4c12364605858601d0bd03793dd5145d
@@ -2,17 +2,38 @@
2
2
  require 'fileutils'
3
3
  require 'json'
4
4
  require 'yaml'
5
+ ##
6
+ # Main module for BasicCfrb
7
+ # @author gsbhasin84
5
8
  module BasicCfrb
6
9
  extend self # Make sure this extends self so it can be used as a module!
7
10
 
11
+ ##
12
+ # Create config storage directory, appending '.' to
13
+ # given name. Can create subdirs recursively, but only
14
+ # root directory gets '.' prepended
15
+ # @param [String] name The name of the configuration directory to create.
8
16
  def setup(name)
9
17
  dotname = "." + name
10
18
  FileUtils.mkdir_p(dotname)
11
19
  end
12
20
 
21
+ ##
22
+ # Create/overwrite a config file and write data to it. Can
23
+ # serialize a hash as yaml or json, or write a string
24
+ # @param [String] dir Path to store config file in
25
+ # @param [String] cfgname Name of config file to write to
26
+ # @param [Hash] opts Optional param hash
27
+ # @option opts [boolean] :json If true, serialize content
28
+ # as JSON (ignored if content isn't a hash) Default #
29
+ # false
30
+ # @option opts [boolean] :yaml If true, serialize content
31
+ # as YAML (ignored if content isn't a hash) Default #
32
+ # false (json: true takes precedence
33
+ # @return path to configuration file created.
13
34
  def mkcfg(dir, cfgname, content, opts={})
14
- as_json=opts.fetch(:json, false)
15
- as_yaml=opts.fetch(:yaml, false)
35
+ as_json=opts.fetch(:json, false) && content.is_a?(Hash)
36
+ as_yaml=opts.fetch(:yaml, false) && content.is_a?(Hash)
16
37
  dotdir = "." + dir
17
38
  cfgpath = File.join(dotdir, cfgname)
18
39
  FileUtils.touch(cfgpath)
@@ -27,39 +48,73 @@ module BasicCfrb
27
48
  end
28
49
  end
29
50
 
51
+ ##
52
+ # Creates nested dir(s) inside a configuration folder set up with setup(). If no configuration folder has been set up, creates one.
53
+ # @param [String] dir Setup/base folder; has '.' prepended
54
+ # to its name, should be what used in setup() and auto-created if setup() was not run with the specific name passed.
55
+ # @param [String] nesteddir Nested dir(s) to create in dir
56
+ # Make sure path delimiter is OS correct, or use
57
+ # File.join
58
+ # @return [String] path to nested directory created.
30
59
  def mknested(dir, nesteddir)
31
60
  dotdir = "." + dir
32
61
  FileUtils.mkdir_p(File.join(dotdir, nesteddir))
33
62
  end
34
63
 
64
+ ##
65
+ # Read a config file from disk
66
+ # @param [String] dir Setup dir name (without '.' prefix)
67
+ # @param [String] cfgname Config file name to read
68
+ # @param [Hash] opts Optional param hash
69
+ # @option opts [boolean] :json If true, deserialize
70
+ # content as JSON (Default false)
71
+ # @option opts [boolean] :yaml If true, deserialize
72
+ # content as YAML. Default false (json: true takes
73
+ # precedence
74
+ # @return [String|Hash] Loaded contents, either a String
75
+ # if not indicated to treat as JSON or YAML, or a hash
76
+ # deserialized from content string
35
77
  def readcfg(dir, cfgname, opts={})
36
78
  dotdir = "." + dir
37
79
  as_json=opts.fetch(:json, false)
38
80
  as_yaml=opts.fetch(:yaml, false)
39
81
  if as_json == true
40
- content = File.read(File.join(dotdir, cfgname)).to_json
82
+ content = JSON::load(File.read(File.join(dotdir, cfgname)))
41
83
  return content
42
84
  elsif as_yaml == true
43
- content = File.read(File.join(dotdir, cfgname)).yaml
85
+ content = YAML::load(File.read(File.join(dotdir, cfgname)))
44
86
  return content
45
87
  else
46
88
  File.read(File.join(dotdir, cfgname))
47
89
  end
48
90
 
49
91
  end
50
-
92
+
93
+ ##
94
+ # Remove a configuration file
95
+ # @param [String] dir Setup directory name, without the . prefix
96
+ # @param [String] cfgname Configuration file name, use nested directories here with File.join()
97
+ # @return [String] path to file deleted.
51
98
  def rmcfg(dir, cfgname)
52
99
  dotdir = "." + dir
53
100
  File.delete(File.join(dotdir, cfgname))
54
101
  end
55
102
 
103
+ ##
104
+ # Remove a whole database
105
+ # @param dir The configuration directory/database to be deleted, without the '.'
106
+ # @return Path to deleted database
56
107
  def rmdb(dir)
57
108
  dotdir = "." + dir
58
109
  FileUtils.rm_rf(dotdir)
59
110
  end
60
111
 
112
+ ## Remove a nested directory
113
+ # @param dir Root configuration directory
114
+ # param nested Nested directory path, please use File.join() if it's more than one layer deep.
115
+ # @return Path to deleted nested directory
61
116
  def rmnested(dir, nested)
62
117
  dotdir = File.join("." + dir, nested)
63
118
  FileUtils.rm_rf(dotdir)
64
119
  end
65
- end
120
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: confrb
3
3
  version: !ruby/object:Gem::Version
4
- version: '1.0'
4
+ version: '1.2'
5
5
  platform: ruby
6
6
  authors:
7
7
  - Gurjus Bhasin