confrb 1.0 → 1.6

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 +141 -57
  3. metadata +4 -3
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: dce20723dba46522772c29be5d17f8895830c498b151b21e61c1987766fa073e
4
- data.tar.gz: bb4c5e003e265873d9b637ffe7e56acf5549fd3a61cd7702ab5edb928fc94d93
3
+ metadata.gz: 057e6c72fa7f625ee6856c4a8146a002c78318a9cf0e7658c5e58bf548763b25
4
+ data.tar.gz: ae506af9f374b7d125307f39e0557c38001c4e08cadecbb9df09fae9518b7f0e
5
5
  SHA512:
6
- metadata.gz: df3255415f4668326196449c68bf71d9ea52c4199ee45e406f7b2a39488b7172e67e61243a1136c22d7e5cf44107b19f4be196ca0df13521a99dce3b9fdfd658
7
- data.tar.gz: 763d109b26b7bff6c31c92a114f3402b346232ea4acb3d98412ba9ee89b3d5282d9c60a466c63754195d37a718c3026152a5de90e1119623a72eb893ae593f16
6
+ metadata.gz: 53aba1502fea37f17a3cdeb933b79ccc0b2661219217fdcf32ca6ac07bef8f8f749bad12cc3a7352ece86e52c12177bec79acff71d61d7660748e3eb3b144dfa
7
+ data.tar.gz: 59d2762f9d990829e6fc66d2b69560ea9348992646e887a19241cd45b72b7228e1ab33bcc6479329df1204e666af9623e09ef812203e5995de889f394d1e00ce
@@ -1,65 +1,149 @@
1
1
  # Require everything!
2
- require 'fileutils'
3
- require 'json'
4
- require 'yaml'
5
- module BasicCfrb
6
- extend self # Make sure this extends self so it can be used as a module!
2
+ require "fileutils"
3
+ require "json"
4
+ require "yaml"
5
+ ##
6
+ # Main class for Conf.rb
7
+ # @author gsbhasin84
8
+ class Confrb
9
+ ##
10
+ # Auto-create config storage directory, prepending '.' to
11
+ # given name. Use .new() on this class to run this! Can create subdirs recursively, but only
12
+ # root directory gets '.' prepended
13
+ # @param [String] db The name of the configuration directory to create.
14
+ def initialize(db)
15
+ @db = db
16
+ dotname = "." + @db
17
+ FileUtils.mkdir_p(dotname)
18
+ end
7
19
 
8
- def setup(name)
9
- dotname = "." + name
10
- FileUtils.mkdir_p(dotname)
11
- end
20
+ ##
21
+ # Access the given database, prepending '.' to given name. Auto-create if doesn't exist.
22
+ # @param [String] db The name of the configuration directory to access
23
+ def self.access(db)
24
+ @db = db
25
+ Confrb.new(db)
26
+ end
12
27
 
13
- def mkcfg(dir, cfgname, content, opts={})
14
- as_json=opts.fetch(:json, false)
15
- as_yaml=opts.fetch(:yaml, false)
16
- dotdir = "." + dir
17
- cfgpath = File.join(dotdir, cfgname)
18
- FileUtils.touch(cfgpath)
19
- if as_json == true
20
- content = content.to_json
21
- File.write(cfgpath, content)
22
- elsif as_yaml == true
23
- content = content.to_yaml
24
- File.write(cfgpath, content)
25
- else
26
- File.write(cfgpath, content)
27
- end
28
- end
28
+ ##
29
+ # Create/overwrite a config file and write data to it. Can
30
+ # serialize a hash as yaml or json, or write a string
31
+ # @param [String] cfgname Name of config file to write to
32
+ # @param [Hash] opts Optional param hash
33
+ # @option opts [boolean] :json If true, serialize content
34
+ # as JSON (ignored if content isn't a hash) Default #
35
+ # false
36
+ # @option opts [boolean] :yaml If true, serialize content
37
+ # as YAML (ignored if content isn't a hash) Default #
38
+ # false (json: true takes precedence)
39
+ # @return path to configuration file created.
40
+ def mkcfg(cfgname, content, opts = {})
41
+ as_json = opts.fetch(:json, false) && content.is_a?(Hash)
42
+ as_yaml = opts.fetch(:yaml, false) && content.is_a?(Hash)
43
+ dotdir = "." + @db
44
+ cfgpath = File.join(dotdir, cfgname)
45
+ FileUtils.touch(cfgpath)
46
+ if as_json == true
47
+ content = content.to_json
48
+ File.write(cfgpath, content)
49
+ elsif as_yaml == true
50
+ content = content.to_yaml
51
+ File.write(cfgpath, content)
52
+ else
53
+ File.write(cfgpath, content)
54
+ end
55
+ end
29
56
 
30
- def mknested(dir, nesteddir)
31
- dotdir = "." + dir
32
- FileUtils.mkdir_p(File.join(dotdir, nesteddir))
33
- end
57
+ ##
58
+ # Creates nested dir(s) inside a database.
59
+ # @param [String] nesteddir Nested dir(s) to create in dir
60
+ # Make sure path delimiter is OS correct, or use
61
+ # File.join
62
+ # @return [String] path to nested directory created.
63
+ def mknested(nesteddir)
64
+ dotdir = "." + @db
65
+ FileUtils.mkdir_p(File.join(dotdir, nesteddir))
66
+ end
34
67
 
