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 })

Raises:

  • (ArgumentError)


113
114
115
116
117
118
119
120
121
# File 'lib/anyway/config.rb', line 113

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

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

  @env_prefix = self.class.env_prefix || @config_name

  self.load(overrides) if load
end

Class Attribute Details

.config_attributesObject (readonly)

Returns the value of attribute config_attributes.



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

def config_attributes
  @config_attributes
end

.defaultsObject (readonly)

Returns the value of attribute defaults.



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

def defaults
  @defaults
end

.option_parser_extensionObject (readonly)

Returns the value of attribute option_parser_extension.



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

def option_parser_extension
  @option_parser_extension
end

Instance Attribute Details

#config_nameObject (readonly)

Returns the value of attribute config_name.



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

def config_name
  @config_name
end

#env_prefixObject (readonly)

Returns the value of attribute env_prefix.



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

def env_prefix
  @env_prefix
end

Class Method Details

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



22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/anyway/config.rb', line 22

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



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

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

.describe_options(**hargs) ⇒ Object



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

def describe_options(**hargs)
  hargs.each do |name, desc|
    option_parser_descriptors[name.to_s][:desc] = desc
  end
end

.env_prefix(val = nil) ⇒ Object



73
74
75
76
77
# File 'lib/anyway/config.rb', line 73

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

  @env_prefix
end

.extend_options(&block) ⇒ Object



60
61
62
# File 'lib/anyway/config.rb', line 60

def extend_options(&block)
  @option_parser_extension = block
end

.flag_options(*args) ⇒ Object



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

def flag_options(*args)
  args.each do |name|
    option_parser_descriptors[name.to_s][:flag] = true
  end
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_*"]


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

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

.ignore_options(*args) ⇒ Object



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

def ignore_options(*args)
  args.each do |name|
    option_parser_descriptors[name.to_s][:ignore] = true
  end
end

.option_parser_optionsObject



64
65
66
67
68
69
70
71
# File 'lib/anyway/config.rb', line 64

def option_parser_options
  config_attributes.each_with_object({}) do |key, result|
    descriptor = option_parser_descriptors[key.to_s]
    next if descriptor[:ignore] == true

    result[key] = descriptor
  end
end

Instance Method Details

#clearObject



129
130
131
132
133
134
# File 'lib/anyway/config.rb', line 129

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

#load(overrides = {}) ⇒ Object



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

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



160
161
162
163
# File 'lib/anyway/config.rb', line 160

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

#load_from_file(config) ⇒ Object



153
154
155
156
157
158
# File 'lib/anyway/config.rb', line 153

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



145
146
147
148
149
150
151
# File 'lib/anyway/config.rb', line 145

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

  load_from_file(config)
  load_from_env(config)
end

#option_parserObject



165
166
167
168
169
170
171
172
# File 'lib/anyway/config.rb', line 165

def option_parser
  @option_parser ||= begin
    parser = OptionParserBuilder.call(self.class.option_parser_options) do |key, arg|
      set_value(key, arg.is_a?(String) ? arg.serialize : arg)
    end
    self.class.option_parser_extension&.call(parser, self) || parser
  end
end

#parse_options!(options) ⇒ Object



174
175
176
# File 'lib/anyway/config.rb', line 174

def parse_options!(options)
  option_parser.parse!(options)
end

#reload(overrides = {}) ⇒ Object



123
124
125
126
127
# File 'lib/anyway/config.rb', line 123

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

#to_hObject



178
179
180
181
182
# File 'lib/anyway/config.rb', line 178

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