en 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 7e0730bf3f441f312f51ca70db752865b50704ba
4
+ data.tar.gz: 55b33939420926b9b580ceaa3fb74372bd2ec5df
5
+ SHA512:
6
+ metadata.gz: 29803135f67434b9a02646c8f35697dd192814d1831c190ac15c52643151c6ff9ad17011062e09ca58a3012fc6d5328c4c16b57b972daa2cb86457eacbc11241
7
+ data.tar.gz: a2c082676f70a311cd37fe6dc5e22e1939389ff04be05d80695c78849044af4733efce0f622cd77d8587861e1c8c3c828c3260ba2da9adaa256f3bf207ef6f6f
data/.gitignore ADDED
@@ -0,0 +1,22 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ *.bundle
19
+ *.so
20
+ *.o
21
+ *.a
22
+ mkmf.log
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Victor Rodrigues
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require 'bundler/gem_tasks'
data/en.gemspec ADDED
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'en/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'en'
8
+ spec.version = EN::VERSION
9
+ spec.authors = ['Victor Rodrigues']
10
+ spec.email = ['victorc.rodrigues@gmail.com']
11
+ spec.summary = 'Simple ENV fetcher'
12
+ spec.homepage = 'https://github.com/rodrigues'
13
+ spec.license = 'MIT'
14
+
15
+ spec.files = `git ls-files -z`.split("\x0")
16
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
17
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
18
+ spec.require_paths = ['lib']
19
+
20
+ spec.add_development_dependency 'bundler', '~> 1.6'
21
+ spec.add_development_dependency 'rake'
22
+ spec.add_development_dependency 'rspec'
23
+ spec.add_development_dependency 'pry'
24
+ spec.add_dependency 'dotenv'
25
+ end
@@ -0,0 +1,17 @@
1
+ require 'en'
2
+
3
+ module EN
4
+ class Namespace
5
+ include ::EN
6
+
7
+ attr_reader :prefix
8
+
9
+ def initialize(prefix)
10
+ @prefix = prefix.to_s.upcase
11
+ end
12
+
13
+ def fetch(variable)
14
+ super "#{prefix}_#{variable}"
15
+ end
16
+ end
17
+ end
data/lib/en/version.rb ADDED
@@ -0,0 +1,3 @@
1
+ module EN
2
+ VERSION = '0.0.1'
3
+ end
data/lib/en.rb ADDED
@@ -0,0 +1,35 @@
1
+ require 'en/version'
2
+
3
+ require 'dotenv'
4
+
5
+ Dotenv.load
6
+
7
+ module EN
8
+ extend self
9
+
10
+ def fetch(variable)
11
+ ENV[variable.to_s.upcase] || begin
12
+ yield if block_given?
13
+ end
14
+ end
15
+
16
+ TYPES = {
17
+ int: method(:Integer),
18
+ integer: method(:Integer),
19
+ float: method(:Float)
20
+ }
21
+
22
+ TYPES.each do |type, conversion|
23
+ define_method "fetch_#{type}" do |variable, error = nil, &block|
24
+ fetch_converted variable, conversion, error, &block
25
+ end
26
+ end
27
+
28
+ def fetch_converted(variable, conversion, error = nil)
29
+ value = fetch(variable)
30
+ value ||= yield if block_given?
31
+ conversion[value]
32
+ rescue
33
+ error ? raise(error) : raise
34
+ end
35
+ end
@@ -0,0 +1,18 @@
1
+ require 'spec_helper'
2
+
3
+ require 'en/namespace'
4
+
5
+ describe EN::Namespace do
6
+ subject(:env) { described_class.new(:test) }
7
+
8
+ describe '.fetch' do
9
+ context 'variable exists' do
10
+ before { ENV['TEST_A'] = 'A' }
11
+ after { ENV.delete 'TEST_A' }
12
+
13
+ it 'returns its value without block' do
14
+ expect(env.fetch(:a)).to eq('A')
15
+ end
16
+ end
17
+ end
18
+ end
data/spec/en_spec.rb ADDED
@@ -0,0 +1,118 @@
1
+ require 'spec_helper'
2
+
3
+ require 'en'
4
+
5
+ describe EN do
6
+ subject(:env) { described_class }
7
+
8
+ describe '.fetch' do
9
+ context 'variable exists' do
10
+ before { ENV['A'] = 'A' }
11
+ after { ENV.delete 'A' }
12
+
13
+ it 'returns its value without block' do
14
+ expect(env.fetch(:a)).to eq('A')
15
+ end
16
+
17
+ it 'returns its value with block' do
18
+ expect(env.fetch(:a) { '1' }).to eq('A')
19
+ expect(env.fetch(:a) { fail }).to eq('A')
20
+ end
21
+ end
22
+
23
+ context 'variable does not exist' do
24
+ it 'returns nil without block' do
25
+ expect(env.fetch(:a)).to be(nil)
26
+ end
27
+
28
+ it 'returns block value with block' do
29
+ expect(env.fetch(:a) { '1' }).to eq('1')
30
+ expect { env.fetch(:a) { fail } }.to raise_error
31
+ end
32
+ end
33
+ end
34
+
35
+ describe '.fetch_int' do
36
+ context 'variable exists' do
37
+ context 'numeric' do
38
+ before { ENV['A'] = '1' }
39
+ after { ENV.delete 'A' }
40
+
41
+ it 'returns its integer value' do
42
+ expect(env.fetch_int(:a)).to eq(1)
43
+ expect(env.fetch_int(:a) { 2 }).to eq(1)
44
+ expect(env.fetch_int(:a) { fail }).to eq(1)
45
+ end
46
+ end
47
+
48
+ context 'not numeric' do
49
+ before { ENV['A'] = 'A' }
50
+ after { ENV.delete 'A' }
51
+
52
+ it 'fails with or without block' do
53
+ expect { env.fetch_int(:a) }.to raise_error
54
+ expect { env.fetch_int(:a, 'wrong') }.to raise_error('wrong')
55
+ expect { env.fetch_int(:a) { 2 } }.to raise_error
56
+ end
57
+ end
58
+
59
+ context 'not present' do
60
+ it 'fails without block' do
61
+ expect { env.fetch_int(:a) }.to raise_error
62
+ expect { env.fetch_int(:a, 'wrong') }.to raise_error('wrong')
63
+ end
64
+
65
+ it 'returns block value when numeric' do
66
+ expect(env.fetch_int(:a) { 1 }).to eq(1)
67
+ end
68
+
69
+ it 'fails when block value is not numeric' do
70
+ expect { env.fetch_int(:a) { 'A' } }.to raise_error
71
+ expect { env.fetch_int(:a, 'wrong') { 'A' }}.to raise_error('wrong')
72
+ end
73
+ end
74
+ end
75
+ end
76
+
77
+ describe '.fetch_float' do
78
+ context 'variable exists' do
79
+ context 'numeric' do
80
+ before { ENV['A'] = '1.2' }
81
+ after { ENV.delete 'A' }
82
+
83
+ it 'returns its float value' do
84
+ expect(env.fetch_float(:a)).to eq(1.2)
85
+ expect(env.fetch_float(:a) { 2 }).to eq(1.2)
86
+ expect(env.fetch_float(:a) { fail }).to eq(1.2)
87
+ end
88
+ end
89
+
90
+ context 'not numeric' do
91
+ before { ENV['A'] = 'A' }
92
+ after { ENV.delete 'A' }
93
+
94
+ it 'fails with or without block' do
95
+ expect { env.fetch_float(:a) }.to raise_error
96
+ expect { env.fetch_float(:a, 'wrong') }.to raise_error('wrong')
97
+ expect { env.fetch_float(:a) { 2 } }.to raise_error
98
+ end
99
+ end
100
+
101
+ context 'not present' do
102
+ it 'fails without block' do
103
+ expect { env.fetch_float(:a) }.to raise_error
104
+ expect { env.fetch_float(:a, 'wrong') }.to raise_error('wrong')
105
+ end
106
+
107
+ it 'returns block value when numeric' do
108
+ expect(env.fetch_float(:a) { 1 }).to eq(1.0)
109
+ end
110
+
111
+ it 'fails when block value is not numeric' do
112
+ expect { env.fetch_float(:a) { 'A' } }.to raise_error
113
+ expect { env.fetch_float(:a, 'wrong') { 'A' }}.to raise_error('wrong')
114
+ end
115
+ end
116
+ end
117
+ end
118
+ end
@@ -0,0 +1,3 @@
1
+ $:.push '.'
2
+
3
+ require 'pry'
metadata ADDED
@@ -0,0 +1,128 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: en
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Victor Rodrigues
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-08-03 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.6'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.6'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: pry
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: dotenv
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ description:
84
+ email:
85
+ - victorc.rodrigues@gmail.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - ".gitignore"
91
+ - Gemfile
92
+ - LICENSE.txt
93
+ - Rakefile
94
+ - en.gemspec
95
+ - lib/en.rb
96
+ - lib/en/namespace.rb
97
+ - lib/en/version.rb
98
+ - spec/en/namespace_spec.rb
99
+ - spec/en_spec.rb
100
+ - spec/spec_helper.rb
101
+ homepage: https://github.com/rodrigues
102
+ licenses:
103
+ - MIT
104
+ metadata: {}
105
+ post_install_message:
106
+ rdoc_options: []
107
+ require_paths:
108
+ - lib
109
+ required_ruby_version: !ruby/object:Gem::Requirement
110
+ requirements:
111
+ - - ">="
112
+ - !ruby/object:Gem::Version
113
+ version: '0'
114
+ required_rubygems_version: !ruby/object:Gem::Requirement
115
+ requirements:
116
+ - - ">="
117
+ - !ruby/object:Gem::Version
118
+ version: '0'
119
+ requirements: []
120
+ rubyforge_project:
121
+ rubygems_version: 2.2.2
122
+ signing_key:
123
+ specification_version: 4
124
+ summary: Simple ENV fetcher
125
+ test_files:
126
+ - spec/en/namespace_spec.rb
127
+ - spec/en_spec.rb
128
+ - spec/spec_helper.rb