Class: EnvVar

Inherits:
RumoduleCommon show all
Defined in:
bin/rumodule

Overview

Environment variable that has been read from “unix” env.

Constant Summary

Constant Summary

Constants inherited from RumoduleCommon

RumoduleCommon::SHELL

Instance Attribute Summary (collapse)

Class Method Summary (collapse)

Instance Method Summary (collapse)

Methods included from RumoduleMod

#home

Constructor Details

- (EnvVar) initialize(name, value = nil)

Initialize name, dirty and unset. If unset is true, then “unset” command is output to shell.

Parameters:

  • name (String)

    Env var name.

  • value (String) (defaults to: nil)

    Value for Env var.



317
318
319
320
321
322
323
324
325
326
# File 'bin/rumodule', line 317

def initialize( name, value = nil )
    @name = name
    @dirty = false
    @unset = false
    if value
        @value = value
    else
        @value = ENV[name].split(':')
    end
end

Instance Attribute Details

- (Object) dirty (readonly)

Dirty flag indicates that the env has to be updated.



297
298
299
# File 'bin/rumodule', line 297

def dirty
  @dirty
end

- (Object) name (readonly)

Variable name.



294
295
296
# File 'bin/rumodule', line 294

def name
  @name
end

- (Object) value (readonly)

EnvVar value.



300
301
302
# File 'bin/rumodule', line 300

def value
  @value
end

Class Method Details

+ (String) exist?(name)

Check for “unix” var existance.

Parameters:

  • name (String)

    Env var name to check.

Returns:

  • (String)

    Value or nil.



307
308
309
# File 'bin/rumodule', line 307

def EnvVar.exist?( name )
    ENV[name]
end

Instance Method Details

- (Object) append_path(value)

Place new value at end of the path.

Parameters:

  • value (String)

    Value for Env var.



360
361
362
363
364
# File 'bin/rumodule', line 360

def append_path( value )
    @dirty = true
    @unset = false
    @value.push( value )
end

- (Object) delete_index(idx)

Remove value at index.

Parameters:

  • idx (Integer)

    Value index used to remove from Env var.



380
381
382
383
384
# File 'bin/rumodule', line 380

def delete_index( idx )
    @dirty = true
    @unset = false
    @value.delete_at( idx )
end

- (String) export

Format EnvVar for shell. Output depends on whether the EnvVar has been “unset” or not.

Returns:

  • (String)

    Shell's eval command arguments.



399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
# File 'bin/rumodule', line 399

def export
    if @unset
        case SHELL
        when 'zsh', 'sh', 'bash'; "unset #{name}"
        when 'fish'; "set -e #{name}"
        else raise "Unknown shell: \"#{SHELL}\""
        end
   else
        case SHELL
        when 'zsh', 'sh', 'bash'; "export #{name}=#{@value.join(':')}"
        when 'fish';
            if name == 'PATH'
                "set -gx #{name} #{@value.join(' ')}"
            else
                "set -gx #{name} #{@value.join(':')}"
            end
        else raise "Unknown shell: \"#{SHELL}\""
        end
    end
end

- (Integer) length

Entries in Env var.

Returns:

  • (Integer)

    Length.



390
391
392
# File 'bin/rumodule', line 390

def length
    @value.length
end

- (Object) prepend_path(value)

Place new value infront of the path.

Parameters:

  • value (String)

    Value for Env var.



350
351
352
353
354
# File 'bin/rumodule', line 350

def prepend_path( value )
    @dirty = true
    @unset = false
    @value.unshift( value )
end

- (Object) remove_path(value)

Remove value from the path.

Parameters:

  • value (String)

    Value to remove from Env var.



370
371
372
373
374
# File 'bin/rumodule', line 370

def remove_path( value )
    @dirty = true
    @unset = false
    @value.delete( value )
end

- (Object) set(value)

Set variable to a value.

Parameters:

  • value (String)

    Value for Env var.



332
333
334
335
336
# File 'bin/rumodule', line 332

def set( value )
    @dirty = true
    @unset = false
    @value = [ value ]
end

- (Object) unset

Unset variable from env.



340
341
342
343
344
# File 'bin/rumodule', line 340

def unset
    @dirty = true
    @unset = true
    @value = []
end