nenv 0.0.3 → 0.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: bf78f1e4b9c0b4b0836dcb88c546d8d917506442
4
- data.tar.gz: e18492bf894e4951f9e57bee5c343e507cbf7c06
3
+ metadata.gz: d917265f8d1d8e39066ac71d48d6b3f5893b1d55
4
+ data.tar.gz: 97282240ee65114e25585c6c69d2049c10de1b65
5
5
  SHA512:
6
- metadata.gz: 2370b04d5e91d4817da56f343c36f0e94ababa43073316695c4f3e0f9365073f3031aca40956a4866d0e4b7a8ff6e790a6d3c9dee0596cabbbd56a820e1aa746
7
- data.tar.gz: 23245f176a801613181ab0443aad7c1770d4cf879e83ba2cdbfdb392e0abf37a8a43013952a42d686a47de4c6f7cf57d8f71e9f0ab5a8ec2fdaa0ea1df2c608a
6
+ metadata.gz: aa347c7f19065e60988782362dff2b8b219f48fd5bf0baba29851f6f73b80c1f66fb42ea3f38a90d8afc41f084790367a36f0b19fde2bfd35b47ef35ad2bdde8
7
+ data.tar.gz: 921f77d78cb85ecaca8d2c4e073e8033bb24385b11ef00f936ea978c71489a83a88a597321d46c332089e7046303ba27abfdcb22fc4eacf3343625a3d59d35d0
data/Gemfile CHANGED
@@ -8,4 +8,5 @@ gem 'coveralls', require: false
8
8
  group :development do
9
9
  gem 'guard-rspec', '~> 4.5', require: false
10
10
  gem 'guard-rubocop', require: false
11
+ gem 'pry'
11
12
  end
data/README.md CHANGED
@@ -71,9 +71,9 @@ Nenv.debug = true
71
71
 
