Class: Knowledge::Adapters::Environment

Inherits:
Base
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initialize(variables:, setter:, params: {}) ⇒ Environment

Just initializes instance variables with given params

Parameters

Parameters:

  • :variables (Hash)
  • :setter (Class)
  • :params (Hash)


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

#setterClass (readonly)

Returns the current value of setter.

Returns:

  • (Class)

    the current value of setter



27
28
29
# File 'lib/knowledge/adapters/environment.rb', line 27

def setter
  @setter
end

#variablesHash (readonly)

Returns the current value of variables.

Returns:

  • (Hash)

    the current value of variables



27
28
29
# File 'lib/knowledge/adapters/environment.rb', line 27

def variables
  @variables
end

Instance Method Details

#runObject

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.message}"
end