nenv 0.2.0 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/nenv/environment.rb +39 -16
- data/lib/nenv/environment/dumper.rb +9 -4
- data/lib/nenv/environment/dumper/default.rb +9 -0
- data/lib/nenv/environment/loader.rb +11 -20
- data/lib/nenv/environment/loader/default.rb +9 -0
- data/lib/nenv/environment/loader/predicate.rb +18 -0
- data/lib/nenv/version.rb +1 -1
- metadata +6 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e171d19f1dd8bef695d51433259491ef6c8dc3d9
|
4
|
+
data.tar.gz: 7d483ec2cc9dbe6e73e71ffbdbba9bf04b7453d8
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 71c964c3183680d92cc5ffbc5473a19eecf97b65bb62e3780a986d0fbc45974b6700acb8c43eea1f652a41c2d41b5cafa410918efd3bc55741da827550d8b02c
|
7
|
+
data.tar.gz: df1f9900dbaec9e6ca93254537b79847acbd3b7857d12ddf8aea6ddc6d9d313e1f747b734de7334747a1aad40a28dda040c1c604760772dbf873240409303970
|
data/lib/nenv/environment.rb
CHANGED
@@ -22,12 +22,8 @@ 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
|
-
|
29
25
|
def create_method(meth, &block)
|
30
|
-
self.class.
|
26
|
+
self.class._create_env_accessor(singleton_class, meth, &block)
|
31
27
|
end
|
32
28
|
|
33
29
|
private
|
@@ -36,23 +32,50 @@ module Nenv
|
|
36
32
|
meth.to_s[/^([^=?]*)[=?]?$/, 1].upcase
|
37
33
|
end
|
38
34
|
|
39
|
-
def
|
40
|
-
|
35
|
+
def _namespaced_sanitize(meth)
|
36
|
+
[@namespace, _sanitize(meth)].compact.join('_')
|
37
|
+
end
|
41
38
|
|
42
|
-
|
43
|
-
|
39
|
+
class << self
|
40
|
+
def create_method(meth, &block)
|
41
|
+
_create_env_accessor(self, meth, &block)
|
42
|
+
end
|
44
43
|
|
45
|
-
|
46
|
-
|
47
|
-
|
44
|
+
def _create_env_accessor(klass, meth, &block)
|
45
|
+
_fail_if_accessor_exists(klass, meth)
|
46
|
+
|
47
|
+
if meth.to_s.end_with? '='
|
48
|
+
_create_env_writer(klass, meth, &block)
|
48
49
|
else
|
49
|
-
|
50
|
+
_create_env_reader(klass, meth, &block)
|
50
51
|
end
|
51
52
|
end
|
52
|
-
end
|
53
53
|
|
54
|
-
|
55
|
-
|
54
|
+
private
|
55
|
+
|
56
|
+
def _create_env_writer(klass, meth, &block)
|
57
|
+
env_name = nil
|
58
|
+
dumper = nil
|
59
|
+
klass.send(:define_method, meth) do |raw_value|
|
60
|
+
env_name ||= _namespaced_sanitize(meth)
|
61
|
+
dumper ||= Dumper.setup(&block)
|
62
|
+
ENV[env_name] = dumper.(raw_value)
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
def _create_env_reader(klass, meth, &block)
|
67
|
+
env_name = nil
|
68
|
+
loader = nil
|
69
|
+
klass.send(:define_method, meth) do
|
70
|
+
env_name ||= _namespaced_sanitize(meth)
|
71
|
+
loader ||= Loader.setup(meth, &block)
|
72
|
+
loader.(ENV[env_name])
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
def _fail_if_accessor_exists(klass, meth)
|
77
|
+
fail(AlreadyExistsError, meth) if klass.method_defined?(meth)
|
78
|
+
end
|
56
79
|
end
|
57
80
|
end
|
58
81
|
end
|
@@ -1,9 +1,14 @@
|
|
1
1
|
module Nenv
|
2
2
|
class Environment
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
3
|
+
module Dumper
|
4
|
+
require 'nenv/environment/dumper/default'
|
5
|
+
|
6
|
+
def self.setup(&callback)
|
7
|
+
if callback
|
8
|
+
callback
|
9
|
+
else
|
10
|
+
Default
|
11
|
+
end
|
7
12
|
end
|
8
13
|
end
|
9
14
|
end
|
@@ -1,27 +1,18 @@
|
|
1
1
|
module Nenv
|
2
2
|
class Environment
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
end
|
7
|
-
|
8
|
-
def load(raw_value, &callback)
|
9
|
-
return callback.call(raw_value) if callback
|
10
|
-
@bool ? _to_bool(raw_value) : raw_value
|
11
|
-
end
|
12
|
-
|
13
|
-
private
|
3
|
+
module Loader
|
4
|
+
require 'nenv/environment/loader/predicate'
|
5
|
+
require 'nenv/environment/loader/default'
|
14
6
|
|
15
|
-
def
|
16
|
-
|
17
|
-
|
18
|
-
nil
|
19
|
-
when ''
|
20
|
-
fail ArgumentError, "Can't convert empty string into Bool"
|
21
|
-
when '0', 'false', 'n', 'no', 'NO', 'FALSE'
|
22
|
-
false
|
7
|
+
def self.setup(meth, &callback)
|
8
|
+
if callback
|
9
|
+
callback
|
23
10
|
else
|
24
|
-
|
11
|
+
if meth.to_s.end_with? '?'
|
12
|
+
Predicate
|
13
|
+
else
|
14
|
+
Default
|
15
|
+
end
|
25
16
|
end
|
26
17
|
end
|
27
18
|
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
module Nenv
|
2
|
+
class Environment
|
3
|
+
module Loader::Predicate
|
4
|
+
def self.call(raw_value)
|
5
|
+
case raw_value
|
6
|
+
when nil
|
7
|
+
nil
|
8
|
+
when ''
|
9
|
+
fail ArgumentError, "Can't convert empty string into Bool"
|
10
|
+
when '0', 'false', 'n', 'no', 'NO', 'FALSE'
|
11
|
+
false
|
12
|
+
else
|
13
|
+
true
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
data/lib/nenv/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: nenv
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Cezary Baginski
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2016-02-08 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -68,7 +68,10 @@ files:
|
|
68
68
|
- lib/nenv/builder.rb
|
69
69
|
- lib/nenv/environment.rb
|
70
70
|
- lib/nenv/environment/dumper.rb
|
71
|
+
- lib/nenv/environment/dumper/default.rb
|
71
72
|
- lib/nenv/environment/loader.rb
|
73
|
+
- lib/nenv/environment/loader/default.rb
|
74
|
+
- lib/nenv/environment/loader/predicate.rb
|
72
75
|
- lib/nenv/version.rb
|
73
76
|
- nenv.gemspec
|
74
77
|
homepage: https://github.com/e2/nenv
|
@@ -91,7 +94,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
91
94
|
version: '0'
|
92
95
|
requirements: []
|
93
96
|
rubyforge_project:
|
94
|
-
rubygems_version: 2.4.
|
97
|
+
rubygems_version: 2.4.5
|
95
98
|
signing_key:
|
96
99
|
specification_version: 4
|
97
100
|
summary: Convenience wrapper for Ruby's ENV
|