appenv 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.gitignore +1 -0
- data/README.md +35 -0
- data/Rakefile +10 -0
- data/appenv.gemspec +18 -0
- data/lib/appenv.rb +3 -0
- data/lib/appenv/environment.rb +48 -0
- data/lib/appenv/version.rb +5 -0
- data/test/data/env +3 -0
- data/test/test_helper.rb +2 -0
- data/test/unit/test_environment.rb +18 -0
- metadata +85 -0
    
        data/.gitignore
    ADDED
    
    | @@ -0,0 +1 @@ | |
| 1 | 
            +
            .screenrc
         | 
    
        data/README.md
    ADDED
    
    | @@ -0,0 +1,35 @@ | |
| 1 | 
            +
            # APPENV
         | 
| 2 | 
            +
             | 
| 3 | 
            +
            A simple gem that gives you an application environment configuration object.
         | 
| 4 | 
            +
            Values are loaded from ENV or from an environment file such as `.env`.
         | 
| 5 | 
            +
             | 
| 6 | 
            +
            Similar to the `.env` feature in Foreman, but these values will be available
         | 
| 7 | 
            +
            to your application no matter how you launch it (script/rails, Passenger,
         | 
| 8 | 
            +
            Unicorn, rake, etc).
         | 
| 9 | 
            +
             | 
| 10 | 
            +
            For convenience, there is also compatibility with Bash-style environment
         | 
| 11 | 
            +
            declarations in your `.env` file; ie: `export ENV_VAR_NAME="foo"`.
         | 
| 12 | 
            +
             | 
| 13 | 
            +
            ## Example
         | 
| 14 | 
            +
             | 
| 15 | 
            +
            You'd typically create your configuration object in `config/application.rb`.
         | 
| 16 | 
            +
             | 
| 17 | 
            +
            ```ruby
         | 
| 18 | 
            +
            module ExampleProjectName
         | 
| 19 | 
            +
             | 
| 20 | 
            +
              Env = AppEnv::Environment.new { |env, src|
         | 
| 21 | 
            +
                env.domain = src.my_domain_variable || 'example.com'
         | 
| 22 | 
            +
                env.set(:access_codes) {
         | 
| 23 | 
            +
                  src.access_codes ? src.access_codes.split : nil
         | 
| 24 | 
            +
                }
         | 
| 25 | 
            +
              }
         | 
| 26 | 
            +
             | 
| 27 | 
            +
              # class Application < Rails::Application
         | 
| 28 | 
            +
              # ... etc ...
         | 
| 29 | 
            +
              #
         | 
| 30 | 
            +
              # Your config is available globally in ExampleProjectName::Env.
         | 
| 31 | 
            +
              #
         | 
| 32 | 
            +
             | 
| 33 | 
            +
            end
         | 
| 34 | 
            +
             | 
| 35 | 
            +
            ```
         | 
    
        data/Rakefile
    ADDED
    
    
    
        data/appenv.gemspec
    ADDED
    
    | @@ -0,0 +1,18 @@ | |
| 1 | 
            +
            # -*- encoding: utf-8 -*-
         | 
| 2 | 
            +
            require File.expand_path('../lib/appenv/version', __FILE__)
         | 
| 3 | 
            +
             | 
| 4 | 
            +
            Gem::Specification.new do |gem|
         | 
| 5 | 
            +
              gem.authors = ['Joseph Pearson']
         | 
| 6 | 
            +
              gem.email = ['joseph@booki.sh']
         | 
| 7 | 
            +
              gem.description = 'Environment-variable compatible application configuration.'
         | 
| 8 | 
            +
              gem.summary = 'Application environment configuration'
         | 
| 9 | 
            +
              gem.homepage = ''
         | 
| 10 | 
            +
             | 
| 11 | 
            +
              gem.files = `git ls-files`.split($\)
         | 
| 12 | 
            +
              gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
         | 
| 13 | 
            +
              gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
         | 
| 14 | 
            +
              gem.name = 'appenv'
         | 
| 15 | 
            +
              gem.require_paths = ['lib']
         | 
| 16 | 
            +
              gem.version = AppEnv::VERSION
         | 
| 17 | 
            +
              gem.add_dependency('activesupport')
         | 
| 18 | 
            +
            end
         | 
    
        data/lib/appenv.rb
    ADDED
    
    
| @@ -0,0 +1,48 @@ | |
| 1 | 
            +
            class AppEnv::Environment < ActiveSupport::OrderedOptions
         | 
| 2 | 
            +
             | 
| 3 | 
            +
              def initialize(file = '.env', &blk)
         | 
