Class: Resat::Variables

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeVariables

Initialize instance



16
17
18
19
20
# File 'lib/variables.rb', line 16

def initialize
  @@exported = Hash.new unless defined? @@exported
  @vars = @@exported.dup
  @marked_for_save = []
end

Instance Attribute Details

#exportedObject (readonly)

Returns the value of attribute exported.



13
14
15
# File 'lib/variables.rb', line 13

def exported
  @exported
end

#marked_for_saveObject (readonly)

Returns the value of attribute marked_for_save.



13
14
15
# File 'lib/variables.rb', line 13

def marked_for_save
  @marked_for_save
end

#varsObject (readonly)

Returns the value of attribute vars.



13
14
15
# File 'lib/variables.rb', line 13

def vars
  @vars
end

Instance Method Details

#[](key) ⇒ Object



39
40
41
# File 'lib/variables.rb', line 39

def [](key)
  vars[key]
end

#[]=(key, value) ⇒ Object



43
44
45
# File 'lib/variables.rb', line 43

def []=(key, value)
  vars[key] = value
end

#allObject



55
56
57
# File 'lib/variables.rb', line 55

def all
  vars.sort
end

#empty?Boolean

Returns:

  • (Boolean)


51
52
53
# File 'lib/variables.rb', line 51

def empty?
  vars.empty?
end

#export(key) ⇒ Object

Exported values will be kept even after new instance is initialized



92
93
94
# File 'lib/variables.rb', line 92

def export(key)
  @@exported[key] = @vars[key]
end

#include?(key) ⇒ Boolean

Returns:

  • (Boolean)


47
48
49
# File 'lib/variables.rb', line 47

def include?(key)
  vars.include?(key)
end

#load(file, schemasdir) ⇒ Object

Load variables from given file



60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/variables.rb', line 60

def load(file, schemasdir)
  schemafile = File.join(schemasdir, 'variables.yaml')
  schema = Kwalify::Yaml.load_file(schemafile)
  validator = Kwalify::Validator.new(schema)
  parser = Kwalify::Yaml::Parser.new(validator)
  serialized_vars = parser.parse_file(file)
  parser.errors.push(Kwalify::ValidationError.new("No variables defined")) unless serialized_vars
  if parser.errors.empty?
    serialized_vars.each { |v| vars[v['name']] = v['value'] }
  else
    Log.warn("Error loading variables from '#{file}': #{KwalifyHelper.parser_error(parser)}")
  end
end

#mark_for_save(key) ⇒ Object



87
88
89
# File 'lib/variables.rb', line 87

def mark_for_save(key)
  @marked_for_save << key
end

#save(file) ⇒ Object

Save variables to given file



75
76
77
78
79
80
81
82
83
84
85
# File 'lib/variables.rb', line 75

def save(file)
  serialized_vars = []
  vars.each do |k, v|
    if marked_for_save.include?(k)
      serialized_vars << { 'name' => k, 'value' => v }
    end
  end
  File.open(file, 'w') do |out|
    YAML.dump(serialized_vars, out)
  end
end

#substitute!(raw) ⇒ Object

Replace occurrences of environment variables in raw with their value



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/variables.rb', line 23

def substitute!(raw)
  if raw.kind_of?(String)
    scans = Array.new
    raw.scan(/[^\$]*\$(\w+)+/) { |scan| scans << scan }
    scans.each do |scan|
      scan.each do |var|
        raw.gsub!('$' + var, @vars[var]) if @vars.include?(var)
      end
    end
  elsif raw.kind_of?(Array)
    raw.each { |i| substitute!(i) }
  elsif raw.kind_of?(Hash)
    raw.each { |k, v| substitute!(v) }
  end
end