35
- def readcfg(dir, cfgname, opts={})
36
- dotdir = "." + dir
37
- as_json=opts.fetch(:json, false)
38
- as_yaml=opts.fetch(:yaml, false)
39
- if as_json == true
40
- content = File.read(File.join(dotdir, cfgname)).to_json
41
- return content
42
- elsif as_yaml == true
43
- content = File.read(File.join(dotdir, cfgname)).yaml
44
- return content
45
- else
46
- File.read(File.join(dotdir, cfgname))
47
- end
48
-
49
- end
68
+ ##
69
+ # Read a config file from disk
70
+ # @param [String] cfgname Config file name to read
71
+ # @param [Hash] opts Optional param hash
72
+ # @option opts [boolean] :json If true, deserialize
73
+ # content as JSON (Default false)
74
+ # @option opts [boolean] :yaml If true, deserialize
75
+ # content as YAML. Default false (json: true takes
76
+ # precedence
77
+ # @return [String|Hash] Loaded contents, either a String
78
+ # if not indicated to treat as JSON or YAML, or a hash
79
+ # deserialized from content string
80
+ def readcfg(cfgname, opts = {})
81
+ dotdir = "." + @db
82
+ as_json = opts.fetch(:json, false)
83
+ as_yaml = opts.fetch(:yaml, false)
84
+ if as_json == true
85
+ content = JSON::load(File.read(File.join(dotdir, cfgname)))
86
+ return content
87
+ elsif as_yaml == true
88
+ content = YAML::load(File.read(File.join(dotdir, cfgname)))
89
+ return content
90
+ else
91
+ File.read(File.join(dotdir, cfgname))
92
+ end
93
+ end
50
94
 
51
- def rmcfg(dir, cfgname)
52
- dotdir = "." + dir
53
- File.delete(File.join(dotdir, cfgname))
54
- end
95
+ ##
96
+ # Remove a configuration file
97
+ # @param [String] cfgname Configuration file name, use nested directories here with File.join()
98
+ # @return [String] path to file deleted.
99
+ def rmcfg(cfgname)
100
+ dotdir = "." + @db
101
+ File.delete(File.join(dotdir, cfgname))
102
+ end
55
103
 
56
- def rmdb(dir)
57
- dotdir = "." + dir
58
- FileUtils.rm_rf(dotdir)
59
- end
104
+ ##
105
+ # Remove a whole database
106
+ # @return [String] Path to deleted database
107
+ def rmdb()
108
+ dotdir = "." + @db
109
+ FileUtils.rm_rf(dotdir)
110
+ end
60
111
 
61
- def rmnested(dir, nested)
62
- dotdir = File.join("." + dir, nested)
63
- FileUtils.rm_rf(dotdir)
64
- end
65
- end
112
+ ## Remove a nested directory
113
+ # @param [String] Nested directory path, please use File.join() if it's more than one layer deep.
114
+ # @return [String] Path to deleted nested directory
115
+ def rmnested(nested)
116
+ dotdir = File.join("." + @db, nested)
117
+ FileUtils.rm_rf(dotdir)
118
+ end
119
+
120
+ ##
121
+ # Create/append to a config file. Can
122
+ # serialize a hash as yaml or json, or write a string
123
+ # @param [String] cfgname Name of config file to write to
124
+ # @param [Hash] opts Optional param hash
125
+ # @option opts [boolean] :json If true, serialize content
126
+ # as JSON (ignored if content isn't a hash) Default #
127
+ # false
128
+ # @option opts [boolean] :yaml If true, serialize content
129
+ # as YAML (ignored if content isn't a hash) Default #
130
+ # false (json: true takes precedence)
131
+ # @return path to configuration file created.
132
+ def writecfg(cfgname, content, opts = {})
133
+ as_json = opts.fetch(:json, false) && content.is_a?(Hash)
134
+ as_yaml = opts.fetch(:yaml, false) && content.is_a?(Hash)
135
+ dotdir = "." + @db
136
+ cfgpath = File.join(dotdir, cfgname)
137
+ FileUtils.touch(cfgpath)
138
+ if as_json == true
139
+ content = content.to_json
140
+ content = "\n" + content
141
+ IO.write(cfgpath, "\n" + content, mode: "a")
142
+ elsif as_yaml == true
143
+ content = content.to_yaml
144
+ IO.write(cfgpath, "\n" + content, mode: "a")
145
+ else
146
+ IO.write(cfgpath, "\n" + content, mode: "a")
147
+ end
148
+ end
149
+ 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.6'
5
5
  platform: ruby
6
6
  authors:
7
7
  - Gurjus Bhasin
@@ -11,8 +11,9 @@ cert_chain: []
11
11
  date: 2019-12-18 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: A Ruby Gem which allows you to store configuration files using hidden
14
- directories. Plaintext, unencrypted. Useful for simple text adventure games, etc.
15
- Docs and examples at https://gitlab.com/Gsbhasin84/confrb
14
+ directories. Supports JSON, YAML, and plaintext. Data is unencrypted, but you can
15
+ hash/encrypt data yourself before passing it to be written. Useful for simple text
16
+ adventure games, etc. Docs and examples at https://gitlab.com/Gsbhasin84/confrb
16
17
  email: gsbhasin84@gmail.com
17
18
  executables: []
18
19
  extensions: []