Class: Nvar::EnvironmentVariable

Inherits:
Object
  • Object
show all
Defined in:
lib/nvar/environment_variable.rb

Overview

Wrapper for loading environment variables, used across relevant Rake tasks

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name:, type: "String", filter_from_requests: nil, **args) ⇒ EnvironmentVariable

Returns a new instance of EnvironmentVariable.



14
15
16
17
18
19
20
21
22
23
24
# File 'lib/nvar/environment_variable.rb', line 14

def initialize(name:, type: "String", filter_from_requests: nil, **args)
  @name = name
  @type = type
  @required = args[:required].nil? ? true : args[:required]
  @filter_from_requests = filter_from_requests.yield_self { |f| [true, false].include?(f) ? f : f&.to_sym }
  @value = fetch_value(**args.slice(:passthrough, :default_value).with_defaults(passthrough: ENV.fetch("NVAR_PASSTHROUGH", "").split(",").include?(name.to_s)))
  @defined = true
rescue KeyError
  @value = args[:default_value]
  @defined = false
end

Instance Attribute Details

#definedObject (readonly)

Returns the value of attribute defined.



12
13
14
# File 'lib/nvar/environment_variable.rb', line 12

def defined
  @defined
end

#nameObject (readonly)

Returns the value of attribute name.



12
13
14
# File 'lib/nvar/environment_variable.rb', line 12

def name
  @name
end

#requiredObject (readonly)

Returns the value of attribute required.



12
13
14
# File 'lib/nvar/environment_variable.rb', line 12

def required
  @required
end

#typeObject (readonly)

Returns the value of attribute type.



12
13
14
# File 'lib/nvar/environment_variable.rb', line 12

def type
  @type
end

#valueObject (readonly)

Returns the value of attribute value.



12
13
14
# File 'lib/nvar/environment_variable.rb', line 12

def value
  @value
end

Instance Method Details

#add_to_env_fileObject



40
41
42
43
44
# File 'lib/nvar/environment_variable.rb', line 40

def add_to_env_file
  return if present_in_env_file?

  File.write(Nvar.env_file_path, to_env_assign, mode: "a")
end

#filter_from_vcr_cassettes(config) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/nvar/environment_variable.rb', line 46

def filter_from_vcr_cassettes(config)
  return if @filter_from_requests.nil? || !@filter_from_requests

  config.filter_sensitive_data("<#{name}>") do
    # :nocov:
    case @filter_from_requests
    when :alone_as_basic_auth_password
      Base64.encode64(["", @value].join(":")).delete("\n")
    when true
      @value
    end
    # :nocov:
  end
  config
end

#set?Boolean

Returns:

  • (Boolean)


32
33
34
35
36
37
38
# File 'lib/nvar/environment_variable.rb', line 32

def set?
  return false unless defined

  return value.present? if required

  true
end

#to_constObject



26
27
28
29
30
# File 'lib/nvar/environment_variable.rb', line 26

def to_const
  raise Nvar::EnvironmentVariableNotPresentError, self unless defined

  Object.const_set(name, typecast_value) unless Object.const_defined?(name)
end