duffel 0.0.1 → 0.0.2

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: 7d8b5557e0783d46497128c02928f779cbf1f7ff
4
- data.tar.gz: a9babc104a73f8fdde5148f9fba5654c18fd9bf3
3
+ metadata.gz: f95bb4cf610f9be60c7dfe01671fa23158b8989c
4
+ data.tar.gz: 832e42fe8597aed4c2e611460059f224420de231
5
5
  SHA512:
6
- metadata.gz: a80a022e07991c7ca4d8b80f5786f4e1ac1f1c20f0f60878b64a40b1b1312033b8dcf9e9c3f560fc0dc92e50cb23d987722354d725cd0f54c9be2f851acd39a3
7
- data.tar.gz: c37a98a23cc8fcb6106333bbc6d1fb24af74d206d986e2d637054300176352b4aa72e3ef62ac6f190d7ca10be37f720bc27c9d9580eb1d2d8e4709eebad9d088
6
+ metadata.gz: eef9c610a64e30f2ca4af58a5fa8c0e0c502d9e79ce21faaa4e973255c2da291caebf1c2c4c2e0bd9ff1926e9ecf372b15742b21ddeb245cc2a0caffa00a3239
7
+ data.tar.gz: c7299ef2a77d0a88f002e29c9e8ba5bf135edbafe9104f46caf4d1f73ab6089e00740945bfdf7e86136d834c6f0869584cf5a903474b3da68cee360132922704
data/.travis.yml CHANGED
@@ -1,6 +1,5 @@
1
1
  language: ruby
2
2
  rvm:
3
- - 1.8.7
4
3
  - 1.9.2
5
4
  - 1.9.3
6
5
  - 2.0.0
data/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # Duffel
2
2
 
3
- Fetch your environment settings. Raise errors or set defaults when a setting does not exist.
3
+ Fetch your environment variables. Raise errors or set defaults when a variable does not exist.
4
4
 
5
5
  ## Installation
6
6
 
@@ -60,7 +60,7 @@ Calling `Settings.env_variable_name` will still return you your environment vari
60
60
 
61
61
  I know.
62
62
 
63
- ### Why did you do it this way and not this [obviously better way]
63
+ ### Why did you do it this way and not [obviously better way]?
64
64
 
65
65
  Because I probably don't know the obviously better way. Feel free to submit a pull request or open an issue and I'll look into it!
66
66
 
@@ -68,7 +68,7 @@ Because I probably don't know the obviously better way. Feel free to submit a pu
68
68
 
69
69
  ## Contributing
70
70
 
