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.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/confrb.rb +104 -113
  3. metadata +4 -3
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 39cea4513710fa1d095a863d9b2b189909ed920d794f38c6609d7cfba3b89cac
4
- data.tar.gz: 1bc7b1543c1c93a3e779f2aec41a3293b805f1d939cd7225c181ad15cb08214b
3
+ metadata.gz: 2421f27329b335dcb84beee53886400ba784e6f6144072f22fe5aea9ad5c16da
4
+ data.tar.gz: f754385d70cdc66174d7ee2e244f51f4dbfe4e123da8e77ee93155c1ec6b328b
5
5
  SHA512:
6
- metadata.gz: 87cc3cb961ddadbc6970d88d1516d0f86a50467ccc4ba178f2d24614014eb4492dbd16ff61f00bb46e5e014efa169a8a2d3f41baf4e64357c47d78e53fae9f94
7
- data.tar.gz: eab6f9b6fd989ad5fea2f77b812e64d9d5d203be3b42f3ce001c18b9b7d85fcc72bba281a3514993e0dd229434e28fa719719026ce1d8df26f1835d3142ac96c
6
+ metadata.gz: 67ac93c027ffe4786e5e60eb51e6477aeb03155f079bcb2fca9eedf61cbeda3b40db324fe42842f02895bc958d0f99f4e4854031b64f6a1d64a202e6aabc43ea
7
+ data.tar.gz: 6916e335a2e2d5bf7685d9f363592441d3d73d4a25490061d79b43a4ef4d97d30af00be461d080ccc0e005c4407f56fe8e01440fbbf6174399037674bba000c5
data/lib/confrb.rb CHANGED
@@ -1,151 +1,142 @@
1
1
  # Require everything!
2
- require 'fileutils'
3
- require 'json'
4
- require 'yaml'
2
+ require "fileutils"
3
+ require "json"
4
+ require "yaml"
5
5
  ##
6
- # Main module for BasicCfrb
6
+ # Main class for Conf.rb
7
7
  # @author gsbhasin84
8
- module BasicCfrb
9
- extend self # Make sure this extends self so it can be used as a module!
8
+ class BasicCfrb
10
9
 
11
10
  ##
12
- # Create config storage directory, appending '.' to
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] name The name of the configuration directory to create.
16
- def setup(name)
17
- dotname = "." + name
18
- FileUtils.mkdir_p(dotname)
19
- end
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
- def mkcfg(dir, cfgname, content, opts={})
35
- as_json=opts.fetch(:json, false) && content.is_a?(Hash)
36
- as_yaml=opts.fetch(:yaml, false) && content.is_a?(Hash)
37
- dotdir = "." + dir
38
- cfgpath = File.join(dotdir, cfgname)
39
- FileUtils.touch(cfgpath)
40
- if as_json == true
41
- content = content.to_json
42
- File.write(cfgpath, content)
43
- elsif as_yaml == true
44
- content = content.to_yaml
45
- File.write(cfgpath, content)
46
- else
47
- File.write(cfgpath, content)
48
- end
49
- end
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 configuration folder set up with setup(). If no configuration folder has been set up, creates one.
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
- def mknested(dir, nesteddir)
60
- dotdir = "." + dir
61
- FileUtils.mkdir_p(File.join(dotdir, nesteddir))
62
- end
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
- def readcfg(dir, cfgname, opts={})
78
- dotdir = "." + dir
79
- as_json=opts.fetch(:json, false)
80
- as_yaml=opts.fetch(:yaml, false)
81
- if as_json == true
82
- content = JSON::load(File.read(File.join(dotdir, cfgname)))
83
- return content
84
- elsif as_yaml == true
85
- content = YAML::load(File.read(File.join(dotdir, cfgname)))
86
- return content
87
- else
88
- File.read(File.join(dotdir, cfgname))
89
- end
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
- # Remove a whole database
105
- # @param [String] dir The configuration directory/database to be deleted, without the '.'
106
- # @return [String] Path to deleted database
107
- def rmdb(dir)
108
- dotdir = "." + dir
109
- FileUtils.rm_rf(dotdir)
110
- end
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
- ## Remove a nested directory
113
- # @param dir Root configuration directory
114
- # @param [String] Nested directory path, please use File.join() if it's more than one layer deep.
115
- # @return [String] Path to deleted nested directory
116
- def rmnested(dir, nested)
117
- dotdir = File.join("." + dir, nested)
118
- FileUtils.rm_rf(dotdir)
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
- # Create/append to a config file. Can
123
- # serialize a hash as yaml or json, or write a string
124
- # @param [String] dir Path to store config file in
125
- # @param [String] cfgname Name of config file to write to
126
- # @param [Hash] opts Optional param hash
127
- # @option opts [boolean] :json If true, serialize content
128
- # as JSON (ignored if content isn't a hash) Default #
129
- # false
130
- # @option opts [boolean] :yaml If true, serialize content
131
- # as YAML (ignored if content isn't a hash) Default #
132
- # false (json: true takes precedence)
133
- # @return path to configuration file created.
134
- def writecfg(dir, cfgname, content, opts = {})
135
- as_json = opts.fetch(:json, false) && content.is_a?(Hash)
136
- as_yaml = opts.fetch(:yaml, false) && content.is_a?(Hash)
137
- dotdir = "." + dir
138
- cfgpath = File.join(dotdir, cfgname)
139
- FileUtils.touch(cfgpath)
140
- if as_json == true
141
- content = content.to_json
142
- content = "\n" + content
143
- IO.write(cfgpath, "\n" + content, mode: 'a')
144
- elsif as_yaml == true
145
- content = content.to_yaml
146
- IO.write(cfgpath, "\n" + content, mode: 'a')
147
- else
148
- IO.write(cfgpath, "\n" + content, mode: 'a')
149
- end
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'
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. 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: []