Class: VpsAdminOS::Converter::Vz6::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/vpsadminos-converter/vz6/config.rb

Overview

Represents a config file for OpenVZ Legacy container

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ctid, io) ⇒ Config

Returns a new instance of Config.



13
14
15
16
17
18
# File 'lib/vpsadminos-converter/vz6/config.rb', line 13

def initialize(ctid, io)
  @ctid = ctid
  @items = {}

  parse(io)
end

Instance Attribute Details

#ctidObject (readonly)

Returns the value of attribute ctid.



11
12
13
# File 'lib/vpsadminos-converter/vz6/config.rb', line 11

def ctid
  @ctid
end

Class Method Details

.parse(ctid, path) ⇒ Object



4
5
6
7
8
9
# File 'lib/vpsadminos-converter/vz6/config.rb', line 4

def self.parse(ctid, path)
  f = File.open(path, 'r')
  c = new(ctid, f)
  f.close
  c
end

Instance Method Details

#[](k) ⇒ Vz6::ConfigItem

Parameters:

  • k (String)

    config key

Returns:



27
28
29
# File 'lib/vpsadminos-converter/vz6/config.rb', line 27

def [](k)
  @items[k]
end

#consume(k) ⇒ Object



31
32
33
34
35
36
37
# File 'lib/vpsadminos-converter/vz6/config.rb', line 31

def consume(k)
  it = @items[k]
  return unless it

  it.consume
  it.value
end

#eachObject

yield [Vz6::ConfigItem]



21
22
23
# File 'lib/vpsadminos-converter/vz6/config.rb', line 21

def each(&)
  @items.each_value(&)
end

#parse(io) ⇒ Object (protected)



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/vpsadminos-converter/vz6/config.rb', line 41

def parse(io)
  io.each_line do |line|
    case line
    when /^\s*#/, /^\s*$/
      next

    when /^([A-Z_]+)="([^"]+)"/, /^([A-Z_]+)=([^\s]+)/
      it = Vz6::ConfigItem.new(ctid, ::Regexp.last_match(1), ::Regexp.last_match(2))
      @items[it.key] = it

    else
      warn "Unknown line '#{line}'"
    end
  end
end