with_env 1.0.0 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: abeb311aab413b325d672a8cd217bf1d22fec541
4
- data.tar.gz: e428a385a8b0d016133b1958fc3177c883238ef7
3
+ metadata.gz: cadacf8e6ecc35259d9beaa51e64c41ffee74660
4
+ data.tar.gz: 3186111444fe4b36f3dd437e0a13ed92078ed218
5
5
  SHA512:
6
- metadata.gz: 9a664b57d61bae50eebf8e33a1a8eeb0417d88b2092e70c931a2a97d1520f65a99a90b0c06ceb10db2917b696ee262365105c70dd58473d15d32cf2ea5bce61b
7
- data.tar.gz: 122076216116980a255c0c18764e450ff8299c0f9f31352fbda8e299432a4ba632ef3559deee7765c2c6f347e8b73250096eeeee85a7d434f3a0c69eef318527
6
+ metadata.gz: a72c3f1dca0c0943b560603d4a0df8a868cfca74b35fa24115603c2927215db131d3078bae82ca2a05f7eb73410be897c0a6486a667965b2911a1de619110855
7
+ data.tar.gz: 637e8b9f3f3fe687fcce19fa2d93c44fd3ba004d717801b29f447d76692c2fe2963b72d5fb4cea84a8f82e68ca07eb13624f978b1f2cbca0785ac694e0b9d0a5
data/README.md CHANGED
@@ -1,8 +1,8 @@
1
1
  # WithEnv
2
2
 
3
- Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/with_env`. To experiment with that code, run `bin/console` for an interactive prompt.
3
+ WithEnv is an extremely small helper module for executing code with ENV variables. It exists because
4
+ I got tired of re-writing or copying over a #with_env helper method for the 131st time.
4
5
 
5
- TODO: Delete this and the text above, and describe your gem
6
6
 
7
7
  ## Installation
8
8
 
@@ -22,7 +22,19 @@ Or install it yourself as:
22
22
 
23
23
  ## Usage
24
24
 
25
- TODO: Write usage instructions here
25
+ ```
26
+ include WithEnv
27
+
28
+ with_env("FOO" => "BAR") do
29
+ `echo $FOO` # => "BAR\n"
30
+ ENV["FOO"] # => "BAR"
31
+ end
32
+
33
+ # The ENV has been restored to what it was before the
34
+ # above with_env block, so FOO no longer exists
35
+ `echo $FOO` # => "\n"
36
+ ENV.has_key?("FOO") # => false
37
+ ```
26
38
 
27
39
  ## Development
28
40
 
@@ -32,10 +44,9 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
32
44
 
33
45
  ## Contributing
34
46
 
35
- Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/with_env.
47
+ Bug reports and pull requests are welcome on GitHub at https://github.com/mhs/with_env-rb.
36
48
 
37
49
 
38
50
  ## License
39
51
 
40
52
  The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
41
-
@@ -4,10 +4,18 @@ module WithEnv
4
4
  extend self
5
5
 
6
6
  def with_env(env, &blk)
7
- before = env.inject({}) { |h, (k, _)| h[k] = ENV[k]; h }
7
+ before = ENV.to_h.dup
8
8
  env.each { |k, v| ENV[k] = v }
9
9
  yield
10
10
  ensure
11
- before.each { |k, v| ENV[k] = v }
11
+ ENV.replace(before)
12
+ end
13
+
14
+ def without_env(*keys, &blk)
15
+ before = ENV.to_h.dup
16
+ keys.flatten.each { |k| ENV.delete(k) }
17
+ yield
18
+ ensure
19
+ ENV.replace(before)
12
20
  end
13
21
  end
@@ -1,3 +1,3 @@
1
1
  module WithEnv
2
- VERSION = "1.0.0"
2
+ VERSION = "1.1.0"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: with_env
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Zach Dennis