Class: Puppet_X::LVM::Output

Inherits:
Object
  • Object
show all
Defined in:
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”



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

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      = columns.zip(parsed_line).to_h
    current_key = values[key]
    values.delete(key)
    results[current_key] = values
  end

  results
end

.remove_prefix(item) ⇒ Object



36
37
38
# File 'lib/puppet_x/lvm/output.rb', line 36

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

.remove_prefixes(array) ⇒ Object



30
31
32
33
34
# File 'lib/puppet_x/lvm/output.rb', line 30

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