Class: Nvar::EnvironmentVariable
- Inherits:
-
Object
- Object
- Nvar::EnvironmentVariable
- Defined in:
- lib/nvar/environment_variable.rb
Overview
Wrapper for loading environment variables, used across relevant Rake tasks
Instance Attribute Summary collapse
-
#defined ⇒ Object
readonly
Returns the value of attribute defined.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#required ⇒ Object
readonly
Returns the value of attribute required.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
-
#value ⇒ Object
readonly
Returns the value of attribute value.
Instance Method Summary collapse
- #add_to_env_file ⇒ Object
- #filter_from_vcr_cassettes(config) ⇒ Object
-
#initialize(name:, type: 'String', filter_from_requests: nil, **args) ⇒ EnvironmentVariable
constructor
A new instance of EnvironmentVariable.
- #set? ⇒ Boolean
- #to_const ⇒ Object
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)) @defined = true rescue KeyError @value = args[:default_value] @defined = false end |
Instance Attribute Details
#defined ⇒ Object (readonly)
Returns the value of attribute defined.
12 13 14 |
# File 'lib/nvar/environment_variable.rb', line 12 def defined @defined end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
12 13 14 |
# File 'lib/nvar/environment_variable.rb', line 12 def name @name end |
#required ⇒ Object (readonly)
Returns the value of attribute required.
12 13 14 |
# File 'lib/nvar/environment_variable.rb', line 12 def required @required end |
#type ⇒ Object (readonly)
Returns the value of attribute type.
12 13 14 |
# File 'lib/nvar/environment_variable.rb', line 12 def type @type end |
#value ⇒ Object (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_file ⇒ Object
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
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_const ⇒ Object
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) end |