This repository has been archived by the owner on Jun 28, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
var.rb
76 lines (65 loc) · 1.44 KB
/
var.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# Lazy environment variable expansion
# stores the value in workdir for later runs
class VAR
@@defaults = {}
class << self
def [](name)
if ENV.key? name
debug "#{name} == #{ENV[name]}"
return ENV[name]
end
value = self.read name
unless value.nil?
debug "#{name} <= #{value}"
ENV[name] = value
return value
end
if @@defaults.key? name
value = @@defaults[name].call
debug "#{name} ?= #{value}"
ENV[name] = value
else
debug "#{name} (not set)"
nil
end
end
def []=(name, value)
debug "#{name} = #{value}"
ENV[name] = value
self.write name, value
end
def store(name)
self[name] &&= self[name]
end
def key?(name)
ENV.key?(name) || @@defaults.key?(name) || File.exists?(self.fn(name))
end
def delete(name)
ENV.delete name
File.unlink self.fn(name) if File.exists? self.fn(name)
end
def default(name, &block)
raise "block missing" unless block_given?
@@defaults[name] = block
end
def delete_default(name)
@@defaults.delete name
end
def load_all
Dir[workdir '*.variable'].each { |fn| VAR[File.basename fn, '.variable'] }
end
def fn(name)
workdir "#{name}.variable"
end
def read(name)
if File.exists? self.fn(name)
File.read self.fn(name)
else
nil
end
end
def write(name, value)
File.open(self.fn(name), 'w') { |file| file.write value.to_s }
end
end
end