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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 206715ae7f773db5e3f4f38a3883e452d9663023
4
- data.tar.gz: 8222926939dc0d449a98b13d394a997c2910663b
3
+ metadata.gz: e171d19f1dd8bef695d51433259491ef6c8dc3d9
4
+ data.tar.gz: 7d483ec2cc9dbe6e73e71ffbdbba9bf04b7453d8
5
5
  SHA512:
6
- metadata.gz: 765ddaad9d2a4ecb29150945fd016c9314d79e472dd49032b47cda912f47c166faa7710d43eaf46feab31586e3b09e982392ffa9708b65eace8cae231316b5a2
7
- data.tar.gz: d014fd2ca1203dc5c1a9537de121d7603e06a42912626abd3eb7d6c33258916ff1e5c377aa0f7f6d746441abfa069e16de017331f12ec57eeeeb54d8fc29e5be
6
+ metadata.gz: 71c964c3183680d92cc5ffbc5473a19eecf97b65bb62e3780a986d0fbc45974b6700acb8c43eea1f652a41c2d41b5cafa410918efd3bc55741da827550d8b02c
7
+ data.tar.gz: df1f9900dbaec9e6ca93254537b79847acbd3b7857d12ddf8aea6ddc6d9d313e1f747b734de7334747a1aad40a28dda040c1c604760772dbf873240409303970
@@ -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._create_env_method(class << self; self; end, meth, &block)
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 self._create_env_method(instance, meth, &block)
40
- _fail_if_exists(instance, meth)
35
+ def _namespaced_sanitize(meth)
36
+ [@namespace, _sanitize(meth)].compact.join('_')
37
+ end
41
38
 
42
- instance.send(:define_method, meth) do |*args|
43
- env_name = [@namespace, _sanitize(meth)].compact.join('_')
39
+ class << self
40
+ def create_method(meth, &block)
41
+ _create_env_accessor(self, meth, &block)
42
+ end
44
43
 
45
- if args.size == 1
46
- raw_value = args.first
47
- ENV[env_name] = Dumper.new.dump(raw_value, &block)
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
- Loader.new(meth).load(ENV[env_name], &block)
50
+ _create_env_reader(klass, meth, &block)
50
51
  end
51
52
  end
52
- end
53
53
 
54
- def self._fail_if_exists(instance, meth)
55
- fail(AlreadyExistsError, meth) if instance.instance_methods.include?(meth)
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
- class Dumper
4
- def dump(raw_value, &callback)
5
- return callback.call(raw_value) if callback
6
- raw_value.nil? ? nil : raw_value.to_s
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
@@ -0,0 +1,9 @@
1
+ module Nenv
2
+ class Environment
3
+ module Dumper::Default
4
+ def self.call(raw_value)
5
+ raw_value.nil? ? nil : raw_value.to_s
6
+ end
7
+ end
8
+ end
9
+ end
@@ -1,27 +1,18 @@
1
1
  module Nenv
2
2
  class Environment
3
- class Loader
4
- def initialize(meth)
5
- @bool = meth.to_s.end_with?('?')
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 _to_bool(raw_value)
16
- case raw_value
17
- when nil
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
- true
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,9 @@
1
+ module Nenv
2
+ class Environment
3
+ module Loader::Default
4
+ def self.call(raw_value)
5
+ raw_value
6
+ end
7
+ end
8
+ end
9
+ 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
@@ -1,3 +1,3 @@
1
1
  module Nenv
2
- VERSION = '0.2.0'
2
+ VERSION = '0.3.0'
3
3
  end
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.2.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: 2015-01-16 00:00:00.000000000 Z
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.3
97
+ rubygems_version: 2.4.5
95
98
  signing_key:
96
99
  specification_version: 4
97
100
  summary: Convenience wrapper for Ruby's ENV