Project

General

Profile

puppet_class.rb

Stefan Julin, 10/30/2014 03:52 AM

 
1
require 'puppet_proxy/class_scanner'
2
require 'puppet_proxy/class_scanner_eparser'
3

    
4
class Proxy::Puppet::PuppetClass
5
  class << self
6
    # scans a given directory and its sub directory for puppet classes
7
    # returns an array of PuppetClass objects.
8
    def scan_directory directory, environment, eparser = false
9
      # Get a Puppet Parser to parse the manifest source
10

    
11
      if eparser
12
        Proxy::Puppet::ClassScannerEParser.scan_directory directory
13
      else
14
        Proxy::Puppet::ClassScanner.scan_directory directory, environment
15
      end
16
    end
17
  end
18

    
19
  def initialize name, params = {}
20
    @klass  = name || raise("Must provide puppet class name")
21
    @params = params
22
  end
23

    
24
  def to_s
25
    self.module.nil? ? name : "#{self.module}::#{name}"
26
  end
27

    
28
  # returns module name (excluding of the class name)
29
  def module
30
    klass[0..(klass.index("::")-1)] if has_module?(klass)
31
  end
32

    
33
  # returns class name (excluding of the module name)
34
  def name
35
    has_module?(klass) ? klass[(klass.index("::")+2)..-1] : klass
36
  end
37

    
38
  attr_reader :params
39

    
40
  private
41
  attr_reader :klass
42

    
43
  def has_module?(klass)
44
    !!klass.index("::")
45
  end
46
end