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.9.1'

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.configObject (readonly)

Returns the value of attribute config.



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

def config
  @config
end

.envObject (readonly) Also known as: required?

Returns the value of attribute env.



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

def env
  @env
end

Class Method Details

.ensure_spring_after_fork_require(args, options = {}) ⇒ Object



59
60
61
62
63
# File 'lib/envied.rb', line 59

def self.ensure_spring_after_fork_require(args, options = {})
  if spring_enabled? && !options[:via_spring]
    Spring.after_fork { ENVied.require(args, options.merge(:via_spring => true)) }
  end
end

.env!(requested_groups, options = {}) ⇒ Object



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

def self.env!(requested_groups, options = {})
  @env = begin
    @config = options.fetch(:config) { Configuration.load }
    groups = required_groups(*requested_groups)
    EnvProxy.new(@config, groups: groups)
  end
end

.error_on_missing_variables!(options = {}) ⇒ Object



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

def self.error_on_missing_variables!(options = {})
  names = env.missing_variables.map(&:name)
  if names.any?
    msg = "The following environment variables should be set: #{names * ', '}."
    msg << "\nPlease make sure to stop Spring before retrying." if spring_enabled? && !options[:via_spring]
    raise msg
  end
end

.error_on_uncoercible_variables!(options = {}) ⇒ Object



42
43
44
45
46
47
48
49
50
51
# File 'lib/envied.rb', line 42

def self.error_on_uncoercible_variables!(options = {})
  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
  if errors.any?
    msg = "The following environment variables are not coercible: #{errors.join(", ")}."
    msg << "\nPlease make sure to stop Spring before retrying." if spring_enabled? && !options[:via_spring]
    raise msg
  end
end

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



82
83
84
# File 'lib/envied.rb', line 82

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

.require(*args) ⇒ Object



15
16
17
18
19
20
21
22
23
# File 'lib/envied.rb', line 15

def self.require(*args)
  options = args.last.is_a?(Hash) ? args.pop : {}
  requested_groups = (args && !args.empty?) ? args : ENV['ENVIED_GROUPS']
  env!(requested_groups, options)
  error_on_missing_variables!(options)
  error_on_uncoercible_variables!(options)

  ensure_spring_after_fork_require(args, options)
end

.required_groups(*groups) ⇒ Object



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

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)


86
87
88
# File 'lib/envied.rb', line 86

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

.spring_enabled?Boolean

Returns:

  • (Boolean)


78
79
80
# File 'lib/envied.rb', line 78

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

.springify(&block) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/envied.rb', line 65

def self.springify(&block)
  if defined?(ActiveSupport::Deprecation.warn) && !required?
    ActiveSupport::Deprecation.warn(<<-MSG)
It's no longer recommended to `ENVied.require` within ENVied.springify's block. Please re-run `envied init:rails` to upgrade.
MSG
  end
  if spring_enabled?
    Spring.after_fork(&block)
  else
    block.call
  end
end