class Rototiller::Task::EnvVar
The main EnvVar
type to implement envrironment variable handling
contains its messaging, status, and whether it is required. The rototiller Param using it knows what to do with its value.
@since v0.1.0 @attr [String] default The default value of this env_var to use. if we have a default and
the system ENV does not have a value this implies the env_var is not required. If not default is specified but the parent parameter has a `#name` then that name is used as the default. Used internally by CommandFlag, ignored for standalone EnvVar.
@attr_reader [Boolean] stop Whether the state of the EnvVar
requires the task to stop @attr_reader [Boolean] value The value of the ENV based on specified default and environment state
Constants
- STATUS
Attributes
Public Class Methods
Creates a new instance of EnvVar
, holds information about the ENV in the environment @param [Hash, Array<Hash>] args hash of information about the environment variable @option args [String] :name The environment variable @option args [String] :default The default value for the environment variable @option args [String] :message A message describing the use of this variable for block { |b| … } @yield EnvVar
object with attributes matching method calls supported by EnvVar
@return EnvVar
object
# File lib/rototiller/task/params/env_var.rb, line 34 def initialize(args={}, &block) @parent_name = args[:parent_name] @message ||= args[:parent_message] args.delete(:parent_name) args.delete(:parent_message) block_given? ? (yield self) : send_hash_keys_as_methods_to_self(args) raise(ArgumentError, 'A name must be supplied to add_env') unless @name @env_value_set_by_us = false reset end
Public Instance Methods
Sets the name of the EnvVar
@raise [ArgumentError] if name contains an illegal character for bash environment variable
# File lib/rototiller/task/params/env_var.rb, line 76 def name=(name) name.each_char do |char| message = "You have defined an environment variable with an illegal character: #{char}" raise ArgumentError.new(message) unless char =~ /[a-zA-Z]|\d|_/ end @name = name end
Private Instance Methods
@private
# File lib/rototiller/task/params/env_var.rb, line 110 def env_status return STATUS[:nodefault_noexist] if !@default && @env_value_set_by_us return STATUS[:nodefault_exist] if !@default && !@env_value_set_by_us return STATUS[:default_noexist] if @default && @env_value_set_by_us return STATUS[:default_exist] if @default && !@env_value_set_by_us end
@private
# File lib/rototiller/task/params/env_var.rb, line 104 def env_value_provided_by_user? # its possible that name could not be set (ENV.key?(@name) if @name) ? true : false end
@private
# File lib/rototiller/task/params/env_var.rb, line 87 def reset # if no default given, use parent param's name @default ||= @parent_name (env_value_provided_by_user? || @default) ? @stop = false : @stop = true if @name @value = ENV[@name] || @default unless env_value_provided_by_user? ENV[@name] = @value @env_value_set_by_us = true end else @value = @default end end