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)


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

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.



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

def config_attributes
  @config_attributes
end

.defaultsObject (readonly)

Returns the value of attribute defaults.



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

def defaults
  @defaults
end

.option_parser_extensionObject (readonly)

Returns the value of attribute option_parser_extension.



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

def option_parser_extension
  @option_parser_extension
end

Instance Attribute Details

#config_nameObject (readonly)

Returns the value of attribute config_name.



110
111
112
# File 'lib/anyway/config.rb', line 110

def config_name
  @config_name
end

#env_prefixObject (readonly)

Returns the value of attribute env_prefix.



110
111
112
# File 'lib/anyway/config.rb', line 110

def env_prefix
  @env_prefix
end

Class Method Details

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



27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/anyway/config.rb', line 27

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



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

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



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

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

.env_prefix(val = nil) ⇒ Object



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

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

  @env_prefix
end

.extend_options(&block) ⇒ Object



65
66
67
# File 'lib/anyway/config.rb', line 65

def extend_options(&block)
  @option_parser_extension = block
end

.flag_options(*args) ⇒ Object



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

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


90
91
92
# File 'lib/anyway/config.rb', line 90

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

.ignore_options(*args) ⇒ Object



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

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

.option_parser_optionsObject



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

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



134
135
136
137
138
139
# File 'lib/anyway/config.rb', line 134

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

#load(overrides = {}) ⇒ Object



141
142
143
144
145
146
147
148
# File 'lib/anyway/config.rb', line 141

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



165
166
167
168
# File 'lib/anyway/config.rb', line 165

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

#load_from_file(config) ⇒ Object



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

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



150
151
152
153
154
155
156
# File 'lib/anyway/config.rb', line 150

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

  load_from_file(config)
  load_from_env(config)
end

#option_parserObject



170
171
172
173
174
175
176
177
# File 'lib/anyway/config.rb', line 170

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



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

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

#reload(overrides = {}) ⇒ Object



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

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

#to_hObject



183
184
185
186
187
# File 'lib/anyway/config.rb', line 183

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