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.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/confrb.rb +36 -23
  3. metadata +1 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: b8dfa076ab1375b93293ff60476790ab13290dabef9a11fc548bf9ec72de19b3
4
- data.tar.gz: 5cb5e6b28791faa5a5b55e20901effeee961f96c1f7e82d628d5ede60725a580
3
+ metadata.gz: 9165a2a0d73d5f050509c432b20151a6ca29d6f25d271169e61eb74a17dde60b
4
+ data.tar.gz: '02708d9d9994f7298115067e855a42d6944458e74526713f665739b04d722659'
5
5
  SHA512:
6
- metadata.gz: e756e3bddb9c8970be8ead63eafd0d499e84b4e5992c216feacf1815260c0e2e529a1b12be64461e17f6f3e7c45ec8706ad89a9bff5247e8110fc8d9f5964ae1
7
- data.tar.gz: ed0d486f91a42faed7a2b3d571b8e6c290cd2d2775792477485aa434ebb4e80a0d822e9f6e8bf209dc984d6627a981c4e3783bf1a54f2214e03f6f246e297b45
6
+ metadata.gz: 0de222c4e4fbb0f3f136645e58550387a1ff496861f7a32e51a60528088ed7b3537520de3444787cf19566a71706794c630f04899442a0523617129586f6fe43
7
+ data.tar.gz: 47b3c588de6c4372120de9c113b074a60c082844a4e3a00ab00c1f7bcda346dbd91b6dcccd700aa643d74b60d9a5b7f78423c3aefaea01e3ac9aa690eec62d1c
@@ -18,7 +18,7 @@ class Confrb
18
18
  end
19
19
 
20
20
  ##
21
- # Access the given database, prepending '.' to given name. Auto-create if doesn't exist.
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 a hash as yaml or json, or write a string
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 (ignored if content isn't a hash) Default #
38
- # false
38
+ # as JSON Default: false
39
39
  # @option opts [boolean] :yaml If true, serialize content
40
- # as YAML (ignored if content isn't a hash) Default #
41
- # false (json: true takes precedence)
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) && content.is_a?(Hash)
45
- as_yaml = opts.fetch(:yaml, false) && content.is_a?(Hash)
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 hash
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 a hash as yaml or json, or write a string
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 (ignored if content isn't a hash) Default #
130
- # false
137
+ # as JSON Default: false
131
138
  # @option opts [boolean] :yaml If true, serialize content
132
- # as YAML (ignored if content isn't a hash) Default #
133
- # false (json: true takes precedence)
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) && content.is_a?(Hash)
137
- as_yaml = opts.fetch(:yaml, false) && content.is_a?(Hash)
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 = "\n" + 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
- IO.write(cfgpath, "\n" + content, mode: "a")
160
+ File.write(cfgpath, content, mode: "a")
148
161
  else
149
- IO.write(cfgpath, "\n" + content, mode: "a")
162
+ File.write(cfgpath, content, mode: "a")
150
163
  end
151
164
  end
152
165
  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.7'
4
+ version: '2.2'
5
5
  platform: ruby
6
6
  authors:
7
7
  - Gurjus Bhasin