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(config_name = nil, do_load = true) ⇒ Config

Returns a new instance of Config.



43
44
45
46
# File 'lib/anyway/config.rb', line 43

def initialize(config_name = nil, do_load = true)
  @config_name = config_name || self.class.config_name
  load if do_load
end

Class Attribute Details

.config_attributesObject (readonly)

Returns the value of attribute config_attributes.



15
16
17
# File 'lib/anyway/config.rb', line 15

def config_attributes
  @config_attributes
end

.defaultsObject (readonly)

Returns the value of attribute defaults.



15
16
17
# File 'lib/anyway/config.rb', line 15

def defaults
  @defaults
end

Instance Attribute Details

#config_nameObject (readonly)

Returns the value of attribute config_name.



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

def config_name
  @config_name
end

Class Method Details

.attr_config(*args, **hargs) ⇒ Object



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

def attr_config(*args, **hargs)
  @defaults = hargs.deep_dup
  defaults.stringify_keys!
  @config_attributes = args + defaults.keys
  attr_accessor(*@config_attributes)
end

.config_name(val = nil) ⇒ Object



24
25
26
27
28
# File 'lib/anyway/config.rb', line 24

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

.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_*"]


36
37
38
# File 'lib/anyway/config.rb', line 36

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

Instance Method Details

#clearObject



54
55
56
57
58
59
# File 'lib/anyway/config.rb', line 54

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

#loadObject



61
62
63
64
65
66
# File 'lib/anyway/config.rb', line 61

def load
  config = load_from_sources((self.class.defaults || {}).deep_dup)
  config.each do |key, val|
    set_value(key, val)
  end
end

#load_from_env(config) ⇒ Object



84
85
86
87
# File 'lib/anyway/config.rb', line 84

def load_from_env(config)
  config.deep_merge!(Anyway.env.send(config_name) || {})
  config
end

#load_from_file(config) ⇒ Object



75
76
77
78
79
80
81
82
# File 'lib/anyway/config.rb', line 75

def load_from_file(config)
  config_path = (Anyway.env.send(config_name) || {}).delete('conf')
  if config_path && File.file?(config_path)
    require 'yaml'
    config.deep_merge!(YAML.load_file(config_path) || {})
  end
  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



68
69
70
71
72
73
# File 'lib/anyway/config.rb', line 68

def load_from_sources(config = {})
  # Handle anonymous configs
  return config unless config_name
  load_from_file(config)
  load_from_env(config)
end

#reloadObject



48
49
50
51
52
# File 'lib/anyway/config.rb', line 48

def reload
  clear
  load
  self
end