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.
- checksums.yaml +4 -4
- data/lib/confrb.rb +141 -57
- metadata +4 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 057e6c72fa7f625ee6856c4a8146a002c78318a9cf0e7658c5e58bf548763b25
|
4
|
+
data.tar.gz: ae506af9f374b7d125307f39e0557c38001c4e08cadecbb9df09fae9518b7f0e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 53aba1502fea37f17a3cdeb933b79ccc0b2661219217fdcf32ca6ac07bef8f8f749bad12cc3a7352ece86e52c12177bec79acff71d61d7660748e3eb3b144dfa
|
7
|
+
data.tar.gz: 59d2762f9d990829e6fc66d2b69560ea9348992646e887a19241cd45b72b7228e1ab33bcc6479329df1204e666af9623e09ef812203e5995de889f394d1e00ce
|
data/lib/confrb.rb
CHANGED
@@ -1,65 +1,149 @@
|
|
1
1
|
# Require everything!
|
2
|
-
require
|
3
|
-
require
|
4
|
-
require
|
5
|
-
|
6
|
-
|
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
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
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
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
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
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
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
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
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
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
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
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
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
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
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.
|
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.
|
15
|
-
|
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: []
|