pax_global_header00006660000000000000000000000064120561505300014507gustar00rootroot0000000000000052 comment=f97bb8842a2f0fb50c5e59bd2895fdfc71734ef1 climate_control-0.0.3/000077500000000000000000000000001205615053000146655ustar00rootroot00000000000000climate_control-0.0.3/.gitignore000066400000000000000000000002321205615053000166520ustar00rootroot00000000000000*.gem *.rbc .bundle .config .yardoc Gemfile.lock InstalledFiles _yardoc coverage doc/ lib/bundler/man pkg rdoc spec/reports test/tmp test/version_tmp tmp climate_control-0.0.3/.travisci.yml000066400000000000000000000001571205615053000173150ustar00rootroot00000000000000language: ruby rvm: - 1.9.2 - 1.9.3 before_install: - gem update --system branches: only: - master climate_control-0.0.3/Gemfile000066400000000000000000000001441205615053000161570ustar00rootroot00000000000000source 'https://rubygems.org' # Specify your gem's dependencies in climate_control.gemspec gemspec climate_control-0.0.3/LICENSE.txt000066400000000000000000000021041205615053000165050ustar00rootroot00000000000000Copyright (c) 2012 Joshua Clayton and thoughtbot, inc. MIT License Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. climate_control-0.0.3/NEWS000066400000000000000000000000541205615053000153630ustar00rootroot000000000000000.0.1 (November 28, 2012) Initial release climate_control-0.0.3/README.md000066400000000000000000000041111205615053000161410ustar00rootroot00000000000000# Climate Control Easily manage your environment. ## Installation Add this line to your application's Gemfile: gem 'climate_control' And then execute: $ bundle Or install it yourself as: $ gem install climate_control ## Usage `ClimateControl` can be used to temporarily assign environment variables within a block: ```ruby ClimateControl.modify CONFIRMATION_INSTRUCTIONS_BCC: 'confirmation_bcc@example.com' do sign_up_as 'john@example.com' confirm_account_for_email 'john@example.com' current_email.should bcc_to('confirmation_bcc@example.com') end ``` To use with RSpec, you could define this in your spec: ```ruby def with_modified_env(options, &block) ClimateControl.modify(options, &block) end ``` This would allow for more straightforward way to modify the environment: ```ruby require 'spec_helper' describe Thing, 'name' do it 'appends ADDITIONAL_NAME' do with_modified_env ADDITIONAL_NAME: 'bar' do expect(Thing.new.name).to eq('John Doe Bar') end end def with_modified_env(options, &block) ClimateControl.modify(options, &block) end end ``` To modify the environment for an entire set of tests in RSpec, use an `around` block: ```ruby describe Thing, 'name' do # ... tests around do |example| ClimateControl.modify FOO: 'bar' do example.run end end end ``` Environment variables assigned within the block will be preserved; essentially, the code should behave exactly the same with and without the block, except for the overrides. Transparency is crucial because the code executed within the block is not for `ClimateControl` to manage or modify. See the tests for more detail about the specific behaviors. ## Contributing 1. Fork it 2. Create your feature branch (`git checkout -b my-new-feature`) 3. Commit your changes (`git commit -am 'Add some feature'`) 4. Push to the branch (`git push origin my-new-feature`) 5. Create new Pull Request ## License climate_control is copyright 2012 Joshua Clayton and thoughtbot, inc. It is free software and may be redistributed under the terms specified in the LICENSE.txt file. climate_control-0.0.3/Rakefile000066400000000000000000000001641205615053000163330ustar00rootroot00000000000000require 'bundler/gem_tasks' require 'rspec/core/rake_task' RSpec::Core::RakeTask.new(:rspec) task default: :rspec climate_control-0.0.3/climate_control.gemspec000066400000000000000000000017271205615053000214170ustar00rootroot00000000000000# -*- encoding: utf-8 -*- lib = File.expand_path('../lib', __FILE__) $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib) require 'climate_control/version' Gem::Specification.new do |gem| gem.name = 'climate_control' gem.version = ClimateControl::VERSION gem.authors = ['Joshua Clayton'] gem.email = ['joshua.clayton@gmail.com'] gem.description = %q{Modify your ENV} gem.summary = %q{Modify your ENV easily with ClimateControl} gem.homepage = 'https://github.com/thoughtbot/climate_control' gem.files = `git ls-files`.split($/) gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) } gem.test_files = gem.files.grep(%r{^(test|spec|features)/}) gem.require_paths = ['lib'] gem.add_dependency 'activesupport', '>= 3.0' gem.add_development_dependency 'rspec', '~> 2.11' gem.add_development_dependency 'rake', '~> 0.9.2' gem.add_development_dependency 'simplecov', '~> 0.7.1' end climate_control-0.0.3/lib/000077500000000000000000000000001205615053000154335ustar00rootroot00000000000000climate_control-0.0.3/lib/climate_control.rb000066400000000000000000000003171205615053000211370ustar00rootroot00000000000000require 'climate_control/modifier' require 'climate_control/version' module ClimateControl def self.modify(environment_overrides, &block) Modifier.new(environment_overrides, &block).process end end climate_control-0.0.3/lib/climate_control/000077500000000000000000000000001205615053000206115ustar00rootroot00000000000000climate_control-0.0.3/lib/climate_control/modifier.rb000066400000000000000000000035361205615053000227430ustar00rootroot00000000000000require 'active_support/core_ext/hash/keys' module ClimateControl class Modifier def initialize(environment_overrides = {}, &block) @environment_overrides = environment_overrides.dup.stringify_keys! @block = block end def process begin prepare_environment_for_block run_block ensure cache_environment_after_block delete_keys_that_do_not_belong revert_changed_keys end end private def prepare_environment_for_block @original_env = clone_environment copy_overrides_to_environment @env_with_overrides_before_block = clone_environment end def run_block @block.call end def copy_overrides_to_environment @environment_overrides.each do |key, value| ENV[key] = value end end def keys_to_remove @environment_overrides.keys end def keys_changed_by_block @keys_changed_by_block ||= OverlappingKeysWithChangedValues.new(@env_with_overrides_before_block, @env_after_block).keys end def cache_environment_after_block @env_after_block = clone_environment end def delete_keys_that_do_not_belong (keys_to_remove - keys_changed_by_block).each {|key| ENV.delete(key) } end def revert_changed_keys (@original_env.keys - keys_changed_by_block).each do |key| ENV[key] = @original_env[key] end end def clone_environment ENV.to_hash end class OverlappingKeysWithChangedValues def initialize(hash_1, hash_2) @hash_1 = hash_1 @hash_2 = hash_2 end def keys overlapping_keys.select do |overlapping_key| @hash_1[overlapping_key] != @hash_2[overlapping_key] end end private def overlapping_keys @hash_2.keys & @hash_1.keys end end end end climate_control-0.0.3/lib/climate_control/version.rb000066400000000000000000000000561205615053000226240ustar00rootroot00000000000000module ClimateControl VERSION = '0.0.3' end climate_control-0.0.3/spec/000077500000000000000000000000001205615053000156175ustar00rootroot00000000000000climate_control-0.0.3/spec/acceptance/000077500000000000000000000000001205615053000177055ustar00rootroot00000000000000climate_control-0.0.3/spec/acceptance/climate_control_spec.rb000066400000000000000000000004651205615053000244270ustar00rootroot00000000000000require 'spec_helper' describe 'Climate control' do it 'allows modification of the environment' do block_run = false ClimateControl.modify FOO: 'bar' do expect(ENV['FOO']).to eq 'bar' block_run = true end expect(ENV['FOO']).to be_nil expect(block_run).to be_true end end climate_control-0.0.3/spec/lib/000077500000000000000000000000001205615053000163655ustar00rootroot00000000000000climate_control-0.0.3/spec/lib/climate_control/000077500000000000000000000000001205615053000215435ustar00rootroot00000000000000climate_control-0.0.3/spec/lib/climate_control/modifier_spec.rb000066400000000000000000000042611205615053000247030ustar00rootroot00000000000000require 'spec_helper' describe ClimateControl::Modifier do it 'modifies the environment' do with_modified_env VARIABLE_1: 'bar', VARIABLE_2: 'qux' do expect(ENV['VARIABLE_1']).to eq 'bar' expect(ENV['VARIABLE_2']).to eq 'qux' end expect(ENV['VARIABLE_1']).to be_nil expect(ENV['VARIABLE_2']).to be_nil end it 'allows for environment variables to be assigned within the block' do with_modified_env VARIABLE_1: 'modified' do ENV['ASSIGNED_IN_BLOCK'] = 'assigned' end expect(ENV['ASSIGNED_IN_BLOCK']).to eq 'assigned' end it 'reassigns previously set environment variables' do ENV['VARIABLE_ASSIGNED_BEFORE_MODIFYING_ENV'] = 'original' expect(ENV['VARIABLE_ASSIGNED_BEFORE_MODIFYING_ENV']).to eq 'original' with_modified_env VARIABLE_ASSIGNED_BEFORE_MODIFYING_ENV: 'overridden' do expect(ENV['VARIABLE_ASSIGNED_BEFORE_MODIFYING_ENV']).to eq 'overridden' end expect(ENV['VARIABLE_ASSIGNED_BEFORE_MODIFYING_ENV']).to eq 'original' end it 'persists the change when overriding the variable in the block' do with_modified_env VARIABLE_MODIFIED_AND_THEN_ASSIGNED: 'modified' do ENV['VARIABLE_MODIFIED_AND_THEN_ASSIGNED'] = 'assigned value' end expect(ENV['VARIABLE_MODIFIED_AND_THEN_ASSIGNED']).to eq 'assigned value' end it 'resets environment variables even if the block raises' do expect { with_modified_env FOO: 'bar' do raise 'broken' end }.to raise_error expect(ENV['FOO']).to be_nil end it 'preserves environment variables set within the block' do ENV['CHANGED'] = 'old value' with_modified_env IRRELEVANT: 'ignored value' do ENV['CHANGED'] = 'new value' end expect(ENV['CHANGED']).to eq 'new value' end it 'returns the value of the block' do value = with_modified_env VARIABLE_1: 'bar' do 'value inside block' end expect(value).to eq 'value inside block' end def with_modified_env(options, &block) ClimateControl::Modifier.new(options, &block).process end around do |example| old_env = ENV.to_hash example.run ENV.clear old_env.each do |key, value| ENV[key] = value end end end climate_control-0.0.3/spec/spec_helper.rb000066400000000000000000000004261205615053000204370ustar00rootroot00000000000000require 'simplecov' SimpleCov.start $LOAD_PATH << File.join(File.dirname(__FILE__), '..', 'lib') $LOAD_PATH << File.join(File.dirname(__FILE__)) require 'rubygems' require 'rspec' require 'climate_control' Dir['spec/support/**/*.rb'].each { |f| require File.expand_path(f) }