1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320
|
[](https://travis-ci.org/e2/nenv)
[](http://badge.fury.io/rb/nenv)
[](https://gemnasium.com/e2/nenv)
[](https://codeclimate.com/github/e2/nenv)
[](https://coveralls.io/r/e2/nenv)
# Nenv
Using ENV in Ruby is like using raw SQL statements - it feels wrong, because it is.
If you agree, this gem is for you.
## The benefits over using ENV directly:
- much friendlier stubbing in tests
- you no longer have to care whether false is "0" or "false" or whatever
- NO MORE ALL CAPS EVERYWHERE!
- keys become methods
- namespaces which can be passed around as objects
- you can subclass!
- you can marshal/unmarshal your own types automatically!
- strict mode saves you from doing validation yourself
- and there's more to come...
Other benefits (and compared to other solutions):
- should still work with Ruby 1.8 (in case anyone is still stuck with it)
- it's designed to be as lightweight and as fast as possible compared to ENV
- designed to be both hackable and convenient
## Installation
Add this line to your application's Gemfile:
```ruby
gem 'nenv', '~> 0.1'
```
And then execute:
$ bundle
Or install it yourself as:
$ gem install nenv
## Examples !!!
### Automatic booleans
You no longer have to care whether the value is "0" or "false" or "no" or "FALSE" or ... whatever
```ruby
# Without Nenv
t.verbose = (ENV['CI'] == 'true')
ok = ENV['RUBYGEMS_GEMDEPS'] == "1" || ENV.key?('BUNDLE_GEMFILE')
ENV['DEBUG'] = "true"
```
now becomes:
```ruby
t.verbose = Nenv.ci?
gemdeps = Nenv.rubygems_gemdeps? || Nenv.bundle_gemfile?
Nenv.debug = true
```
### "Namespaces"
```ruby
# Without Nenv
puts ENV['GIT_BROWSER']
puts ENV['GIT_PAGER']
puts ENV['GIT_EDITOR']
```
now becomes:
```ruby
git = Nenv :git
puts git.browser
puts git.pager
puts git.editor
```
Or in block form
```ruby
Nenv :git do |git|
puts git.browser
puts git.pager
puts git.editor
end
```
### Custom type handling
```ruby
# Code without Nenv
paths = [ENV['GEM_HOME`]] + ENV['GEM_PATH'].split(':')
enable_logging if Integer(ENV['WEB_CONCURRENCY']) > 1
mydata = YAML.load(ENV['MY_DATA'])
ENV['VERBOSE'] = debug ? "1" : nil
```
can become:
```ruby
# setup
gem = Nenv :gem
gem.instance.create_method(:path) { |p| p.split(':') }
web = Nenv :web
web.instance.create_method(:concurrency) { |c| Integer(c) }
my = Nenv :my
my.instance.create_method(:data) { |d| YAML.load(d) }
Nenv.instance.create_method(:verbose=) { |v| v ? 1 : nil }
# and then you can simply do:
paths = [gem.home] + gem.path
enable_logging if web.concurrency > 1
mydata = my.data
Nenv.verbose = debug
```
### Automatic conversion to string
```ruby
ENV['RUBYGEMS_GEMDEPS'] = 1 # TypeError: no implicit conversion of Fixnum (...)
```
Nenv automatically uses `to_s`:
```ruby
Nenv.rubygems_gemdeps = 1 # no problem here
```
### Custom assignment
```ruby
data = YAML.load(ENV['MY_DATA'])
data[:foo] = :bar
ENV['MY_DATA'] = YAML.dump(data)
```
can now become:
```ruby
my = Nenv :my
my.instance.create_method(:data) { |d| YAML.load(d) }
my.instance.create_method(:data=) { |d| YAML.dump(d) }
data = my.data
data[:foo] = :bar
my.data = data
```
### Strict mode
```ruby
# Without Nenv
fail 'home not allowed' if ENV['HOME'] = Dir.pwd # BUG! Assignment instead of comparing!
puts ENV['HOME'] # Now contains clobbered value
```
Now, clobbering can be prevented:
```ruby
env = Nenv::Environment.new
env.create_method(:home)
fail 'home not allowed' if env.home = Dir.pwd # Fails with NoMethodError
puts env.home # works
```
### Mashup mode
You can first define all the load/dump logic globally in one place
```ruby
Nenv.instance.create_method(:web_concurrency) { |d| Integer(d) }
Nenv.instance.create_method(:web_concurrency=)
Nenv.instance.create_method(:path) { |p| Pathname(p.split(File::PATH_SEPARATOR)) }
Nenv.instance.create_method(:path=) { |array| array.map(&:to_s).join(File::PATH_SEPARATOR) }
# And now, anywhere in your app:
Nenv.web_concurrency += 3
Nenv.path += Pathname.pwd + "foo"
```
### Your own class (recommended version for simpler unit tests)
```ruby
MyEnv = Nenv::Builder.build do
create_method(:foo?)
end
MyEnv.new('my').foo? # same as ENV['MY_FOO'][/^(?:false|no|n|0)/i,1].nil?
```
### Your own class (dynamic version - not recommended because harder to test)
```ruby
class MyEnv < Nenv::Environment
def initialize
super("my")
create_method(:foo?)
end
end
MyEnv.new.foo? # same as ENV['MY_FOO'][/^(?:false|no|n|0)/i,1].nil?
```
## NOTES
Still, avoid using environment variables if you can.
At least, avoid actually setting them - especially in multithreaded apps.
As for Nenv, while you can access the same variable with or without namespaces,
filters are tied to instances, e.g.:
```ruby
Nenv.instance.create_method(:foo_bar) { |d| Integer(d) }
Nenv('foo').instance.create_method(:bar) { |d| Float(d) }
env = Nenv::Environment.new(:foo).tap { |e| e.create_method(:bar) }
```
all work on the same variable, but each uses a different filter for reading the value.
## Documentation / SemVer / API
Any behavior not mentioned here (in this README) is subject to change. This
includes module names, class names, file names, method names, etc.
If you are relying on behavior not documented here, please open a ticket.
## What's wrong with ENV?
Well sure, having ENV act like a Hash is much better than calling "getenv".
Unfortunately, the advantages of using ENV make no sense:
- it's faster but ... environment variables are rarely used thousands of times in tight loops
- it's already an object ... but there's not much you can do with it (try ENV.class)
- it's globally available ... but you can't isolate it in tests (you need to reset it every time)
- you can use it to set variables ... but it's named like a const
- it allows you to use keys regardless of case ... but by convention lowercase shouldn't be used except for local variables (which are only really used by shell scripts)
- it's supposed to look ugly to discourage use ... but often your app/gem is forced to use 3rd party environment variables anyway
- it's a simple Hash-like class ... but either you encapsulate it in your own classes - or all the value mapping/validation happens everywhere you want the data (yuck!)
But the BIGGEST disadvantage is in specs, e.g.:
```ruby
allow(ENV).to receive(:[]).with('MY_VARIABLE').and_return("foo")
allow(ENV).to receive(:[]=).with('MY_VARIABLE', "foo bar")
# (and if you get the above wrong, you may be debugging for a long, long time...)
```
which could instead be completely isolated as (and without side effects):
```ruby
allow(env).to receive(:variable).and_return("foo")
expect(env).to receive(:variable=).with("foo bar")
# (with verifying doubles it's hard to get it wrong and get stuck)
```
Here's a full example:
```ruby
# In your implementation
MyEnv = Nenv::Builder.build do
create_method(:variable)
create_method(:variable=)
end
class Foo
def foo
MyEnv.new(:my).variable += "bar"
end
end
# Stubbing the class in your specs
RSpec.describe Foo do
let(:env) { instance_double(MyEnv) }
before { allow(MyEnv).to receive(:new).with(:my).and_return(env) }
describe "#foo" do
before { allow(env).to receive(:variable).and_return("foo") }
it "appends a value" do
expect(env).to receive(:variable=).with("foo bar")
subject.foo
end
end
end
```
## Contributing
1. Fork it ( https://github.com/[my-github-username]/nenv/fork )
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 a new Pull Request
|