Class: PuppetModuleParser
- Inherits:
-
Object
- Object
- PuppetModuleParser
- Defined in:
- lib/kafo/puppet_module_parser.rb
Overview
Based on ideas from puppet-parse by Johan van den Dorpe
Class Method Summary collapse
Instance Method Summary collapse
- #docs ⇒ Object
-
#initialize(file) ⇒ PuppetModuleParser
constructor
A new instance of PuppetModuleParser.
- #klass ⇒ Object
- #parameters ⇒ Object
- #validations(param = nil) ⇒ Object
Constructor Details
#initialize(file) ⇒ PuppetModuleParser
Returns a new instance of PuppetModuleParser.
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/kafo/puppet_module_parser.rb', line 15 def initialize(file) raise ModuleName, "File not found #{file}, check you answer file" unless File.exists?(file) parser = Puppet::Parser::Parser.new('production') values = Puppet.settings.instance_variable_get('@values') values[:production][:confdir] ||= '/' # just some stubbing parser.import(file) # Find object in list of hostclasses parser.environment.known_resource_types.hostclasses.each do |x| @object = x.last if x.last.file == file end # Find object in list of definitions parser.environment.known_resource_types.definitions.each do |x| @object = x.last if x.last.file == file end end |
Class Method Details
.parse(file) ⇒ Object
5 6 7 8 9 10 11 12 13 |
# File 'lib/kafo/puppet_module_parser.rb', line 5 def self.parse(file) content = new(file) { 'parameters' => content.parameters, 'docs' => content.docs, 'validations' => content.validations } end |
Instance Method Details
#docs ⇒ Object
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/kafo/puppet_module_parser.rb', line 47 def docs docs = {} if !@object.doc.nil? rdoc = RDoc::Markup.parse(@object.doc) items = rdoc.parts.select { |part| part.respond_to?(:items) }.map(&:items).flatten items.each do |item| # Skip rdoc items that aren't paragraphs next unless (item.parts.to_s.scan("RDoc::Markup::Paragraph") == ["RDoc::Markup::Paragraph"]) # RDoc (>= 4) makes label an array label = item.label.is_a?(Array) ? item.label.first : item.label # Documentation must be a list - if there's no label then skip next if label.nil? key = label.tr('^A-Za-z0-9_-', '') docs[key] = item.parts.first.parts.map!(&:strip) end end docs end |
#klass ⇒ Object
39 40 41 |
# File 'lib/kafo/puppet_module_parser.rb', line 39 def klass @object.name if @object.class.respond_to?(:name) end |
#parameters ⇒ Object
32 33 34 35 36 37 |
# File 'lib/kafo/puppet_module_parser.rb', line 32 def parameters parameters = {} arguments = @object.respond_to?(:arguments) ? @object.arguments : {} arguments.each { |k, v| parameters[k] = v.respond_to?(:value) ? v.value : nil } parameters end |
#validations(param = nil) ⇒ Object
43 44 45 |
# File 'lib/kafo/puppet_module_parser.rb', line 43 def validations(param = nil) @object.code.select { |stmt| stmt.is_a?(Puppet::Parser::AST::Function) && stmt.name =~ /^validate_/ } end |