Module: Configlet

Extended by:
Configlet
Included in:
Configlet
Defined in:
lib/configlet.rb

Overview

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.

Constant Summary collapse

VERSION =

Duh.

"2.1.1"
I =

:nodoc:

lambda { |v| v }

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#prefixObject

What’s at the front of our environment variables? This will be upcased and a trailing underscore will be added, so Configlet[:foo] with a prefix of thunk maps to the THUNK_FOO environment variable. Default is nil.



12
13
14
# File 'lib/configlet.rb', line 12

def prefix
  @prefix
end

Instance Method Details

#[](key) ⇒ Object

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 "thunk", for example, calling Configlet[:severity] will return the value of the THUNK_SEVERITY environment variable, or defaults["severity"] if the env var isn’t set.



29
30
31
32
33
34
35
36
37
# File 'lib/configlet.rb', line 29

def [] key
  key = key.to_s

  if Proc === value = ENV[envify(key)] || defaults[key]
    defaults[key] = value = value.call # resolve lambda defaults
  end

  mungers[key].call value
end

#[]=(key, value) ⇒ Object

Set an environment value. key is translated to an unfriendly environment name as in Configlet#[] above.



42
43
44
# File 'lib/configlet.rb', line 42

def []= key, value
  ENV[envify(key)] = value
end

#config(prefix = nil, &block) ⇒ Object

Swanky block form. More pleasant to read when setting multiple defaults, e.g.,

Configlet.config :myapp do
  default "email.from" => "[email protected]"
  default :host        => "myapp.local"
end

If prefix isn’t specified, a downcased version of the current class’ name will be used.



89
90
91
92
93
94
95
# File 'lib/configlet.rb', line 89

def config prefix = nil, &block
  self.prefix = prefix ||
    (respond_to?(:name) ? self : self.class).
      name.split("::").last.downcase.to_sym

  instance_eval(&block) if block_given?
end

#default(args, &block) ⇒ Object

Set one or more default values. Use “friendly” names, not env vars, so a default for the THUNK_SECRET could be set as Configlet.default :secret => "sssssh" (assuming a "thunk" prefix).

If a default value is a lambda, it’ll be resolved the first time the config value is retrieved, and the result of calling the lambda will be the new default value.

If a single string or symbol is provided instead of a hash, the method block form may be used to provide a delayed default. This is just sugar over providing a lambda:

default(:foo) { Rails.env } # is the same as
default :foo => lambda { Rails.env }


62
63
64
65
66
67
68
# File 'lib/configlet.rb', line 62

def default args, &block
  if Hash === args
    args.each { |k, v| defaults[k.to_s] = v }
  elsif block_given?
    defaults[args.to_s] = block
  end
end

#defaultsObject

:nodoc:



70
71
72
# File 'lib/configlet.rb', line 70

def defaults #:nodoc:
  @defaults ||= {}
end

#envify(key) ⇒ Object

:nodoc:



74
75
76
# File 'lib/configlet.rb', line 74

def envify key #:nodoc:
  [prefix, key].compact.join("_").tr(".", "_").upcase
end

#munge(key, &block) ⇒ Object

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


107
108
109
# File 'lib/configlet.rb', line 107

def munge key, &block
  mungers[key.to_s] = block
end

#mungersObject

:nodoc:



111
112
113
# File 'lib/configlet.rb', line 111

def mungers #:nodoc:
  @mungers ||= Hash.new { |h, k| I }
end

#url(args, &block) ⇒ Object

Behaves exactly like default, but additionally installs a munger for each key that calls URI.parse.



118
119
120
121
122
123
124
125
# File 'lib/configlet.rb', line 118

def url args, &block
  default args, &block
  require "uri"

  (args.keys rescue [args]).each do |key|
    munge(key) { |v| URI.parse v }
  end
end