Class: ENVied::EnvProxy

Inherits:
Object
  • Object
show all
Defined in:
lib/envied/configuration.rb

Overview

Responsible for anything related to the ENV.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config, options = {}) ⇒ EnvProxy



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

def initialize(config, options = {})
  @config = config
  @coercer = options.fetch(:coercer, ENVied::Coercer.new)
  @groups = options.fetch(:groups, [])
end

Instance Attribute Details

#coercerObject (readonly)

Returns the value of attribute coercer.



44
45
46
# File 'lib/envied/configuration.rb', line 44

def coercer
  @coercer
end

#configObject (readonly)

Returns the value of attribute config.



44
45
46
# File 'lib/envied/configuration.rb', line 44

def config
  @config
end

#groupsObject (readonly)

Returns the value of attribute groups.



44
45
46
# File 'lib/envied/configuration.rb', line 44

def groups
  @groups
end

Instance Method Details

#[](name) ⇒ Object



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

def [](name)
  coerce(variables_by_name[name.to_sym])
end

#coerce(var) ⇒ Object



91
92
93
94
95
# File 'lib/envied/configuration.rb', line 91

def coerce(var)
  coerced?(var) ?
    value_to_coerce(var) :
    coercer.coerce(value_to_coerce(var), var.type)
end

#coerced?(var) ⇒ Boolean



105
106
107
# File 'lib/envied/configuration.rb', line 105

def coerced?(var)
  coercer.coerced?(value_to_coerce(var))
end

#coercible?(var) ⇒ Boolean



97
98
99
# File 'lib/envied/configuration.rb', line 97

def coercible?(var)
  coercer.coercible?(value_to_coerce(var), var.type)
end

#default_value_of(var) ⇒ Object



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

def default_value_of(var)
  var.default_value(ENVied, var)
end

#env_value_of(var) ⇒ Object



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

def env_value_of(var)
  ENV[var.name.to_s]
end

#has_key?(name) ⇒ Boolean



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

def has_key?(name)
  variables_by_name[name.to_sym]
end

#missing?(var) ⇒ Boolean



101
102
103
# File 'lib/envied/configuration.rb', line 101

def missing?(var)
  value_to_coerce(var).nil?
end

#missing_variablesObject



52
53
54
# File 'lib/envied/configuration.rb', line 52

def missing_variables
  variables.select(&method(:missing?))
end

#uncoercible_variablesObject



56
57
58
# File 'lib/envied/configuration.rb', line 56

def uncoercible_variables
  variables.reject(&method(:coerced?)).reject(&method(:coercible?))
end

#value_to_coerce(var) ⇒ Object



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

def value_to_coerce(var)
  return env_value_of(var) unless env_value_of(var).nil?
  config.defaults_enabled? ? default_value_of(var) : nil
end

#variablesObject



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

def variables
  @variables ||= begin
    config.variables.select {|v| groups.include?(v.group) }
  end
end

#variables_by_nameObject



66
67
68
# File 'lib/envied/configuration.rb', line 66

def variables_by_name
  Hash[variables.map {|v| [v.name, v] }]
end