Class: LitaDotenv::Loader

Inherits:
Object
  • Object
show all
Defined in:
lib/lita_dotenv/loader.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Loader

Returns a new instance of Loader.



8
9
10
11
12
# File 'lib/lita_dotenv/loader.rb', line 8

def initialize(config)
  @config = config
  Dotenv.load
  config_env
end

Instance Attribute Details

#configObject

Returns the value of attribute config.



6
7
8
# File 'lib/lita_dotenv/loader.rb', line 6

def config
  @config
end

Instance Method Details

#config_envObject



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/lita_dotenv/loader.rb', line 14

def config_env
  ENV.keys.grep(/^LITA_/i) do |key|
    val = ENV[key]
    slugs = key.split('__')
     # Convert value types
    if slugs.length==2
      type = slugs[1].downcase
      if type=='typesym'
        val = val.to_sym
      elsif type == 'typebool'
        val = val.downcase == 'true' ? true : false
      elsif type == 'typeint' && val =~ /^\s*[0-9]+\s*$/
        val = val.to_i
      elsif type == 'typejson'
        val = MultiJson.decode val
      elsif type == 'typejsonsym'
        val = MultiJson.decode val, symbolize_keys: true
      end
    end
     # Convert path
    path = slugs[0].split('_')
    path.shift # Remove LITA prefix
    path.each_with_index do |part, i|
      if part =~ /[a-z]/
        path[i] = part.gsub(/([A-Z])/, '_\1')
      end
      path[i].downcase!
    end
    if path.length == 2
      @config.send(path[0]).send("#{path[1]}=",val)
    elsif path.length == 3
      @config.send(path[0]).send(path[1]).send("#{path[2]}=",val)
    else
      raise "Config path length #{path.length} not supported for #{path.join('.')}."
    end
  end
end