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.



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

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.



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

def dir
  @dir
end

#fileObject

Returns the value of attribute file.



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

def file
  @file
end

#indexObject (readonly)

Returns the value of attribute index.



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

def index
  @index
end

#linesObject (readonly)

Returns the value of attribute lines.



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

def lines
  @lines
end

#real_fnameObject

Returns the value of attribute real_fname.



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

def real_fname
  @real_fname
end

Instance Method Details

#add(key, value = nil) ⇒ Object



45
46
47
48
49
50
# 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) ⇒ Object



52
53
54
55
56
# 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) ⇒ Object



58
59
60
61
62
63
# 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

#entriesObject

def files

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

end



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

def entries
  {file => self}
end

#get(key) ⇒ Object



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

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

#key_to_path(key, &block) ⇒ Object



26
27
28
29
30
31
32
33
34
35
# 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

#parseObject



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# 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) ⇒ Object



37
38
39
40
41
42
43
# 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_sObject



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

def to_s
  @file
end

#writeObject



77
78
79
80
81
82
83
84
85
86
87
88
89
# 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