conker 0.12.0 → 0.12.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. data/conker.gemspec +1 -1
  2. data/lib/conker.rb +54 -24
  3. metadata +2 -2
data/conker.gemspec CHANGED
@@ -2,7 +2,7 @@ Gem::Specification.new do |s|
2
2
  s.name = 'conker'
3
3
  s.authors = ['Sam Stokes', 'Conrad Irwin', 'Lee Mallabone', 'Martin Kleppmann']
4
4
  s.email = 'supportive@rapportive.com'
5
- s.version = '0.12.0'
5
+ s.version = '0.12.1'
6
6
  s.summary = %q{Conker will conquer your config.}
7
7
  s.description = "Configuration library."
8
8
  s.homepage = "https://github.com/rapportive/conker"
data/lib/conker.rb CHANGED
@@ -41,34 +41,35 @@ module Conker
41
41
  class << self
42
42
  # Parse a multi-key hash into globals and raise an informative error message on failure.
43
43
  def setup_config!(current_env, *args)
44
- hash = args.extract_options!
45
- config = case args[0]
46
- when Hash; args[0]
47
- when String; require 'yaml'; YAML.parse_file(args[0]).to_ruby
48
- else; ENV
49
- end
44
+ declarations = args.extract_options!
45
+ values = values_hash(args[0])
50
46
 
51
- errors = []
52
- hash.each do |varname, declaration|
53
- begin
54
- Kernel.const_set(varname, declaration.evaluate(current_env, config, varname.to_s))
55
- rescue => error
56
- errors << [varname, error.message]
57
- end
58
- end
59
-
60
- error_message = errors.sort_by {|v, e| v.to_s }.map do |varname, error|
61
- varname.to_s + ': ' + error
62
- end.join(", ")
63
- raise Error, error_message unless errors.empty?
47
+ setup_constants(current_env, declarations, values)
64
48
  end
65
49
 
66
- # A wrapper around setup_config! that uses ENV["RACK_ENV"] || 'development'
67
- def setup_rack_environment!(hash)
68
- ENV["RACK_ENV"] ||= 'development'
50
+ # Like setup_config! but uses ENV['RACK_ENV'] || 'development' as the
51
+ # environment. Also sets constant RACK_ENV.
52
+ #
53
+ # N.B. if using this method, you don't need to specify :RACK_ENV in your
54
+ # variable declarations, and it will complain if you do. This is partly to
55
+ # make clear that this method *won't* read RACK_ENV from your config file,
56
+ # only from the environment variable, for compatibility with other code
57
+ # (e.g. Sinatra) that depends directly on the environment variable.
58
+ def setup_rack_environment!(*args)
59
+ ENV['RACK_ENV'] ||= 'development'
60
+ set_constant(:RACK_ENV, ENV['RACK_ENV'])
61
+
62
+ declarations = args.extract_options!
63
+ values = values_hash(args[0])
64
+
65
+ if declarations.key?('RACK_ENV') || declarations.key?(:RACK_ENV)
66
+ raise Error, "No need to declare RACK_ENV; please remove it to avoid confusion!"
67
+ end
68
+ if ENV.key?('RACK_ENV') && values.key?('RACK_ENV') && (env = ENV['RACK_ENV']) != (conf = values['RACK_ENV'])
69
+ raise "RACK_ENV differs between environment (#{env}) and config (#{conf})! Please remove it from your config."
70
+ end
69
71
 
70
- setup_config!(ENV["RACK_ENV"],
71
- hash.merge(:RACK_ENV => required_in_production(:development => 'development', :test => 'test')))
72
+ setup_constants(ENV['RACK_ENV'], declarations, values)
72
73
  end
73
74
 
74
75
  # Declare an environment variable that is required to be defined in the
@@ -123,6 +124,35 @@ module Conker
123
124
  def optional(declaration_opts = {})
124
125
  VariableDeclaration.new(declaration_opts)
125
126
  end
127
+
128
+ private
129
+ def values_hash(values)
130
+ case values
131
+ when Hash; values
132
+ when String; require 'yaml'; YAML.parse_file(values).to_ruby
133
+ else; ENV
134
+ end
135
+ end
136
+
137
+ def setup_constants(current_env, declarations, values)
138
+ errors = []
139
+ declarations.each do |varname, declaration|
140
+ begin
141
+ set_constant(varname, declaration.evaluate(current_env, values, varname.to_s))
142
+ rescue => error
143
+ errors << [varname, error.message]
144
+ end
145
+ end
146
+
147
+ error_message = errors.sort_by {|v, e| v.to_s }.map do |varname, error|
148
+ varname.to_s + ': ' + error
149
+ end.join(", ")
150
+ raise Error, error_message unless errors.empty?
151
+ end
152
+
153
+ def set_constant(varname, value)
154
+ Kernel.const_set(varname, value)
155
+ end
126
156
  end
127
157
 
128
158
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: conker
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.12.0
4
+ version: 0.12.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -12,7 +12,7 @@ authors:
12
12
  autorequire:
13
13
  bindir: bin
14
14
  cert_chain: []
15
- date: 2013-01-09 00:00:00.000000000 Z
15
+ date: 2013-02-08 00:00:00.000000000 Z
16
16
  dependencies:
17
17
  - !ruby/object:Gem::Dependency
18
18
  name: activesupport