Sha256: 7be304b8bd1c9b590d3949a40e88a18d9fc318fba6cca0626cd7e4aa57a3e6ab

Contents?: true

Size: 1.29 KB

Versions: 1

Compression:

Stored size: 1.29 KB

Contents

require 'dotenv'

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
            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.1 lib/lita_dotenv/loader.rb