# class Puppet::Resource::TypeCollection

### Attributes

environment[R]
parse_failed[RW]

### Public Class Methods

new(env) click to toggle source

    # File lib/puppet/resource/type_collection.rb, line 15
    def initialize(env)
      @environment = env.is_a?(String) ? Puppet::Node::Environment.new(env) : env
      @hostclasses = {}
      @definitions = {}
      @nodes = {}
      @notfound = {}

      # So we can keep a list and match the first-defined regex
      @node_list = []

      @watched_files = {}
    end

### Public Instance Methods

<<(thing) click to toggle source

    # File lib/puppet/resource/type_collection.rb, line 38
    def <<(thing)
      add(thing)
      self
    end

add(instance) click to toggle source

    # File lib/puppet/resource/type_collection.rb, line 43
    def add(instance)
      if instance.type == :hostclass and other = @hostclasses[instance.name] and other.type == :hostclass
        other.merge(instance)
        return other
      end
      method = "add_#{instance.type}"
      send(method, instance)
      instance.resource_type_collection = self
      instance
    end

add_definition(instance) click to toggle source

    # File lib/puppet/resource/type_collection.rb, line 100
    def add_definition(instance)
      dupe_check(instance, @hostclasses) { |dupe| "'#{instance.name}' is already defined#{dupe.error_context} as a class; cannot redefine as a definition" }
      dupe_check(instance, @definitions) { |dupe| "Definition '#{instance.name}' is already defined#{dupe.error_context}; cannot be redefined" }
      @definitions[instance.name] = instance
    end

add_hostclass(instance) click to toggle source

    # File lib/puppet/resource/type_collection.rb, line 54
    def add_hostclass(instance)
      dupe_check(instance, @hostclasses) { |dupe| "Class '#{instance.name}' is already defined#{dupe.error_context}; cannot redefine" }
      dupe_check(instance, @definitions) { |dupe| "Definition '#{instance.name}' is already defined#{dupe.error_context}; cannot be redefined as a class" }

      @hostclasses[instance.name] = instance
      instance
    end

add_node(instance) click to toggle source

    # File lib/puppet/resource/type_collection.rb, line 66
    def add_node(instance)
      dupe_check(instance, @nodes) { |dupe| "Node '#{instance.name}' is already defined#{dupe.error_context}; cannot redefine" }

      @node_list << instance
      @nodes[instance.name] = instance
      instance
    end

clear() click to toggle source

    # File lib/puppet/resource/type_collection.rb, line 7
    def clear
      @hostclasses.clear
      @definitions.clear
      @nodes.clear
      @watched_files.clear
      @notfound.clear
    end

definition(name) click to toggle source

    # File lib/puppet/resource/type_collection.rb, line 106
    def definition(name)
      @definitions[munge_name(name)]
    end

find_definition(namespaces, name) click to toggle source

    # File lib/puppet/resource/type_collection.rb, line 118
    def find_definition(namespaces, name)
      find_or_load(namespaces, name, :definition)
    end

find_hostclass(namespaces, name, options = {}) click to toggle source

    # File lib/puppet/resource/type_collection.rb, line 114
    def find_hostclass(namespaces, name, options = {})
      find_or_load(namespaces, name, :hostclass, options)
    end

find_node(namespaces, name) click to toggle source

    # File lib/puppet/resource/type_collection.rb, line 110
    def find_node(namespaces, name)
      @nodes[munge_name(name)]
    end

hostclass(name) click to toggle source

    # File lib/puppet/resource/type_collection.rb, line 62
    def hostclass(name)
      @hostclasses[munge_name(name)]
    end

import_ast(ast, modname) click to toggle source

    # File lib/puppet/resource/type_collection.rb, line 28
    def import_ast(ast, modname)
      ast.instantiate(modname).each do |instance|
        add(instance)
      end
    end

inspect() click to toggle source

    # File lib/puppet/resource/type_collection.rb, line 34
    def inspect
      "TypeCollection" + { :hostclasses => @hostclasses.keys, :definitions => @definitions.keys, :nodes => @nodes.keys }.inspect
    end

loader() click to toggle source

    # File lib/puppet/resource/type_collection.rb, line 74
    def loader
      @loader ||= Puppet::Parser::TypeLoader.new(environment)
    end

node(name) click to toggle source

    # File lib/puppet/resource/type_collection.rb, line 78
    def node(name)
      name = munge_name(name)

      if node = @nodes[name]
        return node
      end

      @node_list.each do |node|
        next unless node.name_is_regex?
        return node if node.match(name)
      end
      nil
    end

node_exists?(name) click to toggle source

    # File lib/puppet/resource/type_collection.rb, line 92
    def node_exists?(name)
      @nodes[munge_name(name)]
    end

nodes?() click to toggle source

    # File lib/puppet/resource/type_collection.rb, line 96
    def nodes?
      @nodes.length > 0
    end

require_reparse?() click to toggle source

    # File lib/puppet/resource/type_collection.rb, line 128
    def require_reparse?
      @parse_failed || stale?
    end

stale?() click to toggle source

    # File lib/puppet/resource/type_collection.rb, line 132
    def stale?
      @watched_files.values.detect { |file| file.changed? }
    end

version() click to toggle source

    # File lib/puppet/resource/type_collection.rb, line 136
    def version
      return @version if defined?(@version)

      if environment[:config_version] == ""
        @version = Time.now.to_i
        return @version
      end

      @version = Puppet::Util::Execution.execute([environment[:config_version]]).strip

    rescue Puppet::ExecutionFailure => e
      raise Puppet::ParseError, "Unable to set config_version: #{e.message}"
    end

watch_file(file) click to toggle source

    # File lib/puppet/resource/type_collection.rb, line 150
    def watch_file(file)
      @watched_files[file] = Puppet::Util::LoadedFile.new(file)
    end

watching_file?(file) click to toggle source

    # File lib/puppet/resource/type_collection.rb, line 154
    def watching_file?(file)
      @watched_files.include?(file)
    end