Class: Puppet_X::LVM::Output

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

Overview

Work with LVM Output

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”



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

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(%r{\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



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

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

.remove_prefixes(array) ⇒ Object



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

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