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

Class Method Details

.can_use_as_filename?(val) ⇒ Boolean

Checks to see if we can use this as a filename

Parameters:

  • val (Object)

    Object to check

Returns:

  • (Boolean)

    ]



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

.configObject



8
9
10
# File 'lib/configatron_plus.rb', line 8

def self.config
  configatron.configatron_plus
end

.errorsArray

Returns of symbols.

Returns:

  • (Array)

    of symbols.



47
48
49
# File 'lib/configatron_plus.rb', line 47

def self.errors
  @@errors ||= []
end

.fail_when_missingObject



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_sourcesObject

Parameters:

  • opts (Hash)

    the options for getting configuration



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_envObject

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

Parameters:

  • file (Pathname)

    file to attempt to load

Raises:

  • (ArgumentError)


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.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

.get_from_special(s) ⇒ Object

looks upo the file for a given symbol and calls get_from_file with it

Parameters:

  • s (Symbol)

    use to look up and load a file

Raises:

  • (ArgumentError)


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

Returns:

  • (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

Parameters:

  • e (String)

    string to check for validity

Returns:

  • (Boolean)

    is valid?



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_sourcesObject

set @@sources = DEFAULT_SOURCES



52
53
54
# File 'lib/configatron_plus.rb', line 52

def self.reset_sources
  self.sources = DEFAULT_SOURCES.dup
end

.sourcesArray<String,Symbol>

Get the current list of sources

Returns:

  • (Array<String,Symbol>)

    of sources to look for configurations in



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

Parameters:

  • new_val (Array<String,Symbol>)

    new array of sources.



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

Returns:

  • (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.

Returns:

  • (Boolean)


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