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
- #[]=(key, value) ⇒ 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
- #config_without_sensitive_or_hardcoded_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).
-
#initialize_config ⇒ Object
returns a hash containing the json config file values merged over the default values.
- #save ⇒ Object
- #to_h ⇒ Object
- #to_s ⇒ 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 79 |
# 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) instance.initialize_config 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 18 |
# File 'lib/easy_json_config/config.rb', line 15 def [](key) @config = Hashly.deep_merge(@config, @frozen_values) @config[key] end |
#[]=(key, value) ⇒ Object
20 21 22 |
# File 'lib/easy_json_config/config.rb', line 20 def []=(key, value) @config[key] = value 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.
34 35 36 |
# File 'lib/easy_json_config/config.rb', line 34 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 |
#config_without_sensitive_or_hardcoded_keys ⇒ Object
44 45 46 47 |
# File 'lib/easy_json_config/config.rb', line 44 def config_without_sensitive_or_hardcoded_keys non_sensitive_content = Hashly.deep_reject(@config) { |k, _v| @sensitive_keys.include?(k) } Hashly.deep_reject_by_hash(non_sensitive_content, @frozen_values) 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.
40 41 42 |
# File 'lib/easy_json_config/config.rb', line 40 def freeze_values(new_frozen_values) @frozen_values = Hashly.deep_merge(@frozen_values, new_frozen_values) end |
#initialize_config ⇒ Object
returns a hash containing the json config file values merged over the default values.
82 83 84 85 86 87 88 89 90 |
# File 'lib/easy_json_config/config.rb', line 82 def initialize_config @config ||= {} @config = Hashly.deep_merge(@config, @defaults) @config = Hashly.deep_merge(@config, @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 |
#save ⇒ Object
49 50 51 |
# File 'lib/easy_json_config/config.rb', line 49 def save ::File.write(path, config_without_sensitive_or_hardcoded_keys.to_json) end |
#to_h ⇒ Object
28 29 30 |
# File 'lib/easy_json_config/config.rb', line 28 def to_h Hashly.deep_merge(@config, @frozen_values).to_h end |
#to_s ⇒ Object
24 25 26 |
# File 'lib/easy_json_config/config.rb', line 24 def to_s "Config path: #{path}\nContent: #{JSON.pretty_generate @config}" end |