duffel 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: 7d8b5557e0783d46497128c02928f779cbf1f7ff
4
+ data.tar.gz: a9babc104a73f8fdde5148f9fba5654c18fd9bf3
5
+ SHA512:
6
+ metadata.gz: a80a022e07991c7ca4d8b80f5786f4e1ac1f1c20f0f60878b64a40b1b1312033b8dcf9e9c3f560fc0dc92e50cb23d987722354d725cd0f54c9be2f851acd39a3
7
+ data.tar.gz: c37a98a23cc8fcb6106333bbc6d1fb24af74d206d986e2d637054300176352b4aa72e3ef62ac6f190d7ca10be37f720bc27c9d9580eb1d2d8e4709eebad9d088
data/.gitignore ADDED
@@ -0,0 +1,24 @@
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
23
+ .ruby-version
24
+ .ruby-gemset
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format documentation
data/.travis.yml ADDED
@@ -0,0 +1,8 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.8.7
4
+ - 1.9.2
5
+ - 1.9.3
6
+ - 2.0.0
7
+ - 2.1
8
+ - ruby-head
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in duffel.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Eric Roberts
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/README.md ADDED
@@ -0,0 +1,75 @@
1
+ # Duffel
2
+
3
+ Fetch your environment settings. Raise errors or set defaults when a setting does not exist.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'duffel'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install duffel
18
+
19
+ ## Usage
20
+
21
+ Duffel is a simple gem for reading things out of your environment.
22
+
23
+ You have something in your environment named `SUPER_SECRET_PASSWORD`? No problem. To get it out, just do the following:
24
+
25
+ Duffel.super_secret_password
26
+
27
+ Simple, right? But why add a layer of abstraction here? Well, there are a few reasons:
28
+
29
+ Most people get their environment variables like so: `ENV['SUPER_SECRET_PASSWORD']`. This will return nil if the variable is not found. Duffel will return a KeyError, so you are told about missing things.
30
+
31
+ ### But I don't want it to raise an exception!
32
+
33
+ That's fine too, you can specify a fallback for Duffel in case something isn't found. Example:
34
+
35
+ Duffel.super_secret_password(fallback: 'another-password')
36
+
37
+ ### What if I want to just return nil if it's not found?
38
+
39
+ No problem, we can set our fallback to nil:
40
+
41
+ Duffel.super_secret_password(fallback: nil)
42
+
43
+ ### What about other settings I might have?
44
+
45
+ I actually started writing this because I noticed was using a combination of `ENV['SOME_VARIABLE_NAME_HERE']` and `Settings.some_method_name`. I wanted to just use the `Settings.method_name` pattern everywhere, so I wrote Duffel.
46
+
47
+ So while Duffel by default gets things out of your environment, you can use it as a general purpose settings drawer.
48
+
49
+ Let's say you wanted to actually have a constant called Settings but still take advantage of Duffel. You could do something like this:
50
+
51
+ class Settings < Duffel
52
+ def self.some_other_awesome_setting
53
+ 'awesome setting return value'
54
+ end
55
+ end
56
+
57
+ Calling `Settings.env_variable_name` will still return you your environment variable, should it exist.
58
+
59
+ ### Duffel is a stupid name
60
+
61
+ I know.
62
+
63
+ ### Why did you do it this way and not this [obviously better way]
64
+
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
+
67
+
68
+
69
+ ## Contributing
70
+
71
+ 1. Fork it ( https://github.com/[my-github-username]/duffel/fork )
72
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
73
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
74
+ 4. Push to the branch (`git push origin my-new-feature`)
75
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/duffel.gemspec ADDED
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'duffel'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "duffel"
8
+ spec.version = Duffel::VERSION
9
+ spec.authors = ["Eric Roberts"]
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."
13
+ spec.homepage = "https://github.com/ericroberts/duffel"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.6"
22
+ spec.add_development_dependency "rake"
23
+ spec.add_development_dependency "rspec", "~> 3.0"
24
+ end
data/lib/duffel.rb ADDED
@@ -0,0 +1,18 @@
1
+ class Duffel
2
+ VERSION = "0.0.1"
3
+
4
+ def self.method_missing(method, *args, &block)
5
+ fetch_default = lambda do |key|
6
+ raise KeyError.new("key not found: #{key}")
7
+ end
8
+
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 }
12
+
13
+ env_name = method.to_s.upcase
14
+ ENV.fetch(env_name, &fallback)
15
+ end
16
+ self.send(method)
17
+ end
18
+ end
@@ -0,0 +1,40 @@
1
+ require 'duffel'
2
+
3
+ describe Duffel do
4
+ it 'should not raise an error' do
5
+ expect { subject }.to_not raise_error
6
+ end
7
+
8
+ describe '.not_an_existing_method' do
9
+ context 'without a matching environment variable' do
10
+ it 'should raise an error if nothing exists in the environment' do
11
+ expect { subject.not_an_existing_method }.to raise_error(KeyError)
12
+ end
13
+
14
+ context 'with a fallback passed' do
15
+ it 'should return the fallback' do
16
+ expect(
17
+ subject.another_non_existing_method(:fallback => 'blah')
18
+ ).to eq 'blah'
19
+ end
20
+
21
+ context 'fallback is nil' do
22
+ it 'should return nil' do
23
+ expect(
24
+ subject.yet_another_non_existing_method(:fallback => nil)
25
+ ).to eq nil
26
+ end
27
+ end
28
+ end
29
+ end
30
+
31
+ context 'with a matching environment variable' do
32
+ before { expect(ENV).to receive(:fetch).with('AN_EXISTING_METHOD').and_return(response) }
33
+ let(:response) { 'response' }
34
+
35
+ it 'should return the env variable' do
36
+ expect(subject.an_existing_method).to eq response
37
+ end
38
+ end
39
+ end
40
+ end
metadata ADDED
@@ -0,0 +1,98 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: duffel
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Eric Roberts
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-07-10 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: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ description: Fetch your environment settings. Raise errors or set defaults when a
56
+ setting does not exist.
57
+ email:
58
+ - ericroberts@gmail.com
59
+ executables: []
60
+ extensions: []
61
+ extra_rdoc_files: []
62
+ files:
63
+ - ".gitignore"
64
+ - ".rspec"
65
+ - ".travis.yml"
66
+ - Gemfile
67
+ - LICENSE.txt
68
+ - README.md
69
+ - Rakefile
70
+ - duffel.gemspec
71
+ - lib/duffel.rb
72
+ - spec/lib/duffel_spec.rb
73
+ homepage: https://github.com/ericroberts/duffel
74
+ licenses:
75
+ - MIT
76
+ metadata: {}
77
+ post_install_message:
78
+ rdoc_options: []
79
+ require_paths:
80
+ - lib
81
+ required_ruby_version: !ruby/object:Gem::Requirement
82
+ requirements:
83
+ - - ">="
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ required_rubygems_version: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - ">="
89
+ - !ruby/object:Gem::Version
90
+ version: '0'
91
+ requirements: []
92
+ rubyforge_project:
93
+ rubygems_version: 2.2.2
94
+ signing_key:
95
+ specification_version: 4
96
+ summary: Fetch your environment settings.
97
+ test_files:
98
+ - spec/lib/duffel_spec.rb