easy_json_config 0.3.0 → 0.4.0

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 +5 -5
  2. data/lib/easy_json_config/config.rb +28 -15
  3. metadata +3 -4
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: ebea935c18c52549ce6006f694c66906b791e33a
4
- data.tar.gz: 2962d01c1efc4eb69cf77d51c1adf4d625a340c6
2
+ SHA256:
3
+ metadata.gz: 26da301be576b29c521829a92f93ef11ca50c89ab7586f2acae0a7253431cb42
4
+ data.tar.gz: 8f4d542dbb5172d545f8dc331170ea1d32144e4a1a9f7133424933ec8114c0c8
5
5
  SHA512:
6
- metadata.gz: 12f88a16f96acda902e2ea941be41ca91d0cf57be36a1679e094e96403c3fa60ea1ccd0ec902d4f0217ceb0d0968187d7ffc0af5a533b9898e89f4adb035b7b0
7
- data.tar.gz: 44c30a5ac6afb8518fd64ed235172746e53cc222951e76e405c4f0070fb6c4ddb08c994b463e023ce10e8f81309fd480db9ae823caf2e6c789ecd0fd3b0f2d43
6
+ metadata.gz: 69f4d45122eb100972476ff1521c73e1e31556a22b066944310a31d917fcfdfc232f01e322851718b7fe711621d120f11cc780618ba529fd4651cfbb0a104d8d
7
+ data.tar.gz: 450c60f78888d1f55139a95ac9c41378d314bb6e4f04c7f8fe08e16adc55ee041ac8f639c539c4108096896bd8ed1bb5638efc97aa4e81e641db3f385bc7d0cb
@@ -13,11 +13,20 @@ module EasyJSON
13
13
  attr_reader :path
14
14
 
15
15
  def [](key)
16
- values[key]
16
+ @config = Hashly.deep_merge(@config, @frozen_values)
17
+ @config[key]
18
+ end
19
+
20
+ def []=(key, value)
21
+ @config[key] = value
17
22
  end
18
23
 
19
24
  def to_s
20
- "Config path: #{path}\nValues: #{JSON.pretty_generate values}"
25
+ "Config path: #{path}\nContent: #{JSON.pretty_generate @config}"
26
+ end
27
+
28
+ def to_h
29
+ Hashly.deep_merge(@config, @frozen_values).to_h
21
30
  end
22
31
 
23
32
  # Add a hash of default keys and values to be merged over the current defaults (if any).
@@ -32,22 +41,13 @@ module EasyJSON
32
41
  @frozen_values = Hashly.deep_merge(@frozen_values, new_frozen_values)
33
42
  end
34
43
 
35
- # returns a hash containing the json config file values merged over the default values.
36
- def values
37
- config = Hashly.deep_merge(@defaults, @json_config)
38
- config = Hashly.deep_merge(config, @frozen_values)
39
- missing_required_keys = Hashly.deep_diff_by_key(config, @required_keys)
40
- raise "The following keys are missing from #{path}: #{missing_required_keys}" unless missing_required_keys.empty?
41
- config
42
- end
43
-
44
- def values_without_sensitive_or_hardcoded_keys
45
- non_sensitive_values = Hashly.deep_reject(values) { |k, _v| @sensitive_keys.include?(k) }
46
- Hashly.deep_reject_by_hash(non_sensitive_values, @frozen_values)
44
+ def config_without_sensitive_or_hardcoded_keys
45
+ non_sensitive_content = Hashly.deep_reject(@config) { |k, _v| @sensitive_keys.include?(k) }
46
+ Hashly.deep_reject_by_hash(non_sensitive_content, @frozen_values)
47
47
  end
48
48
 
49
49
  def save
50
- ::File.write(path, values_without_sensitive_or_hardcoded_keys.to_json)
50
+ ::File.write(path, config_without_sensitive_or_hardcoded_keys.to_json)
51
51
  end
52
52
 
53
53
  def add_required_keys(new_required_keys)
@@ -72,11 +72,23 @@ module EasyJSON
72
72
  instance.add_defaults(defaults)
73
73
  instance.freeze_values(frozen_values)
74
74
  instance.add_required_keys(required_keys)
75
+ instance.initialize_config
75
76
  return instance
76
77
  end
77
78
  EasyJSON.configs[path] = super(path, defaults, frozen_values, required_keys) # create new instance if none existed
78
79
  end
79
80
 
81
+ # returns a hash containing the json config file values merged over the default values.
82
+ def initialize_config
83
+ @config ||= {}
84
+ @config = Hashly.deep_merge(@config, @defaults)
85
+ @config = Hashly.deep_merge(@config, @json_config)
86
+ @config = Hashly.deep_merge(@config, @frozen_values)
87
+ missing_required_keys = Hashly.deep_diff_by_key(@config, @required_keys)
88
+ raise "The following keys are missing from #{path}: #{missing_required_keys}" unless missing_required_keys.empty?
89
+ @config
90
+ end
91
+
80
92
  private
81
93
 
82
94
  def initialize(path, defaults, frozen_values, required_keys)
@@ -86,6 +98,7 @@ module EasyJSON
86
98
  @required_keys = required_keys || {}
87
99
  ::File.write(path, "{\n}") unless ::File.exist?(path) # Add empty config if none exists
88
100
  @json_config = JSON.parse(::File.read(path))
101
+ initialize_config
89
102
  end
90
103
  end
91
104
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: easy_json_config
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alex Munoz
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-05-27 00:00:00.000000000 Z
11
+ date: 2022-06-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: json
@@ -67,8 +67,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
67
67
  - !ruby/object:Gem::Version
68
68
  version: '0'
69
69
  requirements: []
70
- rubyforge_project:
71
- rubygems_version: 2.5.2.3
70
+ rubygems_version: 3.0.3.1
72
71
  signing_key:
73
72
  specification_version: 4
74
73
  summary: Ruby library for ease of using a basic json config file with the ability