Class: Anyway::Config
- Inherits:
-
Object
- Object
- Anyway::Config
- Defined in:
- lib/anyway/config.rb,
lib/anyway/rails/config.rb
Overview
:nodoc:
Class Attribute Summary collapse
-
.config_attributes ⇒ Object
readonly
Returns the value of attribute config_attributes.
-
.defaults ⇒ Object
readonly
Returns the value of attribute defaults.
Instance Attribute Summary collapse
-
#config_name ⇒ Object
readonly
Returns the value of attribute config_name.
Class Method Summary collapse
- .attr_config(*args, **hargs) ⇒ Object
- .config_name(val = nil) ⇒ Object
-
.for(name) ⇒ Object
Load config as Hash by any name.
Instance Method Summary collapse
- #clear ⇒ Object
-
#initialize(config_name = nil, do_load = nil, name: nil, load: true, overrides: {}) ⇒ Config
constructor
Instantiate config with specified name, loads the data and applies overrides.
- #load(overrides = {}) ⇒ Object
- #load_from_env(config) ⇒ Object
- #load_from_file(config) ⇒ Object
- #load_from_secrets(config) ⇒ Object
- #load_from_sources(config = {}) ⇒ Object
-
#reload(overrides = {}) ⇒ Object
rubocop:enable Metrics/LineLength,Metrics/CyclomaticComplexity,Metrics/PerceivedComplexity.
- #to_h ⇒ Object
Constructor Details
#initialize(config_name = nil, do_load = nil, name: nil, load: true, overrides: {}) ⇒ Config
68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/anyway/config.rb', line 68 def initialize(config_name = nil, do_load = nil, name: nil, load: true, overrides: {}) unless config_name.nil? && do_load.nil? warn "[Deprecated] Positional arguments for Anyway::Config#initialize will be removed in 1.2.0. Use keyword arguments instead: initialize(name:, load:, overrides:)" end name = config_name unless config_name.nil? load = do_load unless do_load.nil? @config_name = name || self.class.config_name raise ArgumentError, "Config name is missing" unless @config_name self.load(overrides) if load end |
Class Attribute Details
.config_attributes ⇒ Object (readonly)
Returns the value of attribute config_attributes.
17 18 19 |
# File 'lib/anyway/config.rb', line 17 def config_attributes @config_attributes end |
.defaults ⇒ Object (readonly)
Returns the value of attribute defaults.
17 18 19 |
# File 'lib/anyway/config.rb', line 17 def defaults @defaults end |
Instance Attribute Details
#config_name ⇒ Object (readonly)
Returns the value of attribute config_name.
59 60 61 |
# File 'lib/anyway/config.rb', line 59 def config_name @config_name end |
Class Method Details
.attr_config(*args, **hargs) ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/anyway/config.rb', line 19 def attr_config(*args, **hargs) @defaults ||= {} @config_attributes ||= [] new_defaults = hargs.deep_dup new_defaults.stringify_keys! defaults.merge! new_defaults new_keys = (args + new_defaults.keys) - config_attributes @config_attributes += new_keys attr_accessor(*new_keys) end |
.config_name(val = nil) ⇒ Object
32 33 34 35 36 |
# File 'lib/anyway/config.rb', line 32 def config_name(val = nil) return (@config_name = val.to_s) unless val.nil? @config_name = underscore_name unless defined?(@config_name) @config_name end |
Instance Method Details
#clear ⇒ Object
87 88 89 90 91 92 |
# File 'lib/anyway/config.rb', line 87 def clear self.class.config_attributes.each do |attr| send("#{attr}=", nil) end self end |
#load(overrides = {}) ⇒ Object
94 95 96 97 98 99 100 101 |
# File 'lib/anyway/config.rb', line 94 def load(overrides = {}) config = load_from_sources((self.class.defaults || {}).deep_dup) config.merge!(overrides) unless overrides.nil? config.each do |key, val| set_value(key, val) end end |
#load_from_env(config) ⇒ Object
117 118 119 120 |
# File 'lib/anyway/config.rb', line 117 def load_from_env(config) config.deep_merge!(Anyway.env.fetch(config_name)) config end |
#load_from_file(config) ⇒ Object
110 111 112 113 114 115 |
# File 'lib/anyway/config.rb', line 110 def load_from_file(config) config_path = Anyway.env.fetch(config_name).delete('conf') || "./config/#{config_name}.yml" config.deep_merge!(parse_yml(config_path) || {}) if config_path && File.file?(config_path) config end |
#load_from_secrets(config) ⇒ Object
25 26 27 28 29 30 |
# File 'lib/anyway/rails/config.rb', line 25 def load_from_secrets(config) if Rails.application.respond_to?(:secrets) config.deep_merge!(Rails.application.secrets.send(@config_name) || {}) end config end |
#load_from_sources(config = {}) ⇒ Object
103 104 105 106 107 108 |
# File 'lib/anyway/config.rb', line 103 def load_from_sources(config = {}) # Handle anonymous configs return config unless config_name load_from_file(config) load_from_env(config) end |
#reload(overrides = {}) ⇒ Object
rubocop:enable Metrics/LineLength,Metrics/CyclomaticComplexity,Metrics/PerceivedComplexity
81 82 83 84 85 |
# File 'lib/anyway/config.rb', line 81 def reload(overrides = {}) clear load(overrides) self end |
#to_h ⇒ Object
122 123 124 125 126 |
# File 'lib/anyway/config.rb', line 122 def to_h self.class.config_attributes.each_with_object({}) do |key, obj| obj[key.to_sym] = send(key) end.deep_dup.deep_freeze end |