Class: Linux::Lxc

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

Defined Under Namespace

Classes: Line, Lines

Constant Summary collapse

VERSION =
"0.0.1"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file, index = {}) ⇒ Lxc

Returns a new instance of Lxc.



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

def initialize(file, index = {})
  @file = file
  @lines = Lines.new
  @index = index
end

Instance Attribute Details

#fileObject

Returns the value of attribute file.



28
29
30
# File 'lib/linux/lxc.rb', line 28

def file
  @file
end

#indexObject (readonly)

Returns the value of attribute index.



27
28
29
# File 'lib/linux/lxc.rb', line 27

def index
  @index
end

#linesObject (readonly)

Returns the value of attribute lines.



27
28
29
# File 'lib/linux/lxc.rb', line 27

def lines
  @lines
end

Class Method Details

.parse(file, index = {}) ⇒ Object



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/linux/lxc.rb', line 99

def self.parse(file, index = {})
  lxc = Lxc.new(file, index)
  IO.read(file).lines.each do |line|
    line = line.chop
    if line.match(/^\s*$/) or line.match(/^\s*#.*$/)
        lxc.add(line, nil)
    else
      match = line.match(/^\s*([a-z\.]+)\s*=\s*(.*)\s*$/)
      throw "illegal line in #{@file}:#{@lines.length}" unless match
      if match[1] == 'lxc.include'
        lxc.add(match[1], parse(match[2], index))
      else
        lxc.add(match[1], match[2])
      end
    end
  end
  lxc
end

Instance Method Details

#add(key, value = nil) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/linux/lxc.rb', line 68

def add(key, value = nil)
  key = key.strip
  if value and value.instance_of?(String)
    value = value.strip
  end
  line = Line.new(self, key, value)
  path = ""
  dot = ""
  key.split('.').each do |element|
    path += dot + element
    dot = "."
    @index[path] ||= Lines.new
    @index[path].add(line)
  end
end

#get(key) ⇒ Object



64
65
66
# File 'lib/linux/lxc.rb', line 64

def get(key)
  @index[key]
end

#to_sObject



95
96
97
# File 'lib/linux/lxc.rb', line 95

def to_s
  @file
end

#writeObject



84
85
86
87
88
89
90
91
92
93
# File 'lib/linux/lxc.rb', line 84

def write
  File.open(file, 'w') do |f|
    @lines.each do |line|
      if line.key == "lxc.include"
        line.value.write
      end
      f.write(line.to_s + "\n")
    end
  end
end