Sha256: de6bfd29c926af03d2f0d3b6a8f31c45ccf5ababee57c72556810d7e78848a19

Contents?: true

Size: 899 Bytes

Versions: 1

Compression:

Stored size: 899 Bytes

Contents

module EnvManager
  class Builder

    def initialize(file_contents)
      build(file_contents)
    end

    def build(file_contents)
      hash = dynamic_assignment(file_contents)
      hash.each do |key, value|
        instance_variable_set("@#{key}", value)
        self.class.send(:attr_accessor, key)
      end
    end

    %w|development staging test production|.each do |env|
      define_method :"#{env}?" do
        env == ::ENV['RACK_ENV']
      end
    end

    def env
      ::ENV['RACK_ENV']
    end

    private

    # Builds a hash of the .env file
    # Assigns values to the ENV global
    def dynamic_assignment(content)
      return if content.nil? || content == ''
      content.split("\n").inject({}) do |hash, line|
        key, value = line.split('=')
        ::ENV[key] = value.strip

        hash[key.downcase.to_sym] = value.strip
        hash
      end
    end

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
env_manager-0.0.11 lib/env_manager/builder.rb