configlet 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/.autotest ADDED
@@ -0,0 +1,5 @@
1
+ require "autotest/restart"
2
+
3
+ Autotest.add_hook :initialize do |at|
4
+ at.testlib = "minitest/autorun"
5
+ end
data/CHANGELOG.rdoc ADDED
@@ -0,0 +1,3 @@
1
+ === 1.0.0 / 2010-01-04
2
+
3
+ * Birthday!
data/Manifest.txt ADDED
@@ -0,0 +1,7 @@
1
+ .autotest
2
+ CHANGELOG.rdoc
3
+ Manifest.txt
4
+ README.rdoc
5
+ Rakefile
6
+ lib/configlet.rb
7
+ test/test_configlet.rb
data/README.rdoc ADDED
@@ -0,0 +1,59 @@
1
+ = Configlet
2
+
3
+ * http://github.com/jbarnette/configlet
4
+
5
+ == Description
6
+
7
+ A stupid simple wrapper for environment variables.
8
+
9
+ This doesn't deserve to be released as a gem, but I'm using it in two
10
+ different projects. Seriously, go find a real configuration library
11
+ and use it instead.
12
+
13
+ == Examples
14
+
15
+ require "configlet"
16
+
17
+ # assuming...
18
+ ENV["THUNK_STATUS"] = "crazy-awesome"
19
+ ENV["THUNK_FINISHED"] = "false"
20
+
21
+ # some bits of optional config
22
+ Configlet.prefix = :thunk
23
+ Configlet.default :token => "default-token"
24
+ Configlet.munge(:finished) { |k| "true" == k }
25
+
26
+ # grabbin' values
27
+ Configlet[:status] # => "crazy-awesome"
28
+ Configlet[:finished] # => false
29
+ Configlet[:token] # => "default-token"
30
+
31
+ It's a module, so +include+ or +extend+ it wherever you want. It
32
+ extends itself for your convenience.
33
+
34
+ == Installation
35
+
36
+ $ gem install configlet
37
+
38
+ == License
39
+
40
+ Copyright 2010 John Barnette (jbarnette@rubyforge.org)
41
+
42
+ Permission is hereby granted, free of charge, to any person obtaining
43
+ a copy of this software and associated documentation files (the
44
+ 'Software'), to deal in the Software without restriction, including
45
+ without limitation the rights to use, copy, modify, merge, publish,
46
+ distribute, sublicense, and/or sell copies of the Software, and to
47
+ permit persons to whom the Software is furnished to do so, subject to
48
+ the following conditions:
49
+
50
+ The above copyright notice and this permission notice shall be
51
+ included in all copies or substantial portions of the Software.
52
+
53
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
54
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
55
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
56
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
57
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
58
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
59
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/Rakefile ADDED
@@ -0,0 +1,14 @@
1
+ require "rubygems"
2
+ require "hoe"
3
+
4
+ Hoe.plugins.delete :rubyforge
5
+ Hoe.plugin :doofus, :git
6
+
7
+ Hoe.spec "configlet" do
8
+ developer "John Barnette", "jbarnette@rubyforge.org"
9
+
10
+ self.extra_rdoc_files = Dir["*.rdoc"]
11
+ self.history_file = "CHANGELOG.rdoc"
12
+ self.readme_file = "README.rdoc"
13
+ self.testlib = :minitest
14
+ end
data/lib/configlet.rb ADDED
@@ -0,0 +1,85 @@
1
+ # An embarassingly simple environment configuration hash. Too little
2
+ # code to be its own library, really. Inflexible and only good for
3
+ # people who name environment variables exactly like I do.
4
+
5
+ module Configlet
6
+
7
+ # What's at the front of our environment variables? This will be
8
+ # upcased and a trailing semicolon will be added, so
9
+ # <tt>Configlet[:foo]</tt> with a prefix of <tt>thunk</tt> maps to
10
+ # the <tt>THUNK_FOO</tt> environment variable. Default is +nil+.
11
+
12
+ attr_accessor :prefix
13
+
14
+ # Duh.
15
+
16
+ VERSION = "1.0.0"
17
+
18
+ I = lambda { |v| v } #:nodoc:
19
+
20
+ # Grab a config value. +key+ is translated to an unfriendly
21
+ # environment name by upcasing, replacing all periods with
22
+ # underscores, and prepending (with an underscore) the +prefix+.
23
+ #
24
+ # If the prefix is set to <tt>"thunk"</tt>, for example, calling
25
+ # <tt>Configlet[:severity]</tt> will return the value of the
26
+ # <tt>THUNK_SEVERITY</tt> environment variable, or
27
+ # <tt>defaults["severity"]</tt> if the env var isn't set.
28
+
29
+ def [] key
30
+ mungers[key.to_s].call ENV[envify key] || defaults[key.to_s]
31
+ end
32
+
33
+ # Set an environment value. +key+ is translated to an unfriendly
34
+ # environment name as in Configlet#[] above.
35
+
36
+ def []= key, value
37
+ ENV[envify key] = value
38
+ end
39
+
40
+ # Set one or more default values. Use "friendly" names, not env
41
+ # vars, so a default for the <tt>THUNK_SECRET</tt> could be set
42
+ # as <tt>Configlet.default :secret => "sssssh"</tt> (assuming a
43
+ # <tt>"thunk"</tt> prefix).
44
+
45
+ def default hash
46
+ hash.each { |k, v| defaults[k.to_s] = v }
47
+ end
48
+
49
+ def defaults #:nodoc:
50
+ @defaults ||= {}
51
+ end
52
+
53
+ def envify key #:nodoc:
54
+ [prefix, key].compact.join("_").tr(".", "_").upcase
55
+ end
56
+
57
+ # Mess with a value when it's retrieved. Useful for turning untyped
58
+ # environment strings into numbers, booleans, enums, or class
59
+ # instances. Here's how to munge a boolean:
60
+ #
61
+ # Configlet.prefix = :thunk
62
+ # Configlet.munge(:enabled) { |v| "true" == v }
63
+ #
64
+ # ENV["THUNK_ENABLED"] = "false"
65
+ # Configlet[:enabled] # => false
66
+
67
+ def munge key, &block
68
+ mungers[key.to_s] = block
69
+ end
70
+
71
+ def mungers #:nodoc:
72
+ @mungers ||= Hash.new { |h, k| I }
73
+ end
74
+
75
+ # Set prefix to +nil+, clear defaults and mungers.
76
+
77
+ def reset
78
+ self.prefix = nil
79
+
80
+ defaults.clear
81
+ mungers.clear
82
+ end
83
+
84
+ extend self
85
+ end
@@ -0,0 +1,45 @@
1
+ require "minitest/autorun"
2
+ require "configlet"
3
+
4
+ class TestConfiglet < MiniTest::Unit::TestCase
5
+ def setup
6
+ @env = ENV.to_hash
7
+ Configlet.reset
8
+ end
9
+
10
+ def teardown
11
+ ENV.replace @env
12
+ end
13
+
14
+ def test_default
15
+ assert_nil Configlet["foo"]
16
+
17
+ Configlet.default :foo => "bar"
18
+ assert_equal "bar", Configlet["foo"]
19
+ end
20
+
21
+ def test_get
22
+ ENV["FOO"] = "bar"
23
+ assert_equal "bar", Configlet[:foo]
24
+
25
+ Configlet.prefix = "boom"
26
+ ENV["BOOM_FOO"] = "bar"
27
+ assert_equal "bar", Configlet[:foo]
28
+ end
29
+
30
+ def test_munge
31
+ ENV["STUPID"] = "true"
32
+ Configlet.munge(:stupid) { |v| "true" == v }
33
+ assert_equal true, Configlet[:stupid]
34
+ end
35
+
36
+ def test_set
37
+ assert_nil ENV["FOO"]
38
+ Configlet[:foo] = "bar"
39
+ assert_equal "bar", ENV["FOO"]
40
+
41
+ Configlet.prefix = "pow"
42
+ Configlet[:thud] = "zap"
43
+ assert_equal "zap", ENV["POW_THUD"]
44
+ end
45
+ end
metadata ADDED
@@ -0,0 +1,89 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: configlet
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - John Barnette
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2010-02-09 00:00:00 -08:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: gemcutter
17
+ type: :development
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: 0.2.1
24
+ version:
25
+ - !ruby/object:Gem::Dependency
26
+ name: hoe
27
+ type: :development
28
+ version_requirement:
29
+ version_requirements: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: 2.5.0
34
+ version:
35
+ description: |-
36
+ A stupid simple wrapper for environment variables.
37
+
38
+ This doesn't deserve to be released as a gem, but I'm using it in two
39
+ different projects. Seriously, go find a real configuration library
40
+ and use it instead.
41
+ email:
42
+ - jbarnette@rubyforge.org
43
+ executables: []
44
+
45
+ extensions: []
46
+
47
+ extra_rdoc_files:
48
+ - Manifest.txt
49
+ - CHANGELOG.rdoc
50
+ - README.rdoc
51
+ files:
52
+ - .autotest
53
+ - CHANGELOG.rdoc
54
+ - Manifest.txt
55
+ - README.rdoc
56
+ - Rakefile
57
+ - lib/configlet.rb
58
+ - test/test_configlet.rb
59
+ has_rdoc: true
60
+ homepage: http://github.com/jbarnette/configlet
61
+ licenses: []
62
+
63
+ post_install_message:
64
+ rdoc_options:
65
+ - --main
66
+ - README.rdoc
67
+ require_paths:
68
+ - lib
69
+ required_ruby_version: !ruby/object:Gem::Requirement
70
+ requirements:
71
+ - - ">="
72
+ - !ruby/object:Gem::Version
73
+ version: "0"
74
+ version:
75
+ required_rubygems_version: !ruby/object:Gem::Requirement
76
+ requirements:
77
+ - - ">="
78
+ - !ruby/object:Gem::Version
79
+ version: "0"
80
+ version:
81
+ requirements: []
82
+
83
+ rubyforge_project: configlet
84
+ rubygems_version: 1.3.5
85
+ signing_key:
86
+ specification_version: 3
87
+ summary: A stupid simple wrapper for environment variables
88
+ test_files:
89
+ - test/test_configlet.rb