Class: Recap::Support::Environment

Inherits:
Object
  • Object
show all
Defined in:
lib/recap/support/environment.rb

Overview

This class is used to manipulate environment variables on the remote server. You should not need to use it directly; you are probably looking for the [env](../tasks/env.html) tasks instead.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(variables = {}) ⇒ Environment

Returns a new instance of Environment.



8
9
10
# File 'lib/recap/support/environment.rb', line 8

def initialize(variables = {})
  @variables = variables
end

Class Method Details

.from_string(string) ⇒ Object



53
54
55
56
57
58
# File 'lib/recap/support/environment.rb', line 53

def from_string(string)
  string.split(/[\n\r]/).inject(new) do |env, line|
    env.set_string(line)
    env
  end
end

Instance Method Details

#each(&block) ⇒ Object



38
39
40
# File 'lib/recap/support/environment.rb', line 38

def each(&block)
  @variables.sort.each(&block)
end

#empty?Boolean

Returns:

  • (Boolean)


30
31
32
# File 'lib/recap/support/environment.rb', line 30

def empty?
  @variables.empty?
end

#get(name) ⇒ Object



12
13
14
# File 'lib/recap/support/environment.rb', line 12

def get(name)
  @variables[name]
end

#include?(key) ⇒ Boolean

Returns:

  • (Boolean)


42
43
44
# File 'lib/recap/support/environment.rb', line 42

def include?(key)
  @variables.include?(key)
end

#merge(hash) ⇒ Object



34
35
36
# File 'lib/recap/support/environment.rb', line 34

def merge(hash)
  hash.each {|k, v| set(k, v)}
end

#set(name, value) ⇒ Object



16
17
18
19
20
21
22
# File 'lib/recap/support/environment.rb', line 16

def set(name, value)
  if value.nil? || value.empty?
    @variables.delete(name)
  else
    @variables[name] = value
  end
end

#set_string(string) ⇒ Object



24
25
26
27
28
# File 'lib/recap/support/environment.rb', line 24

def set_string(string)
  if string =~ /\A([A-Za-z0-9_]+)=(.*)\z/
    set $1, $2
  end
end

#to_sObject



46
47
48
49
50
# File 'lib/recap/support/environment.rb', line 46

def to_s
  @variables.keys.sort.map do |key|
    key + "=" + @variables[key] + "\n" if @variables[key]
  end.compact.join
end