Class: Knowledge::Adapters::Environment
- Defined in:
- lib/knowledge/adapters/environment.rb
Overview
Description
This adapter takes some vars in ENV vars and put it in your project’s config.
Usage
@example:
# Define your vars with the name of the variable as key and the name of the env var as value
my_vars = { application_token: 'APPLICATION_TOKEN', aws_secret: 'AWS_SECRET_KEY' }
# Instanciate the adapter
adapter = Knowledge::Adapters::Environment.new(setter: MySetter, variables: my_vars)
# And run it
adapter.run
Attributes
Instance Attribute Summary collapse
-
#setter ⇒ Class
readonly
The current value of setter.
-
#variables ⇒ Hash
readonly
The current value of variables.
Instance Method Summary collapse
-
#initialize(variables:, setter:, params: {}) ⇒ Environment
constructor
Just initializes instance variables with given params.
-
#run ⇒ Object
Runs the actual adapter.
Constructor Details
#initialize(variables:, setter:, params: {}) ⇒ Environment
Just initializes instance variables with given params
Parameters
40 41 42 43 44 |
# File 'lib/knowledge/adapters/environment.rb', line 40 def initialize(variables:, setter:, params: {}) super @raise_not_found = params[:raise_on_value_not_found] || params['raise_on_value_not_found'] || false end |
Instance Attribute Details
#setter ⇒ Class (readonly)
Returns the current value of setter.
27 28 29 |
# File 'lib/knowledge/adapters/environment.rb', line 27 def setter @setter end |
#variables ⇒ Hash (readonly)
Returns the current value of variables.
27 28 29 |
# File 'lib/knowledge/adapters/environment.rb', line 27 def variables @variables end |
Instance Method Details
#run ⇒ Object
Runs the actual adapter.
51 52 53 54 55 56 57 58 59 |
# File 'lib/knowledge/adapters/environment.rb', line 51 def run variables.each do |name_in_project, (name_in_env, default_value)| value = @raise_not_found ? ENV.fetch(name_in_env.to_s) : ENV.fetch(name_in_env.to_s) { default_value } setter.set(name: name_in_project, value: value) end rescue ::KeyError => e raise ::Knowledge::ValueNotFound, "[#{e.class}]: #{e.}" end |