Class: Anyway::Config
- Inherits:
-
Object
- Object
- Anyway::Config
- Defined in:
- lib/anyway/config.rb,
lib/anyway/rails/config.rb
Class Attribute Summary collapse
-
.config_attributes ⇒ Object
readonly
Returns the value of attribute config_attributes.
-
.defaults ⇒ Object
readonly
Returns the value of attribute defaults.
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 = true) ⇒ Config
constructor
A new instance of Config.
- #load ⇒ Object
- #load_from_env(config) ⇒ Object
- #load_from_file(config) ⇒ Object
- #load_from_secrets(config) ⇒ Object
- #load_from_sources(config = {}.with_indifferent_access) ⇒ Object
- #reload ⇒ Object
Constructor Details
#initialize(config_name = nil, do_load = true) ⇒ Config
Returns a new instance of Config.
33 34 35 36 |
# File 'lib/anyway/config.rb', line 33 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_attributes ⇒ Object (readonly)
Returns the value of attribute config_attributes.
4 5 6 |
# File 'lib/anyway/config.rb', line 4 def config_attributes @config_attributes end |
.defaults ⇒ Object (readonly)
Returns the value of attribute defaults.
4 5 6 |
# File 'lib/anyway/config.rb', line 4 def defaults @defaults end |
Class Method Details
.attr_config(*args, hargs) ⇒ Object
6 7 8 9 10 |
# File 'lib/anyway/config.rb', line 6 def attr_config(*args,hargs) @defaults = hargs.dup.with_indifferent_access @config_attributes = args+hargs.keys attr_accessor *@config_attributes end |
.config_name(val = nil) ⇒ Object
12 13 14 15 |
# File 'lib/anyway/config.rb', line 12 def config_name(val = nil) return (@config_name = val.to_s) unless val.nil? @config_name ||= extract_name end |
Instance Method Details
#clear ⇒ Object
44 45 46 47 48 49 |
# File 'lib/anyway/config.rb', line 44 def clear self.class.config_attributes.each do |attr| send("#{attr}=", nil) end self end |
#load ⇒ Object
51 52 53 54 55 56 |
# File 'lib/anyway/config.rb', line 51 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
72 73 74 75 |
# File 'lib/anyway/config.rb', line 72 def load_from_env(config) config.deep_merge! (Anyway.env.send(@config_name) || {}) config end |
#load_from_file(config) ⇒ Object
63 64 65 66 67 68 69 70 |
# File 'lib/anyway/config.rb', line 63 def load_from_file(config) config_path = (Anyway.env.send(@config_name)||{}).delete(:conf) if config_path and File.file?(config_path) require 'yaml' config.deep_merge! (YAML.load_file(config_path) || {}) end config end |
#load_from_secrets(config) ⇒ Object
18 19 20 21 22 23 |
# File 'lib/anyway/rails/config.rb', line 18 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 = {}.with_indifferent_access) ⇒ Object
58 59 60 61 |
# File 'lib/anyway/config.rb', line 58 def load_from_sources(config={}.with_indifferent_access) load_from_file(config) load_from_env(config) end |
#reload ⇒ Object
38 39 40 41 42 |
# File 'lib/anyway/config.rb', line 38 def reload clear load self end |