Class: Ifconfig

Inherits:
Object
  • Object
show all
Defined in:
lib/Unix/Ifconfig.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(string = '') ⇒ Ifconfig

Returns a new instance of Ifconfig.



9
10
11
12
13
# File 'lib/Unix/Ifconfig.rb', line 9

def initialize(string = '')
  @interfaces_raw = []
  @interfaces = {}
  parse(string) unless string.empty?
end

Instance Attribute Details

#interfacesObject (readonly)

Returns the value of attribute interfaces.



7
8
9
# File 'lib/Unix/Ifconfig.rb', line 7

def interfaces
  @interfaces
end

#interfaces_rawObject (readonly)

Returns the value of attribute interfaces_raw.



6
7
8
# File 'lib/Unix/Ifconfig.rb', line 6

def interfaces_raw
  @interfaces_raw
end

#string_rawObject (readonly)

Returns the value of attribute string_raw.



5
6
7
# File 'lib/Unix/Ifconfig.rb', line 5

def string_raw
  @string_raw
end

Instance Method Details

#parse(string) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/Unix/Ifconfig.rb', line 15

def parse(string)
  @string_raw = string

  interface_raw = ''
  string.each_line do |line|
    next if (line =~ /^\s*$/)
    if (line =~ /^\w+\d+:/)
      @interfaces_raw.push(interface_raw) unless interface_raw.empty?
      interface_raw = line
    else
      interface_raw += line + "\n"
    end
  end
  @interfaces_raw.push(interface_raw) unless interface_raw.empty?

  @interfaces_raw.each do |string|
    interface = Ifconfig_interface.new(string)
    @interfaces[interface.name] = interface
  end
end