Class: EasyJSON::Config
- Inherits:
-
Object
- Object
- EasyJSON::Config
- Defined in:
- lib/easy_json_config/config.rb
Instance Attribute Summary collapse
-
#path ⇒ Object
readonly
Returns the value of attribute path.
Class Method Summary collapse
-
.new(path: nil, defaults: nil, frozen_values: nil, required_keys: nil) ⇒ Object
override the new method to return existing class if it exists.
Instance Method Summary collapse
- #[](key) ⇒ Object
-
#add_defaults(new_defaults) ⇒ Object
Add a hash of default keys and values to be merged over the current defaults (if any).
- #add_required_keys(new_required_keys) ⇒ Object
- #add_sensitive_keys(new_sensitive_keys) ⇒ Object
- #clear_sensitive_keys ⇒ Object
-
#freeze_values(new_frozen_values) ⇒ Object
Add a hash of hard-coded keys and values to be merged over the current hard-coded values (if any).
- #save ⇒ Object
- #to_s ⇒ Object
-
#values ⇒ Object
returns a hash containing the json config file values merged over the default values.
- #values_without_sensitive_or_hardcoded_keys ⇒ Object
Instance Attribute Details
#path ⇒ Object (readonly)
Returns the value of attribute path.
13 14 15 |
# File 'lib/easy_json_config/config.rb', line 13 def path @path end |
Class Method Details
.new(path: nil, defaults: nil, frozen_values: nil, required_keys: nil) ⇒ Object
override the new method to return existing class if it exists
67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/easy_json_config/config.rb', line 67 def self.new(path: nil, defaults: nil, frozen_values: nil, required_keys: nil) path = ::File.(path || 'config.json') instance = EasyJSON.configs[path] # return existing instance if one already exists for the path provided unless instance.nil? # add defaults and hard coded values provided to existing instance instance.add_defaults(defaults) instance.freeze_values(frozen_values) instance.add_required_keys(required_keys) return instance end EasyJSON.configs[path] = super(path, defaults, frozen_values, required_keys) # create new instance if none existed end |
Instance Method Details
#[](key) ⇒ Object
15 16 17 |
# File 'lib/easy_json_config/config.rb', line 15 def [](key) values[key] end |
#add_defaults(new_defaults) ⇒ Object
Add a hash of default keys and values to be merged over the current defaults (if any). The json config can override these values.
25 26 27 |
# File 'lib/easy_json_config/config.rb', line 25 def add_defaults(new_defaults) @defaults = Hashly.deep_merge(@defaults, new_defaults) end |
#add_required_keys(new_required_keys) ⇒ Object
53 54 55 |
# File 'lib/easy_json_config/config.rb', line 53 def add_required_keys(new_required_keys) @required_keys = Hashly.deep_merge(@required_keys, new_required_keys) end |
#add_sensitive_keys(new_sensitive_keys) ⇒ Object
57 58 59 60 |
# File 'lib/easy_json_config/config.rb', line 57 def add_sensitive_keys(new_sensitive_keys) new_sensitive_keys = [new_sensitive_keys] if new_sensitive_keys.is_a?(String) || new_sensitive_keys.is_a?(Symbol) @sensitive_keys = (@sensitive_keys + new_sensitive_keys).uniq end |
#clear_sensitive_keys ⇒ Object
62 63 64 |
# File 'lib/easy_json_config/config.rb', line 62 def clear_sensitive_keys @sensitive_keys = [] end |
#freeze_values(new_frozen_values) ⇒ Object
Add a hash of hard-coded keys and values to be merged over the current hard-coded values (if any). The json config can NOT override these values.
31 32 33 |
# File 'lib/easy_json_config/config.rb', line 31 def freeze_values(new_frozen_values) @frozen_values = Hashly.deep_merge(@frozen_values, new_frozen_values) end |
#save ⇒ Object
49 50 51 |
# File 'lib/easy_json_config/config.rb', line 49 def save ::File.write(path, values_without_sensitive_or_hardcoded_keys.to_json) end |
#to_s ⇒ Object
19 20 21 |
# File 'lib/easy_json_config/config.rb', line 19 def to_s "Config path: #{path}\nValues: #{JSON.pretty_generate values}" end |
#values ⇒ Object
returns a hash containing the json config file values merged over the default values.
36 37 38 39 40 41 42 |
# File 'lib/easy_json_config/config.rb', line 36 def values config = Hashly.deep_merge(@defaults, @json_config) config = Hashly.deep_merge(config, @frozen_values) missing_required_keys = Hashly.deep_diff_by_key(config, @required_keys) raise "The following keys are missing from #{path}: #{missing_required_keys}" unless missing_required_keys.empty? config end |
#values_without_sensitive_or_hardcoded_keys ⇒ Object
44 45 46 47 |
# File 'lib/easy_json_config/config.rb', line 44 def values_without_sensitive_or_hardcoded_keys non_sensitive_values = Hashly.deep_reject(values) { |k, _v| @sensitive_keys.include?(k) } Hashly.deep_reject_by_hash(non_sensitive_values, @frozen_values) end |