class Linux::Lxc::File

Attributes

dir[R]
file[R]
index[R]
lines[R]
real_fname[RW]

Public Class Methods

new(file, dir, index) click to toggle source
# File lib/linux/lxc/file.rb, line 9
def initialize(file, dir, index)
  self.file = file
  @dir = dir
  @lines = Lines.new
  @index = index
end

Public Instance Methods

add(key, value = nil) click to toggle source
# File lib/linux/lxc/file.rb, line 45
def add(key, value = nil)
  key = key.strip
  value = value.strip if value && value.instance_of?(String)
  line = Line.new(self, key, value)
  add_to_index(line)
end
add_to_index(line) click to toggle source
# File lib/linux/lxc/file.rb, line 52
def add_to_index(line)
  key_to_path(line.key) do |path|
    @index.add_line(path, line)
  end
end
all_lines(&block) click to toggle source
# File lib/linux/lxc/file.rb, line 58
def all_lines(&block)
  @lines.each do |line|
    block.call(line)
    line.value.all_lines(&block) if line.value.respond_to?(:all_lines)
  end
end
entries() click to toggle source

def files

ret = [Line.new(nil, 'lxc.include', self)]
all_lines do |line|
  line.value.instance_of?(File) && (ret << line)
end
ret

end

# File lib/linux/lxc/file.rb, line 73
def entries
  {file => self}
end
file=(a) click to toggle source

file is more important than real_fname

# File lib/linux/lxc/file.rb, line 17
def file=(a)
  @file = a
  @real_fname = a
end
get(key) click to toggle source
# File lib/linux/lxc/file.rb, line 22
def get(key)
  @index.get_key(key)
end
key_to_path(key, &block) click to toggle source
# File lib/linux/lxc/file.rb, line 26
def key_to_path(key, &block)
  path = ''
  dot = ''
  key.split('.').each do |element|
    path += dot + element
    dot = '.'
    # puts ">>>>#{path}"
    block.call(path)
  end
end
parse() click to toggle source
# File lib/linux/lxc/file.rb, line 95
def parse
  IO.read(file).lines.each do |line|
    line = line.chop
    if line.match(/^\s*$/)
      self.add(line, nil)
    elsif line.match(/^\s*#.*$/)
      self.add('#', line)
    else
      match = line.match(/^\s*([a-z0-9\-_\.]+)\s*=\s*(.*)\s*$/)
      throw "illegal line in #{@file}:#{@lines.length}" unless match
      if match[1] == 'lxc.include'
        self.add(match[1], Lxc.parse(match[2], index))
      else
        self.add(match[1], match[2])
      end
    end
  end

  self
end
remove_from_index(line) click to toggle source
# File lib/linux/lxc/file.rb, line 37
def remove_from_index(line)
  key_to_path(line.key) do |path|
    lines = @index.get_key(path)
    lines.remove(line)
    @index.delete_key(path)
  end
end
to_s() click to toggle source
# File lib/linux/lxc/file.rb, line 91
def to_s
  @file
end
write() click to toggle source
# File lib/linux/lxc/file.rb, line 77
def write
  FileUtils.mkdir_p ::File.dirname(real_fname)
  ::File.open(real_fname, 'w') do |f|
    @lines.each do |line|
      if line.key == 'lxc.include'
        line.value.write
        f.write(line.to_s + "\n")
      else
        f.write(line.to_s + "\n")
      end
    end
  end
end