Sha256: 25afceb9081e1f4fe7a39e03b1a29faccfd9eb0fe240e5ddb961a55b63f6a0d4

Contents?: true

Size: 1.41 KB

Versions: 1

Compression:

Stored size: 1.41 KB

Contents

require 'dotenv'
require 'multi_json'

module LitaDotenv
  class Loader
    attr_accessor :config

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

     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' && type =~ /^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
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
lita_dotenv-0.0.2 lib/lita_dotenv/loader.rb