Class Puppet::Indirector::Terminus
In: lib/puppet/indirector/terminus.rb
Parent: Object

A simple class that can function as the base class for indirected types.

Methods

Included Modules

Puppet::Util::InstanceLoader

Attributes

abstract_terminus  [R] 
indirection  [R] 
name  [RW] 
terminus_type  [RW] 

Public Class methods

Are we an abstract terminus type, rather than an instance with an associated indirection?

[Source]

    # File lib/puppet/indirector/terminus.rb, line 18
18:         def abstract_terminus?
19:             abstract_terminus
20:         end

Convert a constant to a short name.

[Source]

    # File lib/puppet/indirector/terminus.rb, line 23
23:         def const2name(const)
24:             const.sub(/^[A-Z]/) { |i| i.downcase }.gsub(/[A-Z]/) { |i| "_" + i.downcase }.intern
25:         end

Look up the indirection if we were only provided a name.

[Source]

    # File lib/puppet/indirector/terminus.rb, line 28
28:         def indirection=(name)
29:             if name.is_a?(Puppet::Indirector::Indirection)
30:                 @indirection = name
31:             elsif ind = Puppet::Indirector::Indirection.instance(name)
32:                 @indirection = ind
33:             else
34:                 raise ArgumentError, "Could not find indirection instance %s for %s" % [name, self.name]
35:             end
36:         end

[Source]

    # File lib/puppet/indirector/terminus.rb, line 38
38:         def indirection_name
39:             @indirection.name
40:         end

Register our subclass with the appropriate indirection. This follows the convention that our terminus is named after the indirection.

[Source]

    # File lib/puppet/indirector/terminus.rb, line 45
45:         def inherited(subclass)
46:             longname = subclass.to_s
47:             if longname =~ /#<Class/
48:                 raise Puppet::DevError, "Terminus subclasses must have associated constants"
49:             end
50:             names = longname.split("::")
51: 
52:             # Convert everything to a lower-case symbol, converting camelcase to underscore word separation.
53:             name = names.pop.sub(/^[A-Z]/) { |i| i.downcase }.gsub(/[A-Z]/) { |i| "_" + i.downcase }.intern
54: 
55:             subclass.name = name
56: 
57:             # Short-circuit the abstract types, which are those that directly subclass
58:             # the Terminus class.
59:             if self == Puppet::Indirector::Terminus
60:                 subclass.mark_as_abstract_terminus
61:                 return
62:             end
63: 
64:             # Set the terminus type to be the name of the abstract terminus type.
65:             # Yay, class/instance confusion.
66:             subclass.terminus_type = self.name
67: 
68:             # Our subclass is specifically associated with an indirection.
69:             raise("Invalid name %s" % longname) unless names.length > 0
70:             indirection_name = names.pop.sub(/^[A-Z]/) { |i| i.downcase }.gsub(/[A-Z]/) { |i| "_" + i.downcase }.intern
71: 
72:             if indirection_name == "" or indirection_name.nil?
73:                 raise Puppet::DevError, "Could not discern indirection model from class constant"
74:             end
75: 
76:             # This will throw an exception if the indirection instance cannot be found.
77:             # Do this last, because it also registers the terminus type with the indirection,
78:             # which needs the above information.
79:             subclass.indirection = indirection_name
80: 
81:             # And add this instance to the instance hash.
82:             Puppet::Indirector::Terminus.register_terminus_class(subclass)
83:         end

Mark that this instance is abstract.

[Source]

    # File lib/puppet/indirector/terminus.rb, line 86
86:         def mark_as_abstract_terminus
87:             @abstract_terminus = true
88:         end

[Source]

    # File lib/puppet/indirector/terminus.rb, line 90
90:         def model
91:             indirection.model
92:         end

Convert a short name to a constant.

[Source]

    # File lib/puppet/indirector/terminus.rb, line 95
95:         def name2const(name)
96:             name.to_s.capitalize.sub(/_(.)/) { |i| $1.upcase }
97:         end

[Source]

     # File lib/puppet/indirector/terminus.rb, line 135
135:     def initialize
136:         if self.class.abstract_terminus?
137:             raise Puppet::DevError, "Cannot create instances of abstract terminus types"
138:         end
139:     end

Register a class, probably autoloaded.

[Source]

     # File lib/puppet/indirector/terminus.rb, line 100
100:         def register_terminus_class(klass)
101:             setup_instance_loading klass.indirection_name
102:             instance_hash(klass.indirection_name)[klass.name] = klass
103:         end

Return a terminus by name, using the autoloader.

[Source]

     # File lib/puppet/indirector/terminus.rb, line 106
106:         def terminus_class(indirection_name, terminus_type)
107:             setup_instance_loading indirection_name
108:             loaded_instance(indirection_name, terminus_type)
109:         end

Return all terminus classes for a given indirection.

[Source]

     # File lib/puppet/indirector/terminus.rb, line 112
112:         def terminus_classes(indirection_name)
113:             setup_instance_loading indirection_name
114:             
115:             # Load them all.
116:             instance_loader(indirection_name).loadall
117: 
118:             # And return the list of names.
119:             loaded_instances(indirection_name)
120:         end

Public Instance methods

[Source]

     # File lib/puppet/indirector/terminus.rb, line 131
131:     def indirection
132:         self.class.indirection
133:     end

[Source]

     # File lib/puppet/indirector/terminus.rb, line 141
141:     def model
142:         self.class.model
143:     end

[Source]

     # File lib/puppet/indirector/terminus.rb, line 145
145:     def name
146:         self.class.name
147:     end

[Source]

     # File lib/puppet/indirector/terminus.rb, line 149
149:     def terminus_type
150:         self.class.terminus_type
151:     end

[Validate]