config_env 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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: bf04daacd474e874dcd756c75225764900265d31
4
+ data.tar.gz: f8e0a12885a96d091158aa92e4ac8a9d3db39ef7
5
+ SHA512:
6
+ metadata.gz: 0be3f107a4d38543e4fdff24ae42960ef76d1df7f2aaeebc5179bb02aaee8840065fdc1b7fb08517989366c304adc552acc1f911573ebf65cf48bb3413ce7714
7
+ data.tar.gz: 5437ea0fd31acadefe267a26d65031ac8dd8c3e2ab65d9b217617166a477f1b6255c766712d224b9319da7873f63ca62fe65abd8a6e239601473613f08988380
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/.ruby-gemset ADDED
@@ -0,0 +1 @@
1
+ config_env
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ ruby-2.0.0
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in config_env.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Sergey Makridenkov
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,64 @@
1
+ # ConfigEnv
2
+
3
+ ENV manager for any Ruby code
4
+
5
+ - Manage ENV[] variables
6
+ - Upload ENV to Heroku
7
+
8
+ Note. It similar to Sinarta configuration
9
+
10
+ ## Installation
11
+
12
+ Add to Gemfile (as top as possible):
13
+
14
+ gem 'config_env'
15
+
16
+ And then execute:
17
+
18
+ $ bundle
19
+
20
+
21
+ ## Usage
22
+
23
+ Create file `config_env.rb`:
24
+
25
+ # any ENV['RACK_ENV']
26
+ config_env do
27
+ set 'omniauth.twitter', 'all'
28
+ end
29
+
30
+ # only when ENV['RACK_ENV'] == :test
31
+ config_env :test do
32
+ set 'omniauth.twitter', 'test'
33
+ end
34
+
35
+ config_env :production, :development do
36
+ set 'omniauth.twitter', 'live'
37
+ end
38
+
39
+ Add line to `.gitignore`
40
+
41
+ config_env.rb
42
+
43
+ ### Heroku
44
+
45
+ If you want use set env at Heroku. Add lines to `Rakefile`
46
+
47
+ require 'config_env/rake_tasks'
48
+ ConfigEnv.path = "#{__dir__}/ruby/config/config_env.rb"
49
+
50
+ Configure Heroku according to config_env
51
+
52
+ rake config_env:heroku
53
+
54
+ Optionally, you can pass in the name of the Heroku app:
55
+
56
+ rake config_env:heroku[app-name]
57
+
58
+ ## Contributing
59
+
60
+ 1. Fork it
61
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
62
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
63
+ 4. Push to the branch (`git push origin my-new-feature`)
64
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,7 @@
1
+ require "bundler/gem_tasks"
2
+
3
+ pattern = '{**/*.rb,**/*.slim,**/*.coffee}'
4
+
5
+ task 'test' do
6
+ system %Q(bundle exec rerun --pattern '#{pattern}' -cx rspec)
7
+ end
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'config_env/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'config_env'
8
+ spec.version = ConfigEnv::VERSION
9
+ spec.authors = ['Sergey Makridenkov']
10
+ spec.email = ['sergey@makridenkov.com']
11
+ spec.description = %q{Manage ENV[] variables, upload ENV to Heroku}
12
+ spec.summary = %q{ENV manager for any Ruby code}
13
+ spec.homepage = ''
14
+ spec.license = 'MIT'
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ['lib']
20
+
21
+ spec.add_development_dependency 'bundler'
22
+ spec.add_development_dependency 'rake'
23
+ spec.add_development_dependency 'rspec'
24
+ spec.add_development_dependency 'rerun'
25
+ end
@@ -0,0 +1,56 @@
1
+ require 'bundler'
2
+ require 'rake'
3
+ require 'shellwords'
4
+ require 'config_env'
5
+
6
+ module ConfigEnv
7
+ module Tasks
8
+ class Heroku
9
+ def initialize(app=nil)
10
+ @app = app
11
+ unless ConfigEnv.path.nil?
12
+ fail unless load(ConfigEnv.path)
13
+ end
14
+ end
15
+
16
+ def upload_env_to_heroku
17
+ puts "Configure Heroku according to config_env[#{environment}]"
18
+ heroku("config:set #{vars}")
19
+ puts ''
20
+ puts heroku('config')
21
+ end
22
+
23
+ private
24
+
25
+ def puts(message)
26
+ super unless ENV['RACK_ENV'] == 'test'
27
+ end
28
+
29
+ def vars
30
+ ConfigEnv.vars(environment).map { |key, value|
31
+ "#{key}=#{Shellwords.escape(value)}"
32
+ }.sort.join(" ")
33
+ end
34
+
35
+ def environment
36
+ @environment ||= heroku("run 'echo $RACK_ENV'").chomp[/(\w+)\z/]
37
+ end
38
+
39
+ def heroku(command)
40
+ with_app = @app ? " --app #{@app}" : ''
41
+ `heroku #{command}#{with_app}`
42
+ end
43
+
44
+ def `(command)
45
+ Bundler.with_clean_env { super }
46
+ end
47
+ end
48
+ end
49
+ end
50
+
51
+ namespace :config_env do
52
+ desc 'Configure Heroku according to config_env'
53
+ task :heroku, [:app] do |_, args|
54
+ ConfigEnv::Tasks::Heroku.new(args[:app]).upload_env_to_heroku
55
+ end
56
+ end
@@ -0,0 +1,3 @@
1
+ module ConfigEnv
2
+ VERSION = "0.0.1"
3
+ end
data/lib/config_env.rb ADDED
@@ -0,0 +1,82 @@
1
+ require "config_env/version"
2
+
3
+ module ConfigEnv
4
+ # path to config_env file. Used in app Rakefile
5
+ def self.path=(new_path)
6
+ @path = new_path
7
+ end
8
+
9
+ def self.path
10
+ @path
11
+ end
12
+
13
+ def self.set_vars(vars, envs)
14
+ @vars ||= {}
15
+
16
+ envs.each do |env|
17
+ env = env.to_s
18
+ @vars[env] ||= {}
19
+ @vars[env].merge!(vars)
20
+ end
21
+ end
22
+
23
+ def self.vars(environment = nil)
24
+ environment ||= self.environment
25
+ environment = environment.to_s
26
+
27
+ vars = (vars_hash[environment] || {}).clone
28
+ vars.merge!(vars_hash["any"] || {})
29
+
30
+ vars
31
+ end
32
+
33
+ def self.clear
34
+ vars_hash.clear
35
+ end
36
+
37
+ def self.environment
38
+ ENV['RACK_ENV'] || 'development'
39
+ end
40
+
41
+ private
42
+
43
+ attr_accessor :vars
44
+
45
+ def self.vars_hash
46
+ @vars ||= {}
47
+ end
48
+
49
+ def self.setup_env(vars, envs)
50
+ if envs == ['any']
51
+ env_fit = true
52
+ else
53
+ env_fit = envs.map{|env| env.to_s}.include?(environment)
54
+ end
55
+
56
+ if env_fit
57
+ vars.each do |key, value|
58
+ ENV[key] = value
59
+ end
60
+ end
61
+ end
62
+
63
+ class Command
64
+ def set(key, value)
65
+ @vars ||= {}
66
+ @vars[key] = value.to_s
67
+ end
68
+
69
+ attr_accessor :vars
70
+ end
71
+ end
72
+
73
+ def config_env(*envs, &code)
74
+ command = ConfigEnv::Command.new
75
+ command.instance_eval(&code)
76
+
77
+ envs = envs.size > 0 ? envs : ['any']
78
+
79
+
80
+ ConfigEnv.set_vars(command.vars, envs)
81
+ ConfigEnv.setup_env(command.vars, envs)
82
+ end
@@ -0,0 +1,36 @@
1
+ require 'spec_helper'
2
+ require 'config_env/rake_tasks'
3
+
4
+ module ConfigEnv::Tasks
5
+ describe Heroku do
6
+ before { ConfigEnv.clear }
7
+
8
+ it 'set use :production as default env'
9
+
10
+ describe '#upload_env_to_heroku' do
11
+ it 'set config:set to heroku' do
12
+ heroku = Heroku.new
13
+
14
+ key2 = "key2_#{rnd}"
15
+ val2 = "val2_#{rnd}"
16
+
17
+ key = "key_#{rnd}"
18
+ val = "val_#{rnd}"
19
+
20
+ config_env :production do
21
+ set key2, val2
22
+ end
23
+
24
+ config_env do
25
+ set key, val
26
+ end
27
+
28
+ expect(heroku).to receive(:heroku){ 'production' }
29
+ expect(heroku).to receive(:heroku).with('config')
30
+ expect(heroku).to receive(:heroku).with("config:set #{key2}=#{val2} #{key}=#{val}")
31
+
32
+ heroku.upload_env_to_heroku
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,71 @@
1
+ require 'spec_helper'
2
+ require 'ostruct'
3
+
4
+ describe ConfigEnv do
5
+ let(:key){ "key_#{rnd}" }
6
+ let(:val){ "val_#{rnd}" }
7
+
8
+ before { ConfigEnv.clear }
9
+
10
+ describe :config_env do
11
+ it 'set ENV from code block' do
12
+ config_env do
13
+ set 'omniauth.twitter', 111
14
+ end
15
+
16
+ ENV['omniauth.twitter'].should == '111'
17
+ end
18
+
19
+ it 'use RACK_ENV as env swither' do
20
+ old_rack_env = ENV['RACK_ENV']
21
+
22
+ ENV['RACK_ENV'] = 'test'
23
+
24
+ config_env :test do
25
+ set 'omniauth.twitter', 'test'
26
+ end
27
+
28
+ config_env :production, :development do
29
+ set 'omniauth.twitter', 'live'
30
+ end
31
+
32
+ ENV['omniauth.twitter'].should == 'test'
33
+
34
+ ENV['RACK_ENV'] = old_rack_env
35
+ end
36
+ end
37
+
38
+ describe :vars do
39
+ it 'returns empty hash by default' do
40
+ ConfigEnv.vars.should == {}
41
+ end
42
+
43
+ it 'returns hash of vars' do
44
+ key = key()
45
+ val = val()
46
+
47
+ config_env do
48
+ set key, val
49
+ end
50
+
51
+ ConfigEnv.vars.should == {key => val}
52
+ end
53
+
54
+ it 'returns production and any envs values' do
55
+ key = key()
56
+ val = val()
57
+ key2 = "key2_#{rnd}"
58
+ val2 = "val2_#{rnd}"
59
+
60
+ config_env :production do
61
+ set key2, val2
62
+ end
63
+
64
+ config_env do
65
+ set key, val
66
+ end
67
+
68
+ ConfigEnv.vars('production').should == {key2 => val2, key => val}
69
+ end
70
+ end
71
+ end
@@ -0,0 +1,13 @@
1
+ ENV['RACK_ENV'] = 'test'
2
+
3
+ require 'config_env'
4
+
5
+ Dir["#{__dir__}/support/**/*.rb"].each{ |path| require path }
6
+
7
+ RSpec.configure do |config|
8
+ config.filter_run focus: true
9
+ config.run_all_when_everything_filtered = true
10
+
11
+ config.include RndHelpers
12
+ end
13
+
@@ -0,0 +1,16 @@
1
+ module RndHelpers
2
+ def rnd(text = nil)
3
+ RndHelpers.rnd(text)
4
+ end
5
+
6
+ # rnd -> 1,5,7
7
+ # rnd('key') -> key 1, key 5, key 7
8
+ def self.rnd(text = nil)
9
+ rnd = Random.rand(10000)
10
+ if text.nil?
11
+ rnd
12
+ else
13
+ "#{text}_#{rnd}"
14
+ end
15
+ end
16
+ end
metadata ADDED
@@ -0,0 +1,119 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: config_env
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Sergey Makridenkov
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-10-17 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '>='
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '>='
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rerun
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description: Manage ENV[] variables, upload ENV to Heroku
70
+ email:
71
+ - sergey@makridenkov.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - .gitignore
77
+ - .ruby-gemset
78
+ - .ruby-version
79
+ - Gemfile
80
+ - LICENSE.txt
81
+ - README.md
82
+ - Rakefile
83
+ - config_env.gemspec
84
+ - lib/config_env.rb
85
+ - lib/config_env/rake_tasks.rb
86
+ - lib/config_env/version.rb
87
+ - spec/config_env/rake_tasts_spec.rb
88
+ - spec/config_env_spec.rb
89
+ - spec/spec_helper.rb
90
+ - spec/support/rnd_helpers.rb
91
+ homepage: ''
92
+ licenses:
93
+ - MIT
94
+ metadata: {}
95
+ post_install_message:
96
+ rdoc_options: []
97
+ require_paths:
98
+ - lib
99
+ required_ruby_version: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - '>='
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ required_rubygems_version: !ruby/object:Gem::Requirement
105
+ requirements:
106
+ - - '>='
107
+ - !ruby/object:Gem::Version
108
+ version: '0'
109
+ requirements: []
110
+ rubyforge_project:
111
+ rubygems_version: 2.0.6
112
+ signing_key:
113
+ specification_version: 4
114
+ summary: ENV manager for any Ruby code
115
+ test_files:
116
+ - spec/config_env/rake_tasts_spec.rb
117
+ - spec/config_env_spec.rb
118
+ - spec/spec_helper.rb
119
+ - spec/support/rnd_helpers.rb