Class: PConfig

Inherits:
Object
  • Object
show all
Defined in:
lib/resources/parse_config.rb

Overview

Usage example:

audit = command('/sbin/auditctl -l').stdout
options = {
  assignment_re: /^\s*([^:]*?)\s*:\s*(.*?)\s*$/,
  multiple_values: true
}
describe parse_config(audit, options ) do

Direct Known Subclasses

PConfigFile

Instance Method Summary collapse

Constructor Details

#initialize(content = nil, useropts = nil) ⇒ PConfig

Returns a new instance of PConfig.



27
28
29
30
31
32
33
34
35
# File 'lib/resources/parse_config.rb', line 27

def initialize(content = nil, useropts = nil)
  @opts = {}
  @opts = useropts.dup unless useropts.nil?
  @files_contents = {}
  @params = nil

  @content = content
  read_content if @content.nil?
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name) ⇒ Object



37
38
39
40
# File 'lib/resources/parse_config.rb', line 37

def method_missing(name)
  @params || read_content
  @params[name.to_s]
end

Instance Method Details

#parse_file(conf_path) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/resources/parse_config.rb', line 42

def parse_file(conf_path)
  @conf_path = conf_path

  # read the file
  if !inspec.file(conf_path).file?
    return skip_resource "Can't find file \"#{conf_path}\""
  end
  @content = read_file(conf_path)
  if @content.empty? && inspec.file(conf_path).size > 0
    return skip_resource "Can't read file \"#{conf_path}\""
  end

  read_content
end

#read_contentObject



61
62
63
64
65
# File 'lib/resources/parse_config.rb', line 61

def read_content
  # parse the file
  @params = SimpleConfig.new(@content, @opts).params
  @content
end

#read_file(path) ⇒ Object



57
58
59
# File 'lib/resources/parse_config.rb', line 57

def read_file(path)
  @files_contents[path] ||= inspec.file(path).content
end

#to_sObject



67
68
69
# File 'lib/resources/parse_config.rb', line 67

def to_s
  "Parse Config #{@conf_path}"
end