Class: Env::EnvVar

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

Overview

An auxiliary class that represents environment variable paths as an array. User is supposed to interact with this class by simply using Env or storing it in a variable.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, value) ⇒ EnvVar

Returns a new instance of EnvVar.

[View source]

131
132
133
134
135
# File 'lib/GGEnv.rb', line 131

def initialize(name, value)
  @owner = true
  @name = name
  replace(value) if value
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.


129
130
131
# File 'lib/GGEnv.rb', line 129

def name
  @name
end

Instance Method Details

#&(b) ⇒ Object

[View source]

192
193
194
195
# File 'lib/GGEnv.rb', line 192

def &(b)
  @owner = false
  EnvVar.new( @name, super )
end

#+(b) ⇒ Object

[View source]

206
207
208
209
210
# File 'lib/GGEnv.rb', line 206

def +(b)
  c = self.dup
  @owner = false
  c << b
end

#-(b) ⇒ Object

[View source]

197
198
199
200
201
202
203
204
# File 'lib/GGEnv.rb', line 197

def -(b)
  @owner = false
  if b.kind_of?(Array)
	EnvVar.new( @name, super )
  else
	EnvVar.new( @name, super( [b] ) )
  end
end

#<<(*b) ⇒ Object

[View source]

212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
# File 'lib/GGEnv.rb', line 212

def <<(*b)
  b.each { |p|
	if p.kind_of?(Array)
	  p.each { |x|
 s = unify_path( x.to_s )
 next if not verify(s)
 super(s)
	  }
	  uniq!
	else
	  s = unify_path( p.to_s )
	  next if self.include?(s)
	  next if not verify(s)
	  super(s)
	end
  }
  to_env
  return self
end

#check_directoriesObject

[View source]

158
159
160
161
# File 'lib/GGEnv.rb', line 158

def check_directories
  delete_if { |p| not File.directory?(p) }
  to_env
end

#clearObject

[View source]

163
164
165
166
167
# File 'lib/GGEnv.rb', line 163

def clear
  super
  to_env
  return self
end

#delete(a, &block) ⇒ Object

[View source]

169
170
171
172
173
# File 'lib/GGEnv.rb', line 169

def delete(a, &block)
  super
  to_env
  return self
end

#delete_at(a) ⇒ Object

[View source]

175
176
177
178
179
# File 'lib/GGEnv.rb', line 175

def delete_at(a)
  super
  to_env
  return self
end

#delete_if(&block) ⇒ Object

[View source]

181
182
183
184
185
# File 'lib/GGEnv.rb', line 181

def delete_if(&block)
  super
  to_env
  return self
end

#from_envObject

Replace value of variable from environment

[View source]

283
284
285
286
287
# File 'lib/GGEnv.rb', line 283

def from_env
  return if not @name
  @owner = true
  replace( ENV[@name] )
end

#inspectObject

[View source]

267
268
269
# File 'lib/GGEnv.rb', line 267

def inspect
  to_s
end

#push(*b) ⇒ Object

[View source]

232
233
234
# File 'lib/GGEnv.rb', line 232

def push(*b)
  self.<<(*b)
end

#remove(b) ⇒ Object

[View source]

236
237
238
239
# File 'lib/GGEnv.rb', line 236

def remove(b)
  self.delete_if { |p| p =~ /#{b}/ }
  to_env
end

#replace(value) ⇒ Object

[View source]

137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/GGEnv.rb', line 137

def replace(value)
  if value.kind_of?(Array)
	super
  elsif value.kind_of?(String)
	super( value.split(SEP) )
  elsif value.kind_of?(NilClass)
	clear; return self
  else
	raise Error, 
	  "Cannot assign #{value} to #{@name}.  Not an array or string."
  end
  uniq!
  map! { |p| unify_path(p) }
  if Env.check_directories?
	check_directories
  else
	to_env
  end
  return self
end

#to_envObject

Send value of variable to environment

[View source]

274
275
276
277
278
# File 'lib/GGEnv.rb', line 274

def to_env
  return if not @name
  from_env if not @owner
  ENV[@name] = to_s
end

#to_sObject

[View source]

262
263
264
265
# File 'lib/GGEnv.rb', line 262

def to_s
  from_env if not @owner
  self.join(SEP)
end

#unshift(*b) ⇒ Object

[View source]

241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
# File 'lib/GGEnv.rb', line 241

def unshift(*b)
  b.reverse_each { |p|
	if p.kind_of?(Array)
	  a = []
	  p.reverse_each { |x|
 s = unify_path( x.to_s )
 next if not verify(s)
 a << s
	  }
	  super(*a)
	  uniq!
	else
	  s = unify_path( p.to_s )
	  next if self.include?(s)
	  next if not verify(s)
	  super(s)
	end
  }
  to_env
end

#|(b) ⇒ Object

[View source]

187
188
189
190
# File 'lib/GGEnv.rb', line 187

def |(b)
  @owner = false
  EnvVar.new( @name, super )
end