configlet 1.2.0 → 1.3.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,3 +1,7 @@
1
+ === 1.3.0 / 2010-05-24
2
+
3
+ * Add support for lazy defaults.
4
+
1
5
  === 1.2.0 / 2010-05-11
2
6
 
3
7
  * Change Configlet.for to Configlet.config.
@@ -36,6 +36,12 @@ defaults:
36
36
  default :host => "thunk.local"
37
37
  end
38
38
 
39
+ Defaults can be lazy, too: Just use a lambda. The default will be
40
+ resolved the first time its value is grabbed.
41
+
42
+ default :db => lambda { "db://whatever/#{Rails.env}" } # or...
43
+ default(:db) { "db://whatever/#{Rails.env}" }
44
+
39
45
  Configlet is a module, so +include+ or +extend+ it wherever you
40
46
  want. It extends itself for your convenience.
41
47
 
@@ -45,7 +51,7 @@ want. It extends itself for your convenience.
45
51
 
46
52
  == License
47
53
 
48
- Copyright 2010 John Barnette (jbarnette@rubyforge.org)
54
+ Copyright 2010 John Barnette (code@jbarnette.com)
49
55
 
50
56
  Permission is hereby granted, free of charge, to any person obtaining
51
57
  a copy of this software and associated documentation files (the
data/Rakefile CHANGED
@@ -5,7 +5,7 @@ Hoe.plugins.delete :rubyforge
5
5
  Hoe.plugin :doofus, :git
6
6
 
7
7
  Hoe.spec "configlet" do
8
- developer "John Barnette", "jbarnette@rubyforge.org"
8
+ developer "John Barnette", "code@jbarnette.com"
9
9
 
10
10
  self.extra_rdoc_files = Dir["*.rdoc"]
11
11
  self.history_file = "CHANGELOG.rdoc"
@@ -13,7 +13,7 @@ module Configlet
13
13
 
14
14
  # Duh.
15
15
 
16
- VERSION = "1.2.0"
16
+ VERSION = "1.3.0"
17
17
 
18
18
  I = lambda { |v| v } #:nodoc:
19
19
 
@@ -27,7 +27,13 @@ module Configlet
27
27
  # <tt>defaults["severity"]</tt> if the env var isn't set.
28
28
 
29
29
  def [] key
30
- mungers[key.to_s].call ENV[envify(key)] || defaults[key.to_s]
30
+ key = key.to_s
31
+
32
+ if Proc === value = ENV[envify(key)] || defaults[key]
33
+ defaults[key] = value = value.call # resolve lambda defaults
34
+ end
35
+
36
+ mungers[key].call value
31
37
  end
32
38
 
33
39
  # Set an environment value. +key+ is translated to an unfriendly
@@ -41,9 +47,24 @@ module Configlet
41
47
  # vars, so a default for the <tt>THUNK_SECRET</tt> could be set
42
48
  # as <tt>Configlet.default :secret => "sssssh"</tt> (assuming a
43
49
  # <tt>"thunk"</tt> prefix).
44
-
45
- def default hash
46
- hash.each { |k, v| defaults[k.to_s] = v }
50
+ #
51
+ # If a default value is a lambda, it'll be resolved the first time
52
+ # the config value is retrieved, and the result of calling the
53
+ # lambda will be the new default value.
54
+ #
55
+ # If a single string or symbol is provided instead of a hash, the
56
+ # method block form may be used to provide a delayed default. This
57
+ # is just sugar over providing a lambda:
58
+ #
59
+ # default(:foo) { Rails.env } # is the same as
60
+ # default :foo => lambda { Rails.env }
61
+
62
+ def default args, &block
63
+ if Hash === args
64
+ args.each { |k, v| defaults[k.to_s] = v }
65
+ elsif block_given?
66
+ defaults[args.to_s] = block
67
+ end
47
68
  end
48
69
 
49
70
  def defaults #:nodoc:
@@ -33,6 +33,23 @@ class TestConfiglet < MiniTest::Unit::TestCase
33
33
  assert_equal "bar", @cfg["foo"]
34
34
  end
35
35
 
36
+ def test_default_lambda
37
+ l = lambda { "bar" }
38
+ @cfg.default :foo => l
39
+
40
+ assert_equal(l, @cfg.defaults["foo"])
41
+ assert_equal "bar", @cfg["foo"]
42
+ assert_equal "bar", @cfg.defaults["foo"]
43
+ end
44
+
45
+ def test_default_method_block
46
+ block = lambda { "bar" }
47
+ @cfg.default :foo, &block
48
+
49
+ assert_equal block, @cfg.defaults["foo"]
50
+ assert_equal "bar", @cfg[:foo]
51
+ end
52
+
36
53
  def test_get
37
54
  ENV["FOO"] = "bar"
38
55
  assert_equal "bar", @cfg[:foo]
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: configlet
3
3
  version: !ruby/object:Gem::Version
4
- hash: 31
4
+ hash: 27
5
5
  prerelease: false
6
6
  segments:
7
7
  - 1
8
- - 2
8
+ - 3
9
9
  - 0
10
- version: 1.2.0
10
+ version: 1.3.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - John Barnette
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2010-05-11 00:00:00 -07:00
18
+ date: 2010-05-24 00:00:00 -07:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
@@ -41,7 +41,7 @@ description: |-
41
41
  different projects. Seriously, go find a real configuration library
42
42
  and use it instead.
43
43
  email:
44
- - jbarnette@rubyforge.org
44
+ - code@jbarnette.com
45
45
  executables: []
46
46
 
47
47
  extensions: []
@@ -89,7 +89,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
89
89
  requirements: []
90
90
 
91
91
  rubyforge_project: configlet
92
- rubygems_version: 1.3.7.pre.1
92
+ rubygems_version: 1.3.7
93
93
  signing_key:
94
94
  specification_version: 3
95
95
  summary: A stupid simple wrapper for environment variables