Class: Linux::Lxc
- Inherits:
-
Object
- Object
- Linux::Lxc
- Defined in:
- lib/linux/lxc.rb,
lib/linux/lxc/version.rb
Defined Under Namespace
Constant Summary collapse
- VERSION =
"0.0.4"
Instance Attribute Summary collapse
-
#file ⇒ Object
Returns the value of attribute file.
-
#index ⇒ Object
readonly
Returns the value of attribute index.
-
#lines ⇒ Object
readonly
Returns the value of attribute lines.
-
#real_fname ⇒ Object
Returns the value of attribute real_fname.
Class Method Summary collapse
Instance Method Summary collapse
- #add(key, value = nil) ⇒ Object
- #add_to_index(line) ⇒ Object
- #all_lines(&block) ⇒ Object
- #files ⇒ Object
- #get(key) ⇒ Object
-
#initialize(file, index = {}) ⇒ Lxc
constructor
A new instance of Lxc.
- #key_to_path(key, &block) ⇒ Object
- #remove_from_index(line) ⇒ Object
- #to_s ⇒ Object
- #write ⇒ Object
Constructor Details
Instance Attribute Details
#file ⇒ Object
Returns the value of attribute file.
76 77 78 |
# File 'lib/linux/lxc.rb', line 76 def file @file end |
#index ⇒ Object (readonly)
Returns the value of attribute index.
76 77 78 |
# File 'lib/linux/lxc.rb', line 76 def index @index end |
#lines ⇒ Object (readonly)
Returns the value of attribute lines.
76 77 78 |
# File 'lib/linux/lxc.rb', line 76 def lines @lines end |
#real_fname ⇒ Object
Returns the value of attribute real_fname.
77 78 79 |
# File 'lib/linux/lxc.rb', line 77 def real_fname @real_fname end |
Class Method Details
.parse(file, index = {}) ⇒ Object
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 |
# File 'lib/linux/lxc.rb', line 159 def self.parse(file, index = {}) lxc = Lxc.new(file, index) IO.read(file).lines.each do |line| line = line.chop if line.match(/^\s*$/) lxc.add(line, nil) elsif line.match(/^\s*#.*$/) lxc.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' 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
113 114 115 116 117 118 119 120 |
# File 'lib/linux/lxc.rb', line 113 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) add_to_index(line) end |
#add_to_index(line) ⇒ Object
122 123 124 125 126 127 |
# File 'lib/linux/lxc.rb', line 122 def add_to_index(line) key_to_path(line.key) do |path| @index[path] ||= Lines.new @index[path].add(line) end end |
#all_lines(&block) ⇒ Object
129 130 131 132 133 134 135 136 |
# File 'lib/linux/lxc.rb', line 129 def all_lines(&block) @lines.each do |line| block.call(line) if line.value.instance_of?(Lxc) line.value.all_lines(&block) end end end |
#files ⇒ Object
138 139 140 141 142 |
# File 'lib/linux/lxc.rb', line 138 def files ret = [Line.new(nil, "lxc.include", self)] all_lines {|line| line.value.instance_of?(Lxc) && (ret << line) } ret end |
#get(key) ⇒ Object
91 92 93 |
# File 'lib/linux/lxc.rb', line 91 def get(key) @index[key] end |
#key_to_path(key, &block) ⇒ Object
95 96 97 98 99 100 101 102 103 104 |
# File 'lib/linux/lxc.rb', line 95 def key_to_path(key, &block) path = "" dot = "" key.split('.').each do |element| path += dot + element dot = "." #puts ">>>>#{path}" block.call(path) end end |
#remove_from_index(line) ⇒ Object
106 107 108 109 110 111 |
# File 'lib/linux/lxc.rb', line 106 def remove_from_index(line) key_to_path(line.key) do |path| get(path).remove(line) get(path).empty? && @index.delete(path) end end |
#to_s ⇒ Object
155 156 157 |
# File 'lib/linux/lxc.rb', line 155 def to_s @file end |
#write ⇒ Object
144 145 146 147 148 149 150 151 152 153 |
# File 'lib/linux/lxc.rb', line 144 def write File.open(real_fname, '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 |