| 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.
| abstract_terminus | [R] | |
| indirection | [R] | |
| name | [RW] | |
| terminus_type | [RW] |
Are we an abstract terminus type, rather than an instance with an associated indirection?
# File lib/puppet/indirector/terminus.rb, line 18
18: def abstract_terminus?
19: abstract_terminus
20: end
Look up the indirection if we were only provided a name.
# 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
# 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.
# 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.
# File lib/puppet/indirector/terminus.rb, line 86
86: def mark_as_abstract_terminus
87: @abstract_terminus = true
88: end
# 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.
# 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 all terminus classes for a given indirection.
# 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
# File lib/puppet/indirector/terminus.rb, line 131
131: def indirection
132: self.class.indirection
133: end