confrb 1.7 → 2.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.
- checksums.yaml +4 -4
- data/lib/confrb.rb +36 -23
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 9165a2a0d73d5f050509c432b20151a6ca29d6f25d271169e61eb74a17dde60b
|
4
|
+
data.tar.gz: '02708d9d9994f7298115067e855a42d6944458e74526713f665739b04d722659'
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 0de222c4e4fbb0f3f136645e58550387a1ff496861f7a32e51a60528088ed7b3537520de3444787cf19566a71706794c630f04899442a0523617129586f6fe43
|
7
|
+
data.tar.gz: 47b3c588de6c4372120de9c113b074a60c082844a4e3a00ab00c1f7bcda346dbd91b6dcccd700aa643d74b60d9a5b7f78423c3aefaea01e3ac9aa690eec62d1c
|
data/lib/confrb.rb
CHANGED
@@ -18,7 +18,7 @@ class Confrb
|
|
18
18
|
end
|
19
19
|
|
20
20
|
##
|
21
|
-
# Access the given database, prepending '.' to given name.
|
21
|
+
# Access the given database, prepending '.' to given name. Raises an exception if nonexistent.
|
22
22
|
# @param [String] db The name of the configuration directory to access
|
23
23
|
def self.access(db)
|
24
24
|
if Dir.exist? '.' + db
|
@@ -30,19 +30,26 @@ class Confrb
|
|
30
30
|
|
31
31
|
##
|
32
32
|
# Create/overwrite a config file and write data to it. Can
|
33
|
-
# serialize
|
34
|
-
# @param [String] cfgname Name of config file to write to
|
33
|
+
# serialize data as yaml or json, or write a string.
|
34
|
+
# @param [String] cfgname Name of config file to write to
|
35
|
+
# @param [String] content The content to write, defaults to an empty string.
|
35
36
|
# @param [Hash] opts Optional param hash
|
36
37
|
# @option opts [boolean] :json If true, serialize content
|
37
|
-
# as JSON
|
38
|
-
# false
|
38
|
+
# as JSON Default: false
|
39
39
|
# @option opts [boolean] :yaml If true, serialize content
|
40
|
-
# as YAML
|
41
|
-
#
|
40
|
+
# as YAML Default: false (json: true takes precedence)
|
41
|
+
# @option opts [boolean] :newline If true, prepends a newline to the beginning of the content. Default: false
|
42
42
|
# @return path to configuration file created.
|
43
|
-
def mkcfg(cfgname, content, opts = {})
|
44
|
-
as_json = opts.fetch(:json, false)
|
45
|
-
as_yaml = opts.fetch(:yaml, false)
|
43
|
+
def mkcfg(cfgname, content='', opts = {})
|
44
|
+
as_json = opts.fetch(:json, false)
|
45
|
+
as_yaml = opts.fetch(:yaml, false)
|
46
|
+
pnewline = opts.fetch(:prepend_newline, false)
|
47
|
+
anewline = opts.fetch(:append_newline, false)
|
48
|
+
if pnewline == true
|
49
|
+
content = "\n" + content
|
50
|
+
elsif anewline == true
|
51
|
+
content = content + "\n"
|
52
|
+
end
|
46
53
|
dotdir = "." + @db
|
47
54
|
cfgpath = File.join(dotdir, cfgname)
|
48
55
|
FileUtils.touch(cfgpath)
|
@@ -78,7 +85,7 @@ class Confrb
|
|
78
85
|
# content as YAML. Default false (json: true takes
|
79
86
|
# precedence
|
80
87
|
# @return [String|Hash] Loaded contents, either a String
|
81
|
-
# if not indicated to treat as JSON or YAML, or a
|
88
|
+
# if not indicated to treat as JSON or YAML, or a data structure
|
82
89
|
# deserialized from content string
|
83
90
|
def readcfg(cfgname, opts = {})
|
84
91
|
dotdir = "." + @db
|
@@ -121,32 +128,38 @@ class Confrb
|
|
121
128
|
end
|
122
129
|
|
123
130
|
##
|
124
|
-
# Create/append to a config file. Can
|
125
|
-
# serialize
|
131
|
+
# Create/append to a config file and write data to it. Can
|
132
|
+
# serialize data as yaml or json, or write a string. Writes a newline at the beginning.
|
126
133
|
# @param [String] cfgname Name of config file to write to
|
134
|
+
# @param [String] content The content to write, defaults to an empty string.
|
127
135
|
# @param [Hash] opts Optional param hash
|
128
136
|
# @option opts [boolean] :json If true, serialize content
|
129
|
-
# as JSON
|
130
|
-
# false
|
137
|
+
# as JSON Default: false
|
131
138
|
# @option opts [boolean] :yaml If true, serialize content
|
132
|
-
# as YAML
|
133
|
-
#
|
139
|
+
# as YAML Default: false (json: true takes precedence)
|
140
|
+
# @option opts [boolean] :newline If true, prepend a newline to content. Default: true
|
134
141
|
# @return path to configuration file created.
|
135
142
|
def writecfg(cfgname, content, opts = {})
|
136
|
-
as_json = opts.fetch(:json, false)
|
137
|
-
as_yaml = opts.fetch(:yaml, false)
|
143
|
+
as_json = opts.fetch(:json, false)
|
144
|
+
as_yaml = opts.fetch(:yaml, false)
|
145
|
+
pnewline = opts.fetch(:prepend_newline, true)
|
146
|
+
anewline = opts.fetch(:append_newline, false)
|
138
147
|
dotdir = "." + @db
|
139
148
|
cfgpath = File.join(dotdir, cfgname)
|
140
149
|
FileUtils.touch(cfgpath)
|
150
|
+
if pnewline == true
|
151
|
+
content = "\n" + content
|
152
|
+
elsif anewline == true
|
153
|
+
content = content + "\n"
|
154
|
+
end
|
141
155
|
if as_json == true
|
142
156
|
content = content.to_json
|
143
|
-
content
|
144
|
-
IO.write(cfgpath, "\n" + content, mode: "a")
|
157
|
+
File.write(cfgpath, content, mode: "a")
|
145
158
|
elsif as_yaml == true
|
146
159
|
content = content.to_yaml
|
147
|
-
|
160
|
+
File.write(cfgpath, content, mode: "a")
|
148
161
|
else
|
149
|
-
|
162
|
+
File.write(cfgpath, content, mode: "a")
|
150
163
|
end
|
151
164
|
end
|
152
165
|
end
|