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