Class: ENVied::EnvProxy
- Inherits:
-
Object
- Object
- ENVied::EnvProxy
- Defined in:
- lib/envied/configuration.rb
Overview
Responsible for anything related to the ENV.
Instance Attribute Summary collapse
-
#coercer ⇒ Object
readonly
Returns the value of attribute coercer.
-
#config ⇒ Object
readonly
Returns the value of attribute config.
-
#groups ⇒ Object
readonly
Returns the value of attribute groups.
Instance Method Summary collapse
- #[](name) ⇒ Object
- #coerce(var) ⇒ Object
- #coerced?(var) ⇒ Boolean
- #coercible?(var) ⇒ Boolean
- #default_value_of(var) ⇒ Object
- #env_value_of(var) ⇒ Object
- #has_key?(name) ⇒ Boolean
-
#initialize(config, options = {}) ⇒ EnvProxy
constructor
A new instance of EnvProxy.
- #missing?(var) ⇒ Boolean
- #missing_variables ⇒ Object
- #uncoercible_variables ⇒ Object
- #value_to_coerce(var) ⇒ Object
- #variables ⇒ Object
- #variables_by_name ⇒ Object
Constructor Details
#initialize(config, options = {}) ⇒ EnvProxy
46 47 48 49 50 |
# File 'lib/envied/configuration.rb', line 46 def initialize(config, = {}) @config = config @coercer = .fetch(:coercer, ENVied::Coercer.new) @groups = .fetch(:groups, []) end |
Instance Attribute Details
#coercer ⇒ Object (readonly)
Returns the value of attribute coercer.
44 45 46 |
# File 'lib/envied/configuration.rb', line 44 def coercer @coercer end |
#config ⇒ Object (readonly)
Returns the value of attribute config.
44 45 46 |
# File 'lib/envied/configuration.rb', line 44 def config @config end |
#groups ⇒ Object (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_variables ⇒ Object
52 53 54 |
# File 'lib/envied/configuration.rb', line 52 def missing_variables variables.select(&method(:missing?)) end |
#uncoercible_variables ⇒ Object
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 |
#variables ⇒ Object
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_name ⇒ Object
66 67 68 |
# File 'lib/envied/configuration.rb', line 66 def variables_by_name Hash[variables.map {|v| [v.name, v] }] end |