Sha256: a7ec06aad81c3b8c81f0e44288e886428c14178e81acc62347588b0db2e9b024

Contents?: true

Size: 1.34 KB

Versions: 1

Compression:

Stored size: 1.34 KB

Contents

module Enviro
  module Configuration

    class FileNotFound < StandardError; end
    class UnknownEnvironment < StandardError; end

    def self.included(base)
      base.send(:extend,  ClassMethods)
    end

    module ClassMethods

      def configuration_path_env(value=nil)
        @_configuration_path_env ||= 'ENVY_CONF_PATH'
        @_configuration_path_env = value.to_s.upcase unless value.nil?
        @_configuration_path_env
      end

      def configuration_path_str(path=nil)
        @_configuration_path_str = path
      end

      def configuration_path
        if @_configuration_path_str
          @_configuration_path ||= @_configuration_path_str
        else
          @_configuration_path ||= (ENV[self.configuration_path_env]||'enviro.yml')
        end
      end

      def configuration
        @_configuration ||= _load_configuration_path
      end

      private

        def _load_configuration_path
          raise FileNotFound.new(self.configuration_path) unless
            File.exists?(self.configuration_path)

          @raw_configuration = YAML.load_file(self.configuration_path)

          raise UnknownEnvironment.new(self.environment) unless
            @raw_configuration.key?(self.environment)

          OpenStruct.new(@raw_configuration[self.environment].merge(:environment => self.environment))
        end

    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
enviro-0.0.3 lib/enviro/configuration.rb