| 4 | 
            +
                super()
         | 
| 5 | 
            +
                @file = file
         | 
| 6 | 
            +
                source = _compile_source_env
         | 
| 7 | 
            +
                blk.call(self, source)
         | 
| 8 | 
            +
              end
         | 
| 9 | 
            +
             | 
| 10 | 
            +
             | 
| 11 | 
            +
              def set(property, &blk)
         | 
| 12 | 
            +
                self.send("#{property}=", blk.call)
         | 
| 13 | 
            +
              end
         | 
| 14 | 
            +
             | 
| 15 | 
            +
             | 
| 16 | 
            +
              private
         | 
| 17 | 
            +
             | 
| 18 | 
            +
                def _compile_source_env
         | 
| 19 | 
            +
                  src = ActiveSupport::OrderedOptions.new
         | 
| 20 | 
            +
                  _source_env_from_file.each_pair { |k, v| src[k] = v; src[k.downcase] = v }
         | 
| 21 | 
            +
                  ENV.each_pair { |k, v| src[k] = v; src[k.downcase] = v }
         | 
| 22 | 
            +
                  src
         | 
| 23 | 
            +
                end
         | 
| 24 | 
            +
             | 
| 25 | 
            +
             | 
| 26 | 
            +
                def _source_env_from_file
         | 
| 27 | 
            +
                  src = {}
         | 
| 28 | 
            +
                  return src  unless File.exists?(@file)
         | 
