confrb 1.3

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 +7 -0
  2. data/lib/confrb.rb +120 -0
  3. metadata +45 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 59dcfb3a452a7606bb6e576974c1737e5045937ccadacdc5c118877834f30952
4
+ data.tar.gz: 32c66d6bf4fa2cddc422c7b8ed0c5a3b8de46fdd97ae6acee8183a528bce6515
5
+ SHA512:
6
+ metadata.gz: 3440bb1335052a4324c6444ae17c248880f30cda32846a10dbb0d87724e7676f86ef9b3cdafaf4f9ed88d2dc55c4f620abb9d2c498363926bd33c05884bfbb1e
7
+ data.tar.gz: 951e5ac8ff096b37bed1aa9f49116f1d4ea77bcc019845106a1c3494212a8c97af1c7bbe4ab7219b13e7e9f43188d0b17a2bb35292be9ece89862c07443eeb6a
@@ -0,0 +1,120 @@
1
+ # Require everything!
2
+ require 'fileutils'
3
+ require 'json'
4
+ require 'yaml'
5
+ ##
6
+ # Main module for BasicCfrb
7
+ # @author gsbhasin84
8
+ module BasicCfrb
9
+ extend self # Make sure this extends self so it can be used as a module!
10
+
11
+ ##
12
+ # Create config storage directory, appending '.' to
13
+ # given name. Can create subdirs recursively, but only
14
+ # 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
20
+
21
+ ##
22
+ # Create/overwrite a config file and write data to it. Can
23
+ # serialize a hash as yaml or json, or write a string
24
+ # @param [String] dir Path to store config file in
25
+ # @param [String] cfgname Name of config file to write to
26
+ # @param [Hash] opts Optional param hash
27
+ # @option opts [boolean] :json If true, serialize content
28
+ # as JSON (ignored if content isn't a hash) Default #
29
+ # false
30
+ # @option opts [boolean] :yaml If true, serialize content
31
+ # as YAML (ignored if content isn't a hash) Default #
32
+ # false (json: true takes precedence
33
+ # @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
50
+
51
+ ##
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.
55
+ # @param [String] nesteddir Nested dir(s) to create in dir
56
+ # Make sure path delimiter is OS correct, or use
57
+ # File.join
58
+ # @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
63
+
64
+ ##
65
+ # Read a config file from disk
66
+ # @param [String] dir Setup dir name (without '.' prefix)
67
+ # @param [String] cfgname Config file name to read
68
+ # @param [Hash] opts Optional param hash
69
+ # @option opts [boolean] :json If true, deserialize
70
+ # content as JSON (Default false)
71
+ # @option opts [boolean] :yaml If true, deserialize
72
+ # content as YAML. Default false (json: true takes
73
+ # precedence
74
+ # @return [String|Hash] Loaded contents, either a String
75
+ # if not indicated to treat as JSON or YAML, or a hash
76
+ # 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
102
+
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
111
+
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
120
+ end
metadata ADDED
@@ -0,0 +1,45 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: confrb
3
+ version: !ruby/object:Gem::Version
4
+ version: '1.3'
5
+ platform: ruby
6
+ authors:
7
+ - Gurjus Bhasin
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2019-12-18 00:00:00.000000000 Z
12
+ dependencies: []
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
16
+ email: gsbhasin84@gmail.com
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - lib/confrb.rb
22
+ homepage: https://gitlab.com/Gsbhasin84/confrb
23
+ licenses:
24
+ - MIT
25
+ metadata: {}
26
+ post_install_message:
27
+ rdoc_options: []
28
+ require_paths:
29
+ - lib
30
+ required_ruby_version: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - ">="
33
+ - !ruby/object:Gem::Version
34
+ version: '0'
35
+ required_rubygems_version: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ version: '0'
40
+ requirements: []
41
+ rubygems_version: 3.0.6
42
+ signing_key:
43
+ specification_version: 4
44
+ summary: Plaintext configuration library for Ruby
45
+ test_files: []