Class: Puppet_X::LVM::Output

Inherits:
Object
  • Object
show all
Defined in:
lib/puppet_x/lvm/output.rb

Class Method Summary collapse

Class Method Details

.parse(key, columns, data) ⇒ Object

Parses the results of LVMs commands. This does not handle when columns have no data and therefore these columns should be avoided. It returns the data with the prefix removed i.e. “lv_free” would be come “free” this however doesn’t descriminate and will turn something like “foo_bar” into “bar”



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/puppet_x/lvm/output.rb', line 9

def self.parse(key,columns,data)
  results = {}
    
  # Remove prefixes
  columns = remove_prefixes(columns)
  key     = remove_prefix(key)

  data.split("\n").each do |line|
    parsed_line = line.gsub(/\s+/, ' ').strip.split(' ')
    values      = Hash[columns.zip(parsed_line)]
    current_key = values[key]
    values.delete(key)
    results[current_key] = values
  end
    
  results
end

.remove_prefix(item) ⇒ Object



33
34
35
# File 'lib/puppet_x/lvm/output.rb', line 33

def self.remove_prefix(item)
  item.gsub(/^[A-Za-z]+_/,'')
end

.remove_prefixes(array) ⇒ Object



27
28
29
30
31
# File 'lib/puppet_x/lvm/output.rb', line 27

def self.remove_prefixes(array)
  array.map do |item|
    remove_prefix(item)
  end
end