jsoncfg 1.1.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +7 -0
  2. data/lib/jsoncfg.rb +143 -0
  3. metadata +64 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 62417306da4c75b321783b383d516f868c73c26a9035fba139169fdd6a49004a
4
+ data.tar.gz: 48476e31d69d8731c5dd6b20ef2d2b3130e086f6f5a6f31d890dcf9134b35d6f
5
+ SHA512:
6
+ metadata.gz: b574084bea3c28b09e45aeb11d18a26edc8cfcfbe75732c432b7abc47dd1b16532517259754da61fba2d4f033a13623a0a2fe8a50371d82cdf94770afc61dd93
7
+ data.tar.gz: 12774c0ee0c4c9cb991996b19d2a1a8f8d910b01998fd305cfb0b81ed90d497e10104c98399a29cdc579fa876117421a9ed9977871e39aff7ececf19650127f8
@@ -0,0 +1,143 @@
1
+ require "fileutils"
2
+ require "json"
3
+ require "pathname"
4
+
5
+ class JSONConfig
6
+ module Keys
7
+ def add_bool_key(key)
8
+ define_method "#{key}?" do
9
+ return get(key)
10
+ end
11
+ define_method key do
12
+ set(key, true)
13
+ end
14
+ define_method "no_#{key}" do
15
+ set(key, false)
16
+ end
17
+ end
18
+
19
+ def add_key(key)
20
+ define_method "#{key}?" do
21
+ return !@config[key].nil?
22
+ end
23
+ define_method "get_#{key}" do
24
+ return get(key)
25
+ end
26
+ define_method "set_#{key}" do |val|
27
+ set(key, val)
28
+ end
29
+ end
30
+ end
31
+
32
+ def clear
33
+ @config = Hash.new
34
+ @diff = Hash.new
35
+ write_config
36
+ end
37
+
38
+ def default
39
+ @config = @defaults.clone
40
+ @diff = @defaults.clone
41
+ write_config
42
+ end
43
+
44
+ def get(key)
45
+ case @config[key]
46
+ when /^\s*false\s*$/i, false
47
+ return false
48
+ when /^\s*true\s*$/i, true
49
+ return true
50
+ when /^\s*\d+\s*$/
51
+ return @config[key].to_i
52
+ when /^\s*\d+\.\d+\s*$/
53
+ return @config[key].to_f
54
+ else
55
+ return @config[key]
56
+ end
57
+ end
58
+
59
+ def getdiff(key)
60
+ case @diff[key]
61
+ when /^\s*false\s*$/i, false
62
+ return false
63
+ when /^\s*true\s*$/i, true
64
+ return true
65
+ else
66
+ return @diff[key]
67
+ end
68
+ end
69
+
70
+ def initialize(file, autosave = true)
71
+ @defaults ||= Hash.new
72
+ @autosave = autosave
73
+ @config_file = Pathname.new(file).expand_path
74
+ read_config
75
+ end
76
+
77
+ def read_config
78
+ if (!@config_file.exist? && !@config_file.symlink?)
79
+ @config = @defaults.clone
80
+ write_config(true)
81
+ end
82
+ @config = JSON.parse(File.read(@config_file))
83
+ @diff = @defaults.clone
84
+ end
85
+ private :read_config
86
+
87
+ def reset
88
+ read_config
89
+ end
90
+
91
+ def save
92
+ @diff = @defaults.clone
93
+ write_config(true)
94
+ end
95
+
96
+ def savediff
97
+ @config = @diff
98
+ write_config(true)
99
+ end
100
+
101
+ def set(key, value)
102
+ case value
103
+ when /^\s*false\s*$/i, false
104
+ unsetbool(key)
105
+ when /^\s*true\s*$/i, true
106
+ setbool(key)
107
+ when /^\s*\d+\s*$/
108
+ @config[key] = value.to_i
109
+ @diff[key] = value.to_i
110
+ write_config
111
+ when /^\s*\d+\.\d+\s*$/
112
+ @config[key] = value.to_f
113
+ @diff[key] = value.to_f
114
+ write_config
115
+ else
116
+ @config[key] = value
117
+ @diff[key] = value
118
+ write_config
119
+ end
120
+ end
121
+
122
+ def setbool(key)
123
+ @config[key] = true
124
+ @diff[key] = true
125
+ write_config
126
+ end
127
+
128
+ def unsetbool(key)
129
+ @config[key] = false
130
+ @diff[key] = false
131
+ write_config
132
+ end
133
+
134
+ def write_config(force = false)
135
+ return if (!@autosave && !force)
136
+
137
+ FileUtils.mkdir_p(@config_file.dirname)
138
+ File.open(@config_file, "w") do |file|
139
+ file.write(JSON.pretty_generate(@config))
140
+ end
141
+ end
142
+ private :write_config
143
+ end
metadata ADDED
@@ -0,0 +1,64 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: jsoncfg
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.1.4
5
+ platform: ruby
6
+ authors:
7
+ - Miles Whittaker
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2019-12-07 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rake
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: 13.0.0
20
+ - - "~>"
21
+ - !ruby/object:Gem::Version
22
+ version: '13.0'
23
+ type: :development
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ version: 13.0.0
30
+ - - "~>"
31
+ - !ruby/object:Gem::Version
32
+ version: '13.0'
33
+ description: Read/write from/to a JSON config file.
34
+ email: mj@whitta.dev
35
+ executables: []
36
+ extensions: []
37
+ extra_rdoc_files: []
38
+ files:
39
+ - lib/jsoncfg.rb
40
+ homepage: https://gitlab.com/mjwhitta/jsoncfg/tree/ruby
41
+ licenses:
42
+ - GPL-3.0
43
+ metadata:
44
+ source_code_uri: https://gitlab.com/mjwhitta/jsoncfg/tree/ruby
45
+ post_install_message:
46
+ rdoc_options: []
47
+ require_paths:
48
+ - lib
49
+ required_ruby_version: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ required_rubygems_version: !ruby/object:Gem::Requirement
55
+ requirements:
56
+ - - ">="
57
+ - !ruby/object:Gem::Version
58
+ version: '0'
59
+ requirements: []
60
+ rubygems_version: 3.0.6
61
+ signing_key:
62
+ specification_version: 4
63
+ summary: Read/write from/to a JSON config file
64
+ test_files: []