Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 4c8cd04
Showing
8 changed files
with
220 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
require "autotest/restart" | ||
|
||
Autotest.add_hook :initialize do |at| | ||
at.testlib = "minitest/autorun" | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
doc | ||
pkg | ||
tmp |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
=== 1.0.0 / 2010-01-04 | ||
|
||
* Birthday! |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
.autotest | ||
CHANGELOG.rdoc | ||
Manifest.txt | ||
README.rdoc | ||
Rakefile | ||
lib/configlet.rb | ||
test/test_configlet.rb |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
= Configlet | ||
|
||
* http://github.com/jbarnette/configlet | ||
|
||
== Description | ||
|
||
A stupid simple wrapper for environment variables. | ||
|
||
This doesn't deserve to be released as a gem, but I'm using it in two | ||
different projects. Seriously, go find a real configuration library | ||
and use it instead. | ||
|
||
== Examples | ||
|
||
require "configlet" | ||
|
||
# assuming... | ||
ENV["THUNK_STATUS"] = "crazy-awesome" | ||
ENV["THUNK_FINISHED"] = "false" | ||
|
||
# some bits of optional config | ||
Configlet.prefix = :thunk | ||
Configlet.default :token => "default-token" | ||
Configlet.munge(:finished) { |k| "true" == k } | ||
|
||
# grabbin' values | ||
Configlet[:status] # => "crazy-awesome" | ||
Configlet[:finished] # => false | ||
Configlet[:token] # => "default-token" | ||
|
||
It's a module, so +include+ or +extend+ it wherever you want. It | ||
extends itself for your convenience. | ||
|
||
== Installation | ||
|
||
$ gem install configlet | ||
|
||
== License | ||
|
||
Copyright 2010 John Barnette (jbarnette@rubyforge.org) | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining | ||
a copy of this software and associated documentation files (the | ||
'Software'), to deal in the Software without restriction, including | ||
without limitation the rights to use, copy, modify, merge, publish, | ||
distribute, sublicense, and/or sell copies of the Software, and to | ||
permit persons to whom the Software is furnished to do so, subject to | ||
the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be | ||
included in all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, | ||
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | ||
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. | ||
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY | ||
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, | ||
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE | ||
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
require "rubygems" | ||
require "hoe" | ||
|
||
Hoe.plugin :doofus, :git | ||
|
||
Hoe.spec "configlet" do | ||
developer "John Barnette", "jbarnette@rubyforge.org" | ||
|
||
self.extra_rdoc_files = Dir["*.rdoc"] | ||
self.history_file = "CHANGELOG.rdoc" | ||
self.readme_file = "README.rdoc" | ||
self.testlib = :minitest | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
# An embarassingly simple environment configuration hash. Too little | ||
# code to be its own library, really. Inflexible and only good for | ||
# people who name environment variables exactly like I do. | ||
|
||
module Configlet | ||
|
||
# What's at the front of our environment variables? This will be | ||
# upcased and a trailing semicolon will be added, so | ||
# <tt>Configlet[:foo]</tt> with a prefix of <tt>thunk</tt> maps to | ||
# the <tt>THUNK_FOO</tt> environment variable. Default is +nil+. | ||
|
||
attr_accessor :prefix | ||
|
||
# Duh. | ||
|
||
VERSION = "1.0.0" | ||
|
||
I = lambda { |v| v } #:nodoc: | ||
|
||
# Grab a config value. +key+ is translated to an unfriendly | ||
# environment name by upcasing, replacing all periods with | ||
# underscores, and prepending (with an underscore) the +prefix+. | ||
# | ||
# If the prefix is set to <tt>"thunk"</tt>, for example, calling | ||
# <tt>Configlet[:severity]</tt> will return the value of the | ||
# <tt>THUNK_SEVERITY</tt> environment variable, or | ||
# <tt>defaults["severity"]</tt> if the env var isn't set. | ||
|
||
def [] key | ||
mungers[key.to_s].call ENV[envify key] || defaults[key.to_s] | ||
end | ||
|
||
# Set an environment value. +key+ is translated to an unfriendly | ||
# environment name as in Configlet#[] above. | ||
|
||
def []= key, value | ||
ENV[envify key] = value | ||
end | ||
|
||
# Set one or more default values. Use "friendly" names, not env | ||
# vars, so a default for the <tt>THUNK_SECRET</tt> could be set | ||
# as <tt>Configlet.default :secret => "sssssh"</tt> (assuming a | ||
# <tt>"thunk"</tt> prefix). | ||
|
||
def default hash | ||
hash.each { |k, v| defaults[k.to_s] = v } | ||
end | ||
|
||
def defaults #:nodoc: | ||
@defaults ||= {} | ||
end | ||
|
||
def envify key #:nodoc: | ||
[prefix, key].compact.join("_").tr(".", "_").upcase | ||
end | ||
|
||
# Mess with a value when it's retrieved. Useful for turning untyped | ||
# environment strings into numbers, booleans, enums, or class | ||
# instances. Here's how to munge a boolean: | ||
# | ||
# Configlet.prefix = :thunk | ||
# Configlet.munge(:enabled) { |v| "true" == v } | ||
# | ||
# ENV["THUNK_ENABLED"] = "false" | ||
# Configlet[:enabled] # => false | ||
|
||
def munge key, &block | ||
mungers[key.to_s] = block | ||
end | ||
|
||
def mungers #:nodoc: | ||
@mungers ||= Hash.new { |h, k| I } | ||
end | ||
|
||
# Set prefix to +nil+, clear defaults and mungers. | ||
|
||
def reset | ||
self.prefix = nil | ||
|
||
defaults.clear | ||
mungers.clear | ||
end | ||
|
||
extend self | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
require "minitest/autorun" | ||
require "configlet" | ||
|
||
class TestConfiglet < MiniTest::Unit::TestCase | ||
def setup | ||
@env = ENV.to_hash | ||
Configlet.reset | ||
end | ||
|
||
def teardown | ||
ENV.replace @env | ||
end | ||
|
||
def test_default | ||
assert_nil Configlet["foo"] | ||
|
||
Configlet.default :foo => "bar" | ||
assert_equal "bar", Configlet["foo"] | ||
end | ||
|
||
def test_get | ||
ENV["FOO"] = "bar" | ||
assert_equal "bar", Configlet[:foo] | ||
|
||
Configlet.prefix = "boom" | ||
ENV["BOOM_FOO"] = "bar" | ||
assert_equal "bar", Configlet[:foo] | ||
end | ||
|
||
def test_munge | ||
ENV["STUPID"] = "true" | ||
Configlet.munge(:stupid) { |v| "true" == v } | ||
assert_equal true, Configlet[:stupid] | ||
end | ||
|
||
def test_set | ||
assert_nil ENV["FOO"] | ||
Configlet[:foo] = "bar" | ||
assert_equal "bar", ENV["FOO"] | ||
|
||
Configlet.prefix = "pow" | ||
Configlet[:thud] = "zap" | ||
assert_equal "zap", ENV["POW_THUD"] | ||
end | ||
end |