Class: Linux::Lxc::File

Inherits:
Object
  • Object
show all
Defined in:
lib/linux/lxc/file.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file, dir, index) ⇒ File

Returns a new instance of File.



8
9
10
11
12
13
# File 'lib/linux/lxc/file.rb', line 8

def initialize(file, dir, index)
  self.file = file
  @dir = dir
  @lines = Lines.new
  @index = index
end

Instance Attribute Details

#dirObject (readonly)

Returns the value of attribute dir.



5
6
7
# File 'lib/linux/lxc/file.rb', line 5

def dir
  @dir
end

#fileObject

Returns the value of attribute file.



5
6
7
# File 'lib/linux/lxc/file.rb', line 5

def file
  @file
end

#indexObject (readonly)

Returns the value of attribute index.



5
6
7
# File 'lib/linux/lxc/file.rb', line 5

def index
  @index
end

#linesObject (readonly)

Returns the value of attribute lines.



5
6
7
# File 'lib/linux/lxc/file.rb', line 5

def lines
  @lines
end

#real_fnameObject

Returns the value of attribute real_fname.



6
7
8
# File 'lib/linux/lxc/file.rb', line 6

def real_fname
  @real_fname
end

Instance Method Details

#add(key, value = nil) ⇒ Object



44
45
46
47
48
49
# File 'lib/linux/lxc/file.rb', line 44

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) ⇒ Object



51
52
53
54
55
# File 'lib/linux/lxc/file.rb', line 51

def add_to_index(line)
  key_to_path(line.key) do |path|
    @index.add_line(path, line)
  end
end

#all_lines(&block) ⇒ Object



57
58
59
60
61
62
# File 'lib/linux/lxc/file.rb', line 57

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

#entriesObject

def files

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

end



72
73
74
# File 'lib/linux/lxc/file.rb', line 72

def entries
  {file => self}
end

#get(key) ⇒ Object



21
22
23
# File 'lib/linux/lxc/file.rb', line 21

def get(key)
  @index.get_key(key)
end

#key_to_path(key, &block) ⇒ Object



25
26
27
28
29
30
31
32
33
34
# File 'lib/linux/lxc/file.rb', line 25

def key_to_path(key, &block)
  path = ''
  dot = ''
  key.split('.').each do |element|
    path += dot + element
    dot = '.'
    # puts ">>>>#{path}"
    block.call(path)
  end
end

#parseObject



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/linux/lxc/file.rb', line 94

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-z_\.]+)\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) ⇒ Object



36
37
38
39
40
41
42
# File 'lib/linux/lxc/file.rb', line 36

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_sObject



90
91
92
# File 'lib/linux/lxc/file.rb', line 90

def to_s
  @file
end

#writeObject



76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/linux/lxc/file.rb', line 76

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