module ConfigatronPlus
Constants
- DEFAULT_SOURCES
Public Class Methods
Checks to see if we can use this as a filename @param val [Object] Object to check @return [Boolean]]
# 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
# File lib/configatron_plus.rb, line 8 def self.config configatron.configatron_plus end
@return [Array] of symbols.
# File lib/configatron_plus.rb, line 47 def self.errors @@errors ||= [] end
# File lib/configatron_plus.rb, line 20 def self.fail_when_missing configatron.configatron_plus.fail_when_missing end
# File lib/configatron_plus.rb, line 24 def self.fail_when_missing=(val) configatron.configatron_plus.fail_when_missing = val end
@param opts [Hash] the options for getting configuration @option opts [Boolean] :fail_on_missing (false) Raise an exception if a file in the @sources list is not present
# 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
scan ENV for any keys that start with alfred_ check to see that they are valid names add them to configatron
# 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
checks to see if file exists and loads it @param file [Pathname] file to attempt to load
# 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.expand_path.file? c = File.read(file.expand_path) eval(c) elsif self.fail_when_missing raise ConfigatronPlus::ConfigError.new("Config file does not exist: #{file.to_s}") end end
looks upo the file for a given symbol and calls get_from_file
with it @param s [Symbol] use to look up and load a file
# 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
# 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
Checks a string to make sure it is a valid identifier so we can eval it @param e [String] string to check for validity @return [Boolean] is valid?
# File lib/configatron_plus.rb, line 140 def self.is_valid_key_name?(e) /^[a-z][a-z0-9]*$/.match(e.downcase) end
set @@sources = DEFAULT_SOURCES
# File lib/configatron_plus.rb, line 52 def self.reset_sources self.sources = DEFAULT_SOURCES.dup end
Get the current list of sources @return [Array<String,Symbol>] of sources to look for configurations in
# File lib/configatron_plus.rb, line 41 def self.sources self.config.sources end
Set the sources to use for configatron @param new_val [Array<String,Symbol>] new array of sources.
# 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
# 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
check each element of the list to see if it’s a correct symbol or can be used a a filename.
@return [Boolean]
# 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