| 29 | 
            +
                  File.open(@file, 'r').each_line { |ln|
         | 
| 30 | 
            +
                    cln = ln.strip.sub(/^export\s+/, '')
         | 
| 31 | 
            +
                    next  if cln.match(/^#/)
         | 
| 32 | 
            +
                    if m = cln.match(/\A([A-Za-z_0-9]+)=(.*)\z/)
         | 
| 33 | 
            +
                      k = m[1]
         | 
| 34 | 
            +
                      v = m[2]
         | 
| 35 | 
            +
                      if v =~ /\A'(.*)'\z/
         | 
| 36 | 
            +
                        v = $1
         | 
| 37 | 
            +
                      elsif v =~ /\A"(.*)"\z/
         | 
| 38 | 
            +
                        v = $1.gsub(/\\(.)/, '\1')
         | 
| 39 | 
            +
                      end
         | 
| 40 | 
            +
                      src[k] = v
         | 
| 41 | 
            +
                    else
         | 
| 42 | 
            +
                      raise ".env parse error: #{ln}"
         | 
| 43 | 
            +
                    end
         | 
| 44 | 
            +
                  }
         | 
| 45 | 
            +
                  src
         | 
| 46 | 
            +
                end
         | 
| 47 | 
            +
             | 
| 48 | 
            +
            end
         | 
    
        data/test/data/env
    ADDED
    
    
    
        data/test/test_helper.rb
    ADDED
    
    
| @@ -0,0 +1,18 @@ | |
| 1 | 
            +
            require 'test_helper'
         | 
| 2 | 
            +
             | 
| 3 | 
            +
            class AppEnv::TestEnvironment < Test::Unit::TestCase
         | 
| 4 | 
            +
             | 
| 5 | 
            +
              def test_sanity
         | 
| 6 | 
            +
                assert_nothing_raised {
         | 
| 7 | 
            +
                  @env = AppEnv::Environment.new('test/data/env') { |env, src|
         | 
| 8 | 
            +
                    env.var1 = src.var_one
         | 
| 9 | 
            +
                    env.set(:var2) { src.var_two }
         | 
| 10 | 
            +
                    env.var3 = src.var_three || 'grault'
         | 
| 11 | 
            +
                  }
         | 
| 12 | 
            +
                }
         | 
| 13 | 
            +
                assert_equal('foo', @env.var1)
         | 
| 14 | 
            +
                assert_equal('bar', @env.var2)
         | 
| 15 | 
            +
                assert_equal('grault', @env.var3)
         | 
| 16 | 
            +
              end
         | 
| 17 | 
            +
             | 
| 18 | 
            +
            end
         | 
    
        metadata
    ADDED
    
    | @@ -0,0 +1,85 @@ | |
| 1 | 
            +
            --- !ruby/object:Gem::Specification 
         | 
| 2 | 
            +
            name: appenv
         | 
| 3 | 
            +
            version: !ruby/object:Gem::Version 
         | 
| 4 | 
            +
              prerelease: false
         | 
| 5 | 
            +
              segments: 
         | 
| 6 | 
            +
              - 0
         | 
| 7 | 
            +
              - 0
         | 
| 8 | 
            +
              - 1
         | 
| 9 | 
            +
              version: 0.0.1
         | 
| 10 | 
            +
            platform: ruby
         | 
| 11 | 
            +
            authors: 
         | 
| 12 | 
            +
            - Joseph Pearson
         | 
| 13 | 
            +
            autorequire: 
         | 
| 14 | 
            +
            bindir: bin
         | 
| 15 | 
            +
            cert_chain: []
         | 
| 16 | 
            +
             | 
| 17 | 
            +
            date: 2012-09-27 00:00:00 +10:00
         | 
| 18 | 
            +
            default_executable: 
         | 
| 19 | 
            +
            dependencies: 
         | 
| 20 | 
            +
            - !ruby/object:Gem::Dependency 
         | 
| 21 | 
            +
              name: activesupport
         | 
| 22 | 
            +
              prerelease: false
         | 
| 23 | 
            +
              requirement: &id001 !ruby/object:Gem::Requirement 
         | 
| 24 | 
            +
                requirements: 
         | 
| 25 | 
            +
                - - ">="
         | 
| 26 | 
            +
                  - !ruby/object:Gem::Version 
         | 
| 27 | 
            +
                    segments: 
         | 
| 28 | 
            +
                    - 0
         | 
| 29 | 
            +
                    version: "0"
         | 
| 30 | 
            +
              type: :runtime
         | 
| 31 | 
            +
              version_requirements: *id001
         | 
| 32 | 
            +
            description: Environment-variable compatible application configuration.
         | 
| 33 | 
            +
            email: 
         | 
| 34 | 
            +
            - joseph@booki.sh
         | 
| 35 | 
            +
            executables: []
         | 
| 36 | 
            +
             | 
| 37 | 
            +
            extensions: []
         | 
| 38 | 
            +
             | 
| 39 | 
            +
            extra_rdoc_files: []
         | 
| 40 | 
            +
             | 
| 41 | 
            +
            files: 
         | 
| 42 | 
            +
            - .gitignore
         | 
| 43 | 
            +
            - README.md
         | 
| 44 | 
            +
            - Rakefile
         | 
| 45 | 
            +
            - appenv.gemspec
         | 
| 46 | 
            +
            - lib/appenv.rb
         | 
| 47 | 
            +
            - lib/appenv/environment.rb
         | 
| 48 | 
            +
            - lib/appenv/version.rb
         | 
| 49 | 
            +
            - test/data/env
         | 
| 50 | 
            +
            - test/test_helper.rb
         | 
| 51 | 
            +
            - test/unit/test_environment.rb
         | 
| 52 | 
            +
            has_rdoc: true
         | 
| 53 | 
            +
            homepage: ""
         | 
| 54 | 
            +
            licenses: []
         | 
| 55 | 
            +
             | 
| 56 | 
            +
            post_install_message: 
         | 
| 57 | 
            +
            rdoc_options: []
         | 
| 58 | 
            +
             | 
| 59 | 
            +
            require_paths: 
         | 
| 60 | 
            +
            - lib
         | 
| 61 | 
            +
            required_ruby_version: !ruby/object:Gem::Requirement 
         | 
| 62 | 
            +
              requirements: 
         | 
| 63 | 
            +
              - - ">="
         | 
| 64 | 
            +
                - !ruby/object:Gem::Version 
         | 
| 65 | 
            +
                  segments: 
         | 
| 66 | 
            +
                  - 0
         | 
| 67 | 
            +
                  version: "0"
         | 
| 68 | 
            +
            required_rubygems_version: !ruby/object:Gem::Requirement 
         | 
| 69 | 
            +
              requirements: 
         | 
| 70 | 
            +
              - - ">="
         | 
| 71 | 
            +
                - !ruby/object:Gem::Version 
         | 
| 72 | 
            +
                  segments: 
         | 
| 73 | 
            +
                  - 0
         | 
| 74 | 
            +
                  version: "0"
         | 
| 75 | 
            +
            requirements: []
         | 
| 76 | 
            +
             | 
| 77 | 
            +
            rubyforge_project: 
         | 
| 78 | 
            +
            rubygems_version: 1.3.6
         | 
| 79 | 
            +
            signing_key: 
         | 
| 80 | 
            +
            specification_version: 3
         | 
| 81 | 
            +
            summary: Application environment configuration
         | 
| 82 | 
            +
            test_files: 
         | 
| 83 | 
            +
            - test/data/env
         | 
| 84 | 
            +
            - test/test_helper.rb
         | 
| 85 | 
            +
            - test/unit/test_environment.rb
         |