Class: ENVied
- Inherits:
-
Object
show all
- Defined in:
- lib/envied.rb,
lib/envied/cli.rb,
lib/envied/version.rb
Defined Under Namespace
Modules: Arrayable, Hashable
Classes: Cli, Configuration
Constant Summary
collapse
- VERSION =
'0.7.0'
Class Attribute Summary collapse
Class Method Summary
collapse
Class Attribute Details
.required_groups ⇒ Object
Returns the value of attribute required_groups.
72
73
74
|
# File 'lib/envied.rb', line 72
def required_groups
@required_groups
end
|
Class Method Details
.build_configuration(&block) ⇒ Object
75
76
77
78
79
|
# File 'lib/envied.rb', line 75
def self.build_configuration(&block)
Class.new(Configuration).tap do |c|
c.instance_eval(&block) if block_given?
end
end
|
.configuration(options = {}, &block) ⇒ Object
57
58
59
60
61
62
63
64
|
# File 'lib/envied.rb', line 57
def self.configuration(options = {}, &block)
if block_given?
@configuration = build_configuration(&block).tap do |c|
options.each {|k, v| c.public_send("#{k}=", v) }
end
end
@configuration ||= build_configuration
end
|
66
67
68
69
|
# File 'lib/envied.rb', line 66
def self.configure(options = {}, &block)
deprecation_warning "ENVied.configure will be deprecated. Please generate an Envfile instead (see the envied command)."
configuration(options, &block)
end
|
127
128
129
|
# File 'lib/envied.rb', line 127
def self.configure_via_envfile
configuration { eval(File.read(ENVied.envfile)) }
end
|
186
187
188
|
# File 'lib/envied.rb', line 186
def self.configured_variables
configuration.attribute_set.dup
end
|
.default_value(variable) ⇒ Object
Yields the assigned default for the variable. When defaults are disabled, nil is returned.
166
167
168
|
# File 'lib/envied.rb', line 166
def self.default_value(variable)
defaults_enabled? ? variable.default_value.value : nil
end
|
.defaults_enabled? ⇒ Boolean
223
224
225
|
# File 'lib/envied.rb', line 223
def self.defaults_enabled?
configuration.enable_defaults
end
|
.deprecation_warning(msg) ⇒ Object
235
236
237
|
# File 'lib/envied.rb', line 235
def self.deprecation_warning(msg)
puts "DEPRECATION WARNING: #{msg}"
end
|
114
115
116
117
|
# File 'lib/envied.rb', line 114
def self.ensure_configured!
configure_via_envfile if envfile_exist?
end
|
.env ⇒ Object
154
155
156
157
158
|
# File 'lib/envied.rb', line 154
def self.env
@env ||= begin
Hash[ENV.to_hash.map {|k,v| [k, v.dup.extend(Hashable, Arrayable)] }]
end
end
|
.env_value(variable) ⇒ Object
150
151
152
|
# File 'lib/envied.rb', line 150
def self.env_value(variable)
env[variable.name.to_s]
end
|
.env_value_or_default(variable) ⇒ Object
160
161
162
|
# File 'lib/envied.rb', line 160
def self.env_value_or_default(variable)
env_value(variable) || default_value(variable)
end
|
.envfile ⇒ Object
119
120
121
|
# File 'lib/envied.rb', line 119
def self.envfile
File.expand_path('Envfile')
end
|
.envfile_exist? ⇒ Boolean
123
124
125
|
# File 'lib/envied.rb', line 123
def self.envfile_exist?
File.exist?(envfile)
end
|
.error_on_missing_variables! ⇒ Object
131
132
133
134
135
|
# File 'lib/envied.rb', line 131
def self.error_on_missing_variables!
if missing_variable_names.any?
raise "Please set the following ENV-variables: #{missing_variable_names.sort.join(',')}"
end
end
|
.error_on_uncoercible_variables! ⇒ Object
137
138
139
140
141
142
143
144
145
146
147
148
|
# File 'lib/envied.rb', line 137
def self.error_on_uncoercible_variables!
if non_coercible_variables.any?
single_error = "ENV['%{name}'] ('%{value}' can't be coerced to %{type})"
errors = non_coercible_variables.map do |v|
var_type = v.type.to_s.split("::").last
single_error % { name: v.name, value: env_value_or_default(v), type: var_type }
end.join ", "
raise "Some ENV-variables are not coercible: #{errors}"
end
end
|
.method_missing(method, *args, &block) ⇒ Object
227
228
229
|
# File 'lib/envied.rb', line 227
def self.method_missing(method, *args, &block)
respond_to_missing?(method) ? @instance.public_send(method, *args, &block) : super
end
|
.missing_variable_names ⇒ Object
207
208
209
210
211
|
# File 'lib/envied.rb', line 207
def self.missing_variable_names
unprovided = required_variable_names - provided_variable_names
unprovided -= names_of_required_variables_with_defaults if defaults_enabled?
unprovided
end
|
.names_of_required_variables_with_defaults ⇒ Object
213
214
215
|
# File 'lib/envied.rb', line 213
def self.names_of_required_variables_with_defaults
required_variables_with_defaults.map(&:name).map(&:to_sym)
end
|
.non_coercible_variables ⇒ Object
194
195
196
|
# File 'lib/envied.rb', line 194
def self.non_coercible_variables
required_variables.reject(&method(:variable_coercible?))
end
|
.provided_variable_names ⇒ Object
190
191
192
|
# File 'lib/envied.rb', line 190
def self.provided_variable_names
ENV.keys.map(&:to_sym)
end
|
.require(*groups) ⇒ Object
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
|
# File 'lib/envied.rb', line 81
def self.require(*groups)
groups.compact!
@instance = nil
if groups.any?
self.required_groups = groups.map(&:to_sym)
else
self.required_groups = [:default]
end
ensure_configured!
error_on_missing_variables!
error_on_uncoercible_variables!
_required_variables = required_variables
group_configuration = build_configuration do
_required_variables.each do |v|
@attribute_set << v
end
end
@instance = group_configuration.new(env)
end
|
.required_variable_names ⇒ Array<Symbol>
A list of all configured variable names.
177
178
179
|
# File 'lib/envied.rb', line 177
def self.required_variable_names
required_variables.map(&:name).map(&:to_sym)
end
|
.required_variables ⇒ Object
181
182
183
184
|
# File 'lib/envied.rb', line 181
def self.required_variables
from_required_group = ->(var){ self.required_groups.include?(var.options[:group]) }
configured_variables.to_a.keep_if(&from_required_group)
end
|
.required_variables_with_defaults ⇒ Object
217
218
219
220
221
|
# File 'lib/envied.rb', line 217
def self.required_variables_with_defaults
required_variables.map do |v|
v unless v.default_value.value.nil?
end.compact
end
|
.respond_to_missing?(method, include_private = false) ⇒ Boolean
231
232
233
|
# File 'lib/envied.rb', line 231
def self.respond_to_missing?(method, include_private = false)
@instance.respond_to?(method) || super
end
|
.springified_require(*args) ⇒ Object
102
103
104
|
# File 'lib/envied.rb', line 102
def self.springified_require(*args)
springify { ENVied.require(*args) }
end
|
.springify(&block) ⇒ Object
106
107
108
109
110
111
112
|
# File 'lib/envied.rb', line 106
def self.springify(&block)
if defined?(Spring) && Spring.respond_to?(:watcher)
Spring.after_fork(&block)
else
block.call
end
end
|
.variable_coercible?(variable) ⇒ Boolean
198
199
200
201
202
203
204
205
|
# File 'lib/envied.rb', line 198
def self.variable_coercible?(variable)
var_value = env_value_or_default(variable)
return true if var_value.respond_to?(:call)
!variable.coerce(var_value).nil?
rescue Virtus::CoercionError
return false
end
|