Class: ENVied

Inherits:
Object
  • Object
show all
Defined in:
lib/envied.rb,
lib/envied/cli.rb,
lib/envied/version.rb,
lib/envied/env_proxy.rb,
lib/envied/configuration.rb,
lib/envied/env_var_extractor.rb

Defined Under Namespace

Classes: Cli, Coercer, Configuration, EnvProxy, EnvVarExtractor, Variable

Constant Summary collapse

VERSION =
'0.8.0'

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.configObject (readonly)

Returns the value of attribute config.



10
11
12
# File 'lib/envied.rb', line 10

def config
  @config
end

.envObject (readonly)

Returns the value of attribute env.



10
11
12
# File 'lib/envied.rb', line 10

def env
  @env
end

Class Method Details

.env!(*args) ⇒ Object



19
20
21
22
23
24
25
26
# File 'lib/envied.rb', line 19

def self.env!(*args)
  @env = begin
    options = args.last.is_a?(Hash) ? args.pop : {}
    config = options.fetch(:config) { Configuration.load }
    groups = required_groups(*args)
    EnvProxy.new(config, groups: groups)
  end
end

.error_on_missing_variables!Object



28
29
30
31
# File 'lib/envied.rb', line 28

def self.error_on_missing_variables!
  names = env.missing_variables.map(&:name)
  raise "The following environment variables should be set: #{names * ', '}" if names.any?
end

.error_on_uncoercible_variables!Object



33
34
35
36
37
38
# File 'lib/envied.rb', line 33

def self.error_on_uncoercible_variables!
  errors = env.uncoercible_variables.map do |v|
    "%{name} ('%{value}' can't be coerced to %{type})" % {name: v.name, value: env.value_to_coerce(v), type: v.type }
  end
  raise "The following environment variables are not coercible: #{errors.join(", ")}" if errors.any?
end

.method_missing(method, *args, &block) ⇒ Object



58
59
60
# File 'lib/envied.rb', line 58

def self.method_missing(method, *args, &block)
  respond_to_missing?(method) ? (env && env[method.to_s]) : super
end

.require(*args) ⇒ Object



13
14
15
16
17
# File 'lib/envied.rb', line 13

def self.require(*args)
  env!(*args)
  error_on_missing_variables!
  error_on_uncoercible_variables!
end

.required_groups(*groups) ⇒ Object



40
41
42
43
44
# File 'lib/envied.rb', line 40

def self.required_groups(*groups)
  splitter = ->(group){ group.is_a?(String) ? group.split(/ *, */) : group }
  result = groups.compact.map(&splitter).flatten
  result.any? ? result.map(&:to_sym) : [:default]
end

.respond_to_missing?(method, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


62
63
64
# File 'lib/envied.rb', line 62

def self.respond_to_missing?(method, include_private = false)
  (env && env.has_key?(method)) || super
end

.spring_enabled?Boolean

Returns:

  • (Boolean)


54
55
56
# File 'lib/envied.rb', line 54

def self.spring_enabled?
  defined?(Spring) && Spring.respond_to?(:watcher)
end

.springify(&block) ⇒ Object



46
47
48
49
50
51
52
# File 'lib/envied.rb', line 46

def self.springify(&block)
  if spring_enabled?
    Spring.after_fork(&block)
  else
    block.call
  end
end