71
- 1. Fork it ( https://github.com/[my-github-username]/duffel/fork )
71
+ 1. Fork it ( https://github.com/ericroberts/duffel/fork )
72
72
  2. Create your feature branch (`git checkout -b my-new-feature`)
73
73
  3. Commit your changes (`git commit -am 'Add some feature'`)
74
74
  4. Push to the branch (`git push origin my-new-feature`)
data/duffel.gemspec CHANGED
@@ -8,8 +8,8 @@ Gem::Specification.new do |spec|
8
8
  spec.version = Duffel::VERSION
9
9
  spec.authors = ["Eric Roberts"]
10
10
  spec.email = ["ericroberts@gmail.com"]
11
- spec.summary = "Fetch your environment settings."
12
- spec.description = "Fetch your environment settings. Raise errors or set defaults when a setting does not exist."
11
+ spec.summary = "Fetch your environment variables."
12
+ spec.description = "Fetch your environment variables. Raise errors or set defaults when a setting does not exist."
13
13
  spec.homepage = "https://github.com/ericroberts/duffel"
14
14
  spec.license = "MIT"
15
15
 
data/lib/duffel.rb CHANGED
@@ -1,18 +1,34 @@
1
1
  class Duffel
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
 
4
- def self.method_missing(method, *args, &block)
5
- fetch_default = lambda do |key|
6
- raise KeyError.new("key not found: #{key}")
4
+ class << self
5
+
6
+ def method_missing(method, *args, &block)
7
+ define_singleton_method(method) do |options={}|
8
+ options ||= {}
9
+ return_value = options.fetch(:fallback, fetch_default)
10
+ fallback = format_return_value(return_value)
11
+
12
+ env_name = method.to_s.upcase
13
+ ENV.fetch(env_name, &fallback)
14
+ end
15
+ self.send(method, args.first)
7
16
  end
8
17
 
9
- define_singleton_method(method) do |options=(args.first || {})|
10
- return_value = options.fetch(:fallback, fetch_default)
11
- fallback = return_value.is_a?(Proc) ? return_value : lambda { |key| return_value }
18
+ protected
19
+
20
+ def format_return_value(value)
21
+ if value.is_a?(Proc)
22
+ value
23
+ else
24
+ lambda { |_key| value }
25
+ end
26
+ end
12
27
 
13
- env_name = method.to_s.upcase
14
- ENV.fetch(env_name, &fallback)
28
+ def fetch_default
29
+ lambda do |key|
30
+ raise KeyError.new("key not found: #{key}")
31
+ end
15
32
  end
16
- self.send(method)
17
33
  end
18
34
  end
@@ -1,6 +1,8 @@
1
1
  require 'duffel'
2
2
 
3
3
  describe Duffel do
4
+ subject { Duffel }
5
+
4
6
  it 'should not raise an error' do
5
7
  expect { subject }.to_not raise_error
6
8
  end
@@ -18,23 +20,81 @@ describe Duffel do
18
20
  ).to eq 'blah'
19
21
  end
20
22
 
23
+ it 'should be idempotent' do
24
+ expect(
25
+ subject.another_non_existing_method(:fallback => 'blah')
26
+ ).to eq 'blah'
27
+ expect(
28
+ subject.another_non_existing_method(:fallback => 'blah')
29
+ ).to eq 'blah'
30
+ end
31
+
32
+ it 'should allow a different fallback the next time it is called' do
33
+ expect(
34
+ subject.another_non_existing_method(:fallback => 'foo')
35
+ ).to eq 'foo'
36
+ end
37
+
38
+ it 'should allow nothing to be passed and to raise a key error' do
39
+ expect{
40
+ subject.another_non_existing_method
41
+ }.to raise_error KeyError
42
+ end
43
+
21
44
  context 'fallback is nil' do
22
45
  it 'should return nil' do
23
46
  expect(
24
47
  subject.yet_another_non_existing_method(:fallback => nil)
25
48
  ).to eq nil
26
49
  end
50
+
51
+ it 'should be idempotent' do
52
+ expect(
53
+ subject.yet_another_non_existing_method(:fallback => nil)
54
+ ).to eq nil
55
+ expect(
56
+ subject.yet_another_non_existing_method(:fallback => nil)
57
+ ).to eq nil
58
+ end
59
+
60
+ it 'should allow a different fallback the next time it is called' do
61
+ expect(
62
+ subject.yet_another_non_existing_method(:fallback => 'bar')
63
+ ).to eq 'bar'
64
+ end
65
+
66
+ it 'should allow nothing to be passed and to raise a key error' do
67
+ expect{
68
+ subject.yet_another_non_existing_method
69
+ }.to raise_error KeyError
70
+ end
27
71
  end
28
72
  end
29
73
  end
30
74
 
31
75
  context 'with a matching environment variable' do
32
- before { expect(ENV).to receive(:fetch).with('AN_EXISTING_METHOD').and_return(response) }
76
+ before do
77
+ allow(ENV).to(
78
+ receive(:fetch).
79
+ with('AN_EXISTING_METHOD').
80
+ and_return(response)
81
+ )
82
+ end
83
+
33
84
  let(:response) { 'response' }
34
85
 
35
86
  it 'should return the env variable' do
36
87
  expect(subject.an_existing_method).to eq response
37
88
  end
89
+
90
+ it 'should be idempotent' do
91
+ expect(subject.an_existing_method).to eq response
92
+ expect(subject.an_existing_method).to eq response
93
+ end
94
+
95
+ it 'should not return the fallback when the env variable exists' do
96
+ expect(subject.an_existing_method(fallback: 'foo')).to eq response
97
+ end
38
98
  end
39
99
  end
40
100
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: duffel
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Eric Roberts
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-07-10 00:00:00.000000000 Z
11
+ date: 2014-07-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -52,7 +52,7 @@ dependencies:
52
52
  - - "~>"
53
53
  - !ruby/object:Gem::Version
54
54
  version: '3.0'
55
- description: Fetch your environment settings. Raise errors or set defaults when a
55
+ description: Fetch your environment variables. Raise errors or set defaults when a
56
56
  setting does not exist.
57
57
  email:
58
58
  - ericroberts@gmail.com
@@ -93,6 +93,6 @@ rubyforge_project:
93
93
  rubygems_version: 2.2.2
94
94
  signing_key:
95
95
  specification_version: 4
96
- summary: Fetch your environment settings.
96
+ summary: Fetch your environment variables.
97
97
  test_files:
98
98
  - spec/lib/duffel_spec.rb