Class: Anyway::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/anyway/config.rb,
lib/anyway/rails/config.rb

Overview

:nodoc:

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name: nil, load: true, overrides: {}) ⇒ Config

Instantiate config with specified name, loads the data and applies overrides

Example:

my_config = Anyway::Config.new(name: :my_app, load: true, overrides: { some: :value })

rubocop:disable Metrics/MethodLength,Metrics/AbcSize,Metrics/LineLength,Metrics/CyclomaticComplexity

Raises:

  • (ArgumentError)


76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/anyway/config.rb', line 76

def initialize(name: nil, load: true, overrides: {})
  @config_name = name || self.class.config_name

  raise ArgumentError, "Config name is missing" unless @config_name

  if @config_name.to_s.include?('_') && self.class.env_prefix.nil?
    warn "[Deprecated] As your config_name is #{@config_name}, " \
         "the prefix `#{@config_name.to_s.delete('_').upcase}` " \
         "will be used to parse env variables. " \
         "This behavior is about to change in 1.4.0 (no more deleting underscores). " \
         "Env prefix can be set explicitly with `env_prefix` method now already " \
         "(check out the docs), and it will be used as is."
  end

  @env_prefix = self.class.env_prefix || @config_name.to_s&.delete('_')

  self.load(overrides) if load
end

Class Attribute Details

.config_attributesObject (readonly)

Returns the value of attribute config_attributes.



17
18
19
# File 'lib/anyway/config.rb', line 17

def config_attributes
  @config_attributes
end

.defaultsObject (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_nameObject (readonly)

Returns the value of attribute config_name.



67
68
69
# File 'lib/anyway/config.rb', line 67

def config_name
  @config_name
end

#env_prefixObject (readonly)

Returns the value of attribute env_prefix.



67
68
69
# File 'lib/anyway/config.rb', line 67

def env_prefix
  @env_prefix
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
37
# 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

.env_prefix(val = nil) ⇒ Object



39
40
41
42
43
# File 'lib/anyway/config.rb', line 39

def env_prefix(val = nil)
  return (@env_prefix = val.to_s) unless val.nil?

  @env_prefix
end

.for(name) ⇒ Object

Load config as Hash by any name

Example:

my_config = Anyway::Config.for(:my_app)
# will load data from config/my_app.yml, secrets.my_app, ENV["MY_APP_*"]


51
52
53
# File 'lib/anyway/config.rb', line 51

def for(name)
  new(name: name, load: false).load_from_sources
end

Instance Method Details

#clearObject



102
103
104
105
106
107
# File 'lib/anyway/config.rb', line 102

def clear
  self.class.config_attributes.each do |attr|
    send("#{attr}=", nil)
  end
  self
end

#load(overrides = {}) ⇒ Object



109
110
111
112
113
114
115
116
# File 'lib/anyway/config.rb', line 109

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



133
134
135
136
# File 'lib/anyway/config.rb', line 133

def load_from_env(config)
  config.deep_merge!(Anyway.env.fetch(env_prefix))
  config
end

#load_from_file(config) ⇒ Object



126
127
128
129
130
131
# File 'lib/anyway/config.rb', line 126

def load_from_file(config)
  config_path = Anyway.env.fetch(env_prefix).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



26
27
28
29
30
31
# File 'lib/anyway/rails/config.rb', line 26

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



118
119
120
121
122
123
124
# File 'lib/anyway/config.rb', line 118

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/MethodLength,Metrics/AbcSize,Metrics/LineLength,Metrics/CyclomaticComplexity



96
97
98
99
100
# File 'lib/anyway/config.rb', line 96

def reload(overrides = {})
  clear
  load(overrides)
  self
end

#to_hObject



138
139
140
141
142
# File 'lib/anyway/config.rb', line 138

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