72
72
  ```ruby
73
73
  # Without Nenv
74
- puts ENV['GIT_BROWSER`]
75
- puts ENV['GIT_PAGER`]
76
- puts ENV['GIT_EDITOR`]
74
+ puts ENV['GIT_BROWSER']
75
+ puts ENV['GIT_PAGER']
76
+ puts ENV['GIT_EDITOR']
77
77
  ```
78
78
 
79
79
  now becomes:
@@ -186,17 +186,29 @@ Nenv.path += Pathname.pwd + "foo"
186
186
 
187
187
  ```
188
188
 
189
- ### Your own class
189
+ ### Your own class (recommended version for simpler unit tests)
190
+
191
+ ```ruby
192
+ MyEnv = Nenv::Builder.build do
193
+ create_method(:foo?)
194
+ end
195
+
196
+ MyEnv.new('my').foo? # same as ENV['MY_FOO'][/^(?:false|no|n|0)/i,1].nil?
197
+
198
+ ```
199
+
200
+
201
+ ### Your own class (dynamic version - not recommended because harder to test)
190
202
 
191
203
  ```ruby
192
204
  class MyEnv < Nenv::Environment
193
205
  def initialize
194
- super("my_env")
206
+ super("my")
195
207
  create_method(:foo?)
196
208
  end
197
209
  end
198
210
 
199
- MyEnv.new.foo? # same as ENV['MY_ENV_FOO'][/^(?:false|no|n|0)/i,1].nil?
211
+ MyEnv.new.foo? # same as ENV['MY_FOO'][/^(?:false|no|n|0)/i,1].nil?
200
212
 
201
213
  ```
202
214
 
@@ -1,6 +1,7 @@
1
1
  require 'nenv/version'
2
2
 
3
3
  require 'nenv/autoenvironment'
4
+ require 'nenv/builder'
4
5
 
5
6
  def Nenv(namespace = nil)
6
7
  Nenv::AutoEnvironment.new(namespace)
@@ -0,0 +1,9 @@
1
+ require 'nenv/environment'
2
+
3
+ module Nenv
4
+ module Builder
5
+ def self.build(&block)
6
+ Class.new(Nenv::Environment, &block)
7
+ end
8
+ end
9
+ end
@@ -22,26 +22,37 @@ module Nenv
22
22
  @namespace = (namespace ? namespace.upcase : nil)
23
23
  end
24
24
 
25
+ def self.create_method(meth, &block)
26
+ _create_env_method(self, meth, &block)
27
+ end
28
+
25
29
  def create_method(meth, &block)
26
- fail(AlreadyExistsError, meth) if respond_to?(meth)
30
+ self.class._create_env_method(class << self; self; end, meth, &block)
31
+ end
32
+
33
+ private
34
+
35
+ def _sanitize(meth)
36
+ meth.to_s[/^([^=?]*)[=?]?$/, 1].upcase
37
+ end
27
38
 
28
- (class << self; self; end).send(:define_method, meth) do |*args|
29
- raw_value = args.first
39
+ def self._create_env_method(instance, meth, &block)
40
+ _fail_if_exists(instance, meth)
41
+
42
+ instance.send(:define_method, meth) do |*args|
30
43
  env_name = [@namespace, _sanitize(meth)].compact.join('_')
31
44
 
32
- callback = block
33
45
  if args.size == 1
34
- ENV[env_name] = Dumper.new.dump(raw_value, &callback)
46
+ raw_value = args.first
47
+ ENV[env_name] = Dumper.new.dump(raw_value, &block)
35
48
  else
36
- Loader.new(meth).load(ENV[env_name], &callback)
49
+ Loader.new(meth).load(ENV[env_name], &block)
37
50
  end
38
51
  end
39
52
  end
40
53
 
41
- private
42
-
43
- def _sanitize(meth)
44
- meth.to_s[/^([^=?]*)[=?]?$/, 1].upcase
54
+ def self._fail_if_exists(instance, meth)
55
+ fail(AlreadyExistsError, meth) if instance.instance_methods.include?(meth)
45
56
  end
46
57
  end
47
58
  end
@@ -1,3 +1,3 @@
1
1
  module Nenv
2
- VERSION = '0.0.3'
2
+ VERSION = '0.1.0'
3
3
  end
@@ -0,0 +1,27 @@
1
+ require 'nenv/builder'
2
+
3
+ RSpec.describe Nenv::Builder do
4
+ describe '#build' do
5
+ before do
6
+ allow(ENV).to receive(:[]).with('FOO')
7
+ end
8
+
9
+ it 'returns a class with the given methods' do
10
+ FooEnv = Nenv::Builder.build do
11
+ create_method(:foo?)
12
+ end
13
+ FooEnv.new.foo?
14
+ end
15
+
16
+ context 'with duplicate methods' do
17
+ it 'fails' do
18
+ expect do
19
+ FooEnv = Nenv::Builder.build do
20
+ create_method(:foo?)
21
+ create_method(:foo?)
22
+ end
23
+ end.to raise_error(Nenv::Environment::AlreadyExistsError)
24
+ end
25
+ end
26
+ end
27
+ end
@@ -16,7 +16,7 @@ RSpec.describe Nenv do
16
16
  end
17
17
  end
18
18
 
19
- describe 'Nenv() module' do
19
+ describe 'Nenv module' do
20
20
  it 'reads from env' do
21
21
  expect(ENV).to receive(:[]).with('CI').and_return('true')
22
22
  Nenv.ci?
@@ -50,4 +50,17 @@ RSpec.describe Nenv do
50
50
  end
51
51
  end
52
52
  end
53
+
54
+ # Test added here to properly test if builder is required
55
+ describe 'Nenv builder' do
56
+ before do
57
+ allow(ENV).to receive(:[]).with('FOO').and_return('false')
58
+ end
59
+ it 'is required and works' do
60
+ FooEnv = Nenv::Builder.build do
61
+ create_method(:foo?)
62
+ end
63
+ FooEnv.new.foo?
64
+ end
65
+ end
53
66
  end
@@ -10,7 +10,7 @@ RSpec.configure do |config|
10
10
  mocks.verify_partial_doubles = true
11
11
  end
12
12
 
13
- config.filter_run :focus
13
+ config.filter_run focus: ENV['CI'] != 'true'
14
14
  config.run_all_when_everything_filtered = true
15
15
 
16
16
  config.disable_monkey_patching!
@@ -33,6 +33,11 @@ RSpec.configure do |config|
33
33
  allow(ENV).to receive(:[]=) do |key, value|
34
34
  fail "stub me: ENV[#{key.inspect}] = #{value.inspect}"
35
35
  end
36
+
37
+ allow(ENV).to receive(:[]).with('PRYRC').and_call_original
38
+ allow(ENV).to receive(:[]).with('DISABLE_PRY').and_call_original
39
+ allow(ENV).to receive(:[]).with('ANSICON').and_call_original
40
+ allow(ENV).to receive(:[]).with('TERM').and_call_original
36
41
  end
37
42
 
38
43
  config.after do
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: nenv
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Cezary Baginski
@@ -72,11 +72,13 @@ files:
72
72
  - Rakefile
73
73
  - lib/nenv.rb
74
74
  - lib/nenv/autoenvironment.rb
75
+ - lib/nenv/builder.rb
75
76
  - lib/nenv/environment.rb
76
77
  - lib/nenv/environment/dumper.rb
77
78
  - lib/nenv/environment/loader.rb
78
79
  - lib/nenv/version.rb
79
80
  - nenv.gemspec
81
+ - spec/lib/nenv/builder_spec.rb
80
82
  - spec/lib/nenv/environment/dumper_spec.rb
81
83
  - spec/lib/nenv/environment/loader_spec.rb
82
84
  - spec/lib/nenv/environment_spec.rb
@@ -107,6 +109,7 @@ signing_key:
107
109
  specification_version: 4
108
110
  summary: Convenience wrapper for Ruby's ENV
109
111
  test_files:
112
+ - spec/lib/nenv/builder_spec.rb
110
113
  - spec/lib/nenv/environment/dumper_spec.rb
111
114
  - spec/lib/nenv/environment/loader_spec.rb
112
115
  - spec/lib/nenv/environment_spec.rb