Class: PuppetParse::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/puppet-parse/parser.rb

Instance Method Summary collapse

Constructor Details

#initialize(file) ⇒ Parser

Returns a new instance of Parser.



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/puppet-parse/parser.rb', line 4

def initialize(file)
  # Read file and return parsed object
  pparser         = Puppet::Parser::Parser.new('production')
  if File.exists?(file)
    @file = File.expand_path(file)
    pparser.import(@file)
    
    # Find object in list of hostclasses
    pparser.environment.known_resource_types.hostclasses.each do |x|
      @object = x.last if x.last.file == @file
    end
    # Find object in list of definitions
    pparser.environment.known_resource_types.definitions.each do |x|
      @object = x.last if x.last.file == @file
    end
    
  else
    'File does not exist'
  end      
end

Instance Method Details

#docsObject

Read RDOC contents from parsed object, returns hash of paragraph headings and the following paragraph contents (i.e. parameter and parameter documentation)



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/puppet-parse/parser.rb', line 40

def docs
  if !@object.doc.nil?
    rdoc            = RDoc::Markup.parse(@object.doc)
    docs            = {}

    rdoc.parts.each do |part|
      if part.respond_to?(:items)
        part.items.each do |item|
          # Skip rdoc items that aren't paragraphs
          next unless (item.parts.to_s.scan("RDoc::Markup::Paragraph") == ["RDoc::Markup::Paragraph"])
          # Documentation must be a list - if there's no label then skip
          next if item.label.nil?
          key       = item.label.tr('^A-Za-z0-9_-', '')
          docs[key] = item.parts.first.parts
        end # do item
      end # endif
    end # do parm
  
    docs
  end # if nil?
end

#klassObject

Read class from parsed object, returns string containing class



33
34
35
# File 'lib/puppet-parse/parser.rb', line 33

def klass
  @object.name if (defined? @object.class.name)
end

#parametersObject

Read parameters from parsed object, returns hash of parameters and default values



27
28
29
30
# File 'lib/puppet-parse/parser.rb', line 27

def parameters
  result = (defined? @object.arguments) ? @object.arguments : {}
  result
end