json_config 1.1.3

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/json_config.rb +143 -0
  3. metadata +64 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 87a043e86da9ac631f8c43f58760714357420a1ebb91d4a1af4e22d4869a3b0c
4
+ data.tar.gz: 62688a29d41b766a10d00fc3d726a893843e158ed1dce4ea887461f96a9b3710
5
+ SHA512:
6
+ metadata.gz: 736cd91e71b8ced02174885349dda7917a85f1ccea7d1ef0667892fb32a1724f1e409bbe276d0dff3603930988b13b6e00baff24dfe689011832809f02d4edc8
7
+ data.tar.gz: b4c8708b2558ec6b1b20953e3fda03b443c6f9df1bf64f8f7bb773fab3004e6589b36de7dd7a8f7ce6feea471d079a67227b3975116b0eb5819b8e98286592a6
@@ -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: json_config
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.1.3
5
+ platform: ruby
6
+ authors:
7
+ - Miles Whittaker
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2019-12-06 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/json_config.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: []