Class: Puppet::Indirector::Indirection
- Extended by:
- Util::Instrumentation::Instrumentable
- Includes:
- Util::Docs
- Defined in:
- lib/vendor/puppet/indirector/indirection.rb
Overview
The class that connects functional classes with their different collection back-ends. Each indirection has a set of associated terminus classes, each of which is a subclass of Puppet::Indirector::Terminus.
Constant Summary collapse
- @@indirections =
[]
Constants included from Util::Instrumentation::Instrumentable
Util::Instrumentation::Instrumentable::INSTRUMENTED_CLASSES
Constants included from Util::Docs
Instance Attribute Summary collapse
-
#cache_class ⇒ Object
Returns the value of attribute cache_class.
-
#model ⇒ Object
Returns the value of attribute model.
-
#name ⇒ Object
Returns the value of attribute name.
-
#termini ⇒ Object
readonly
Returns the value of attribute termini.
-
#terminus_setting ⇒ Object
This can be used to select the terminus class.
Attributes included from Util::Instrumentation::Instrumentable
Attributes included from Util::Docs
Class Method Summary collapse
-
.instance(name) ⇒ Object
Find an indirection by name.
-
.instances ⇒ Object
Return a list of all known indirections.
-
.model(name) ⇒ Object
Find an indirected model by name.
Instance Method Summary collapse
-
#cache ⇒ Object
Create and return our cache terminus.
-
#cache? ⇒ Boolean
Should we use a cache?.
-
#delete ⇒ Object
This is only used for testing.
-
#destroy(key, *args) ⇒ Object
Remove something via the terminus.
-
#doc ⇒ Object
Generate the full doc string.
-
#expiration ⇒ Object
Calculate the expiration date for a returned instance.
-
#expire(key, *args) ⇒ Object
Expire a cached object, if one is cached.
-
#find(key, *args) ⇒ Object
Search for an instance in the appropriate terminus, caching the results if caching is configured..
- #find_in_cache(request) ⇒ Object
-
#head(key, *args) ⇒ Object
Search for an instance in the appropriate terminus, and return a boolean indicating whether the instance was found.
-
#initialize(model, name, options = {}) ⇒ Indirection
constructor
A new instance of Indirection.
-
#request(*args) ⇒ Object
Set up our request object.
- #reset_terminus_class ⇒ Object
-
#save(instance, key = nil) ⇒ Object
Save the instance in the appropriate terminus.
-
#search(key, *args) ⇒ Object
Search for more than one instance.
-
#terminus(terminus_name = nil) ⇒ Object
Return the singleton terminus for this indirection.
-
#terminus_class ⇒ Object
Determine the terminus class.
-
#terminus_class=(klass) ⇒ Object
Specify the terminus class to use.
-
#ttl ⇒ Object
Default to the runinterval for the ttl.
-
#ttl=(value) ⇒ Object
Set the time-to-live for instances created through this indirection.
-
#validate_terminus_class(terminus_class) ⇒ Object
This is used by terminus_class= and cache=.
Methods included from Util::Instrumentation::Instrumentable
clear_probes, disable_probes, each_probe, enable_probes, probe, probe_names, probes
Methods included from Util::Docs
#desc, #dochook, #doctable, #markdown_definitionlist, #markdown_header, #nodoc?, #pad, scrub
Constructor Details
#initialize(model, name, options = {}) ⇒ Indirection
Returns a new instance of Indirection.
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 |
# File 'lib/vendor/puppet/indirector/indirection.rb', line 94 def initialize(model, name, = {}) @model = model @name = name @termini = {} @cache_class = nil @terminus_class = nil raise(ArgumentError, "Indirection #{@name} is already defined") if @@indirections.find { |i| i.name == @name } @@indirections << self if mod = [:extend] extend(mod) .delete(:extend) end # This is currently only used for cache_class and terminus_class. .each do |name, value| begin send(name.to_s + "=", value) rescue NoMethodError raise ArgumentError, "#{name} is not a valid Indirection parameter" end end end |
Instance Attribute Details
#cache_class ⇒ Object
Returns the value of attribute cache_class.
53 54 55 |
# File 'lib/vendor/puppet/indirector/indirection.rb', line 53 def cache_class @cache_class end |
#model ⇒ Object
Returns the value of attribute model.
13 14 15 |
# File 'lib/vendor/puppet/indirector/indirection.rb', line 13 def model @model end |
#name ⇒ Object
Returns the value of attribute name.
13 14 15 |
# File 'lib/vendor/puppet/indirector/indirection.rb', line 13 def name @name end |
#termini ⇒ Object (readonly)
Returns the value of attribute termini.
14 15 16 |
# File 'lib/vendor/puppet/indirector/indirection.rb', line 14 def termini @termini end |
#terminus_setting ⇒ Object
This can be used to select the terminus class.
134 135 136 |
# File 'lib/vendor/puppet/indirector/indirection.rb', line 134 def terminus_setting @terminus_setting end |
Class Method Details
.instance(name) ⇒ Object
Find an indirection by name. This is provided so that Terminus classes can specifically hook up with the indirections they are associated with.
25 26 27 |
# File 'lib/vendor/puppet/indirector/indirection.rb', line 25 def self.instance(name) @@indirections.find { |i| i.name == name } end |
.instances ⇒ Object
Return a list of all known indirections. Used to generate the reference.
31 32 33 |
# File 'lib/vendor/puppet/indirector/indirection.rb', line 31 def self.instances @@indirections.collect { |i| i.name } end |
.model(name) ⇒ Object
Find an indirected model by name. This is provided so that Terminus classes can specifically hook up with the indirections they are associated with.
37 38 39 40 |
# File 'lib/vendor/puppet/indirector/indirection.rb', line 37 def self.model(name) return nil unless match = @@indirections.find { |i| i.name == name } match.model end |
Instance Method Details
#cache ⇒ Object
Create and return our cache terminus.
43 44 45 46 |
# File 'lib/vendor/puppet/indirector/indirection.rb', line 43 def cache raise(Puppet::DevError, "Tried to cache when no cache class was set") unless cache_class terminus(cache_class) end |
#cache? ⇒ Boolean
Should we use a cache?
49 50 51 |
# File 'lib/vendor/puppet/indirector/indirection.rb', line 49 def cache? cache_class ? true : false end |
#delete ⇒ Object
This is only used for testing.
61 62 63 |
# File 'lib/vendor/puppet/indirector/indirection.rb', line 61 def delete @@indirections.delete(self) if @@indirections.include?(self) end |
#destroy(key, *args) ⇒ Object
Remove something via the terminus.
236 237 238 239 240 241 242 243 244 245 246 247 248 |
# File 'lib/vendor/puppet/indirector/indirection.rb', line 236 def destroy(key, *args) request = request(:destroy, key, *args) terminus = prepare(request) result = terminus.destroy(request) if cache? and cached = cache.find(request(:find, key, *args)) # Reuse the existing request, since it's equivalent. cache.destroy(request) end result end |
#doc ⇒ Object
Generate the full doc string.
82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/vendor/puppet/indirector/indirection.rb', line 82 def doc text = "" text += scrub(@doc) + "\n\n" if @doc if s = terminus_setting text += "* **Terminus Setting**: #{terminus_setting}" end text end |
#expiration ⇒ Object
Calculate the expiration date for a returned instance.
77 78 79 |
# File 'lib/vendor/puppet/indirector/indirection.rb', line 77 def expiration Time.now + ttl end |
#expire(key, *args) ⇒ Object
Expire a cached object, if one is cached. Note that we don’t actually remove it, we expire it and write it back out to disk. This way people can still use the expired object if they want.
169 170 171 172 173 174 175 176 177 178 179 180 181 182 |
# File 'lib/vendor/puppet/indirector/indirection.rb', line 169 def expire(key, *args) request = request(:expire, key, *args) return nil unless cache? return nil unless instance = cache.find(request(:find, key, *args)) Puppet.info "Expiring the #{self.name} cache of #{instance.name}" # Set an expiration date in the past instance.expiration = Time.now - 60 cache.save(request(:save, instance, *args)) end |
#find(key, *args) ⇒ Object
Search for an instance in the appropriate terminus, caching the results if caching is configured..
186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 |
# File 'lib/vendor/puppet/indirector/indirection.rb', line 186 def find(key, *args) request = request(:find, key, *args) terminus = prepare(request) if result = find_in_cache(request) return result end # Otherwise, return the result from the terminus, caching if appropriate. if ! request.ignore_terminus? and result = terminus.find(request) result.expiration ||= self.expiration if result.respond_to?(:expiration) if cache? and request.use_cache? Puppet.info "Caching #{self.name} for #{request.key}" cache.save request(:save, result, *args) end return terminus.respond_to?(:filter) ? terminus.filter(result) : result end nil end |
#find_in_cache(request) ⇒ Object
219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 |
# File 'lib/vendor/puppet/indirector/indirection.rb', line 219 def find_in_cache(request) # See if our instance is in the cache and up to date. return nil unless cache? and ! request.ignore_cache? and cached = cache.find(request) if cached.expired? Puppet.info "Not using expired #{self.name} for #{request.key} from cache; expired at #{cached.expiration}" return nil end Puppet.debug "Using cached #{self.name} for #{request.key}" cached rescue => detail puts detail.backtrace if Puppet[:trace] Puppet.err "Cached #{self.name} for #{request.key} failed: #{detail}" nil end |
#head(key, *args) ⇒ Object
Search for an instance in the appropriate terminus, and return a boolean indicating whether the instance was found.
210 211 212 213 214 215 216 217 |
# File 'lib/vendor/puppet/indirector/indirection.rb', line 210 def head(key, *args) request = request(:head, key, *args) terminus = prepare(request) # Look in the cache first, then in the terminus. Force the result # to be a boolean. !!(find_in_cache(request) || terminus.head(request)) end |
#request(*args) ⇒ Object
Set up our request object.
121 122 123 |
# File 'lib/vendor/puppet/indirector/indirection.rb', line 121 def request(*args) Puppet::Indirector::Request.new(self.name, *args) end |
#reset_terminus_class ⇒ Object
148 149 150 |
# File 'lib/vendor/puppet/indirector/indirection.rb', line 148 def reset_terminus_class @terminus_class = nil end |
#save(instance, key = nil) ⇒ Object
Save the instance in the appropriate terminus. This method is normally an instance method on the indirected class.
267 268 269 270 271 272 273 274 275 276 277 |
# File 'lib/vendor/puppet/indirector/indirection.rb', line 267 def save(instance, key = nil) request = request(:save, key, instance) terminus = prepare(request) result = terminus.save(request) # If caching is enabled, save our document there cache.save(request) if cache? result end |
#search(key, *args) ⇒ Object
Search for more than one instance. Should always return an array.
251 252 253 254 255 256 257 258 259 260 261 262 263 |
# File 'lib/vendor/puppet/indirector/indirection.rb', line 251 def search(key, *args) request = request(:search, key, *args) terminus = prepare(request) if result = terminus.search(request) raise Puppet::DevError, "Search results from terminus #{terminus.name} are not an array" unless result.is_a?(Array) result.each do |instance| next unless instance.respond_to? :expiration instance.expiration ||= self.expiration end return result end end |
#terminus(terminus_name = nil) ⇒ Object
Return the singleton terminus for this indirection.
126 127 128 129 130 131 |
# File 'lib/vendor/puppet/indirector/indirection.rb', line 126 def terminus(terminus_name = nil) # Get the name of the terminus. raise Puppet::DevError, "No terminus specified for #{self.name}; cannot redirect" unless terminus_name ||= terminus_class termini[terminus_name] ||= make_terminus(terminus_name) end |
#terminus_class ⇒ Object
Determine the terminus class.
137 138 139 140 141 142 143 144 145 146 |
# File 'lib/vendor/puppet/indirector/indirection.rb', line 137 def terminus_class unless @terminus_class if setting = self.terminus_setting self.terminus_class = Puppet.settings[setting].to_sym else raise Puppet::DevError, "No terminus class nor terminus setting was provided for indirection #{self.name}" end end @terminus_class end |
#terminus_class=(klass) ⇒ Object
Specify the terminus class to use.
153 154 155 156 |
# File 'lib/vendor/puppet/indirector/indirection.rb', line 153 def terminus_class=(klass) validate_terminus_class(klass) @terminus_class = klass end |
#ttl ⇒ Object
Default to the runinterval for the ttl.
72 73 74 |
# File 'lib/vendor/puppet/indirector/indirection.rb', line 72 def ttl @ttl ||= Puppet[:runinterval].to_i end |
#ttl=(value) ⇒ Object
Set the time-to-live for instances created through this indirection.
66 67 68 69 |
# File 'lib/vendor/puppet/indirector/indirection.rb', line 66 def ttl=(value) raise ArgumentError, "Indirection TTL must be an integer" unless value.is_a?(Fixnum) @ttl = value end |
#validate_terminus_class(terminus_class) ⇒ Object
This is used by terminus_class= and cache=.
159 160 161 162 163 164 |
# File 'lib/vendor/puppet/indirector/indirection.rb', line 159 def validate_terminus_class(terminus_class) raise ArgumentError, "Invalid terminus name #{terminus_class.inspect}" unless terminus_class and terminus_class.to_s != "" unless Puppet::Indirector::Terminus.terminus_class(self.name, terminus_class) raise ArgumentError, "Could not find terminus #{terminus_class} for indirection #{self.name}" end end |