Module: ConfigatronPlus
- Defined in:
- lib/configatron_plus.rb,
lib/configatron_plus/errors.rb
Defined Under Namespace
Classes: ConfigError
Constant Summary collapse
- DEFAULT_SOURCES =
[:current, :env]
Class Method Summary collapse
-
.can_use_as_filename?(val) ⇒ Boolean
Checks to see if we can use this as a filename.
- .config ⇒ Object
-
.errors ⇒ Array
Of symbols.
- .fail_when_missing ⇒ Object
- .fail_when_missing=(val) ⇒ Object
- .fetch_sources ⇒ Object
-
.get_from_env ⇒ Object
scan ENV for any keys that start with alfred_ check to see that they are valid names add them to configatron.
-
.get_from_file(file) ⇒ Object
checks to see if file exists and loads it.
-
.get_from_special(s) ⇒ Object
looks upo the file for a given symbol and calls get_from_file with it.
- .is_symbol_defined?(sym) ⇒ Boolean
-
.is_valid_key_name?(e) ⇒ Boolean
Checks a string to make sure it is a valid identifier so we can eval it.
-
.reset_sources ⇒ Object
set @@sources = DEFAULT_SOURCES.
-
.sources ⇒ Array<String,Symbol>
Get the current list of sources.
-
.sources=(new_val) ⇒ Object
Set the sources to use for configatron.
- .use_this_env?(env) ⇒ Boolean
-
.valid_list?(val) ⇒ Boolean
check each element of the list to see if it’s a correct symbol or can be used a a filename.
Class Method Details
.can_use_as_filename?(val) ⇒ Boolean
Checks to see if we can use this as a filename
88 89 90 91 92 93 94 95 |
# File 'lib/configatron_plus.rb', line 88 def self.can_use_as_filename?(val) return true if val.is_a?(String) return true if val.is_a?(Pathname) return true if val.is_a?(Symbol) #if no other false end |
.config ⇒ Object
8 9 10 |
# File 'lib/configatron_plus.rb', line 8 def self.config configatron.configatron_plus end |
.errors ⇒ Array
Returns of symbols.
47 48 49 |
# File 'lib/configatron_plus.rb', line 47 def self.errors @@errors ||= [] end |
.fail_when_missing ⇒ Object
20 21 22 |
# File 'lib/configatron_plus.rb', line 20 def self.fail_when_missing configatron.configatron_plus.fail_when_missing end |
.fail_when_missing=(val) ⇒ Object
24 25 26 |
# File 'lib/configatron_plus.rb', line 24 def self.fail_when_missing=(val) configatron.configatron_plus.fail_when_missing = val end |
.fetch_sources ⇒ Object
75 76 77 78 79 80 81 82 83 |
# File 'lib/configatron_plus.rb', line 75 def self.fetch_sources self.sources.each do |s| if s.is_a?(Symbol) get_from_special(s) else get_from_file(s.is_a?(Pathname) ? s : Pathname(s)) end end end |
.get_from_env ⇒ Object
scan ENV for any keys that start with alfred_ check to see that they are valid names add them to configatron
123 124 125 126 127 128 129 130 131 |
# File 'lib/configatron_plus.rb', line 123 def self.get_from_env ENV.keys.find_all {|e| self.use_this_env?(e)}.each do |key| setting = key[config.env_prefix.length .. -1] setting.split('_').each do |e| raise ConfigatronPlus::ConfigError.new("Env variable '#{key}' contains invalid characters") unless self.is_valid_key_name?(e) end eval("configatron." + setting.gsub('_','.') + " = \"#{ENV[key]}\"") end end |
.get_from_file(file) ⇒ Object
checks to see if file exists and loads it
110 111 112 113 114 115 116 117 118 |
# File 'lib/configatron_plus.rb', line 110 def self.get_from_file(file) raise ArgumentError.new("Not Pathname: #{file}") unless file.is_a?(Pathname) if file..file? c = File.read(file.) eval(c) elsif self.fail_when_missing raise ConfigatronPlus::ConfigError.new("Config file does not exist: #{file.to_s}") end end |
.get_from_special(s) ⇒ Object
looks upo the file for a given symbol and calls get_from_file with it
99 100 101 102 103 104 105 106 |
# File 'lib/configatron_plus.rb', line 99 def self.get_from_special(s) raise ArgumentError unless s.is_a?(Symbol) return get_from_env if s == :env file = config.source_files.fetch(s,nil) raise ConfigatronPlus::ConfigError.new("Setting does not exist for: #{s}") if file.nil? file = Pathname(file) self.get_from_file(file) end |
.is_symbol_defined?(sym) ⇒ Boolean
68 69 70 71 |
# File 'lib/configatron_plus.rb', line 68 def self.is_symbol_defined?(sym) return true if sym == :env self.config.source_files.keys.include?(sym) end |
.is_valid_key_name?(e) ⇒ Boolean
Checks a string to make sure it is a valid identifier so we can eval it
140 141 142 |
# File 'lib/configatron_plus.rb', line 140 def self.is_valid_key_name?(e) /^[a-z][a-z0-9]*$/.match(e.downcase) end |
.reset_sources ⇒ Object
set @@sources = DEFAULT_SOURCES
52 53 54 |
# File 'lib/configatron_plus.rb', line 52 def self.reset_sources self.sources = DEFAULT_SOURCES.dup end |
.sources ⇒ Array<String,Symbol>
Get the current list of sources
41 42 43 |
# File 'lib/configatron_plus.rb', line 41 def self.sources self.config.sources end |
.sources=(new_val) ⇒ Object
Set the sources to use for configatron
30 31 32 33 34 35 36 37 |
# File 'lib/configatron_plus.rb', line 30 def self.sources=(new_val) @@errors = nil raise ArgumentError.new "value must be an Array." unless new_val.is_a?(Array) unless self.valid_list?(new_val) raise ArgumentError.new "Array members must be a symbol, string, or Pathname: #{self.errors}" end self.config.sources = new_val end |
.use_this_env?(env) ⇒ Boolean
133 134 135 |
# File 'lib/configatron_plus.rb', line 133 def self.use_this_env?(env) env.length > config.env_prefix.length && env.start_with?(config.env_prefix) end |
.valid_list?(val) ⇒ Boolean
check each element of the list to see if it’s a correct symbol or can be used a a filename.
58 59 60 61 62 63 64 65 66 |
# File 'lib/configatron_plus.rb', line 58 def self.valid_list?(val) val.each do |i| errors << "Invalid entry: #{i}" unless self.can_use_as_filename?(i) if i.is_a?(Symbol) && ! self.is_symbol_defined?(i) errors << "Invalid symbol: #{i.to_s}" end end errors.empty? end |