class Puppet::Network::Format
A simple class for modeling encoding formats for moving instances around the network.
Attributes
charset[RW]
extension[RW]
intern_method[RW]
intern_multiple_method[RW]
mime[R]
name[R]
render_method[RW]
render_multiple_method[RW]
required_methods[RW]
weight[RW]
Public Class Methods
new(name, options = {}, &block)
click to toggle source
# File lib/puppet/network/format.rb 18 def initialize(name, options = {}, &block) 19 @name = name.to_s.downcase.intern 20 21 @options = options 22 23 # This must be done early the values can be used to set required_methods 24 define_method_names 25 26 method_list = { 27 :intern_method => "from_#{name}", 28 :intern_multiple_method => "from_multiple_#{name}", 29 :render_multiple_method => "to_multiple_#{name}", 30 :render_method => "to_#{name}" 31 } 32 33 init_attribute(:mime, "text/#{name}") 34 init_attribute(:weight, 5) 35 init_attribute(:required_methods, method_list.keys) 36 init_attribute(:extension, name.to_s) 37 init_attribute(:charset, nil) 38 39 method_list.each do |method, value| 40 init_attribute(method, value) 41 end 42 43 raise ArgumentError, _("Unsupported option(s) %{options_list}") % { options_list: @options.keys } unless @options.empty? 44 45 @options = nil 46 47 instance_eval(&block) if block_given? 48 end
Public Instance Methods
init_attribute(name, default)
click to toggle source
# File lib/puppet/network/format.rb 11 def init_attribute(name, default) 12 value = @options.delete(name) 13 value = default if value.nil? 14 15 self.send(name.to_s + "=", value) 16 end
intern(klass, text)
click to toggle source
# File lib/puppet/network/format.rb 50 def intern(klass, text) 51 return klass.send(intern_method, text) if klass.respond_to?(intern_method) 52 raise NotImplementedError, "#{klass} does not respond to #{intern_method}; can not intern instances from #{mime}" 53 end
intern_multiple(klass, text)
click to toggle source
# File lib/puppet/network/format.rb 55 def intern_multiple(klass, text) 56 return klass.send(intern_multiple_method, text) if klass.respond_to?(intern_multiple_method) 57 raise NotImplementedError, "#{klass} does not respond to #{intern_multiple_method}; can not intern multiple instances from #{mime}" 58 end
mime=(mime)
click to toggle source
# File lib/puppet/network/format.rb 60 def mime=(mime) 61 @mime = mime.to_s.downcase 62 end
render(instance)
click to toggle source
# File lib/puppet/network/format.rb 64 def render(instance) 65 return instance.send(render_method) if instance.respond_to?(render_method) 66 raise NotImplementedError, "#{instance.class} does not respond to #{render_method}; can not render instances to #{mime}" 67 end
render_multiple(instances)
click to toggle source
# File lib/puppet/network/format.rb 69 def render_multiple(instances) 70 # This method implicitly assumes that all instances are of the same type. 71 return instances[0].class.send(render_multiple_method, instances) if instances[0].class.respond_to?(render_multiple_method) 72 raise NotImplementedError, _("%{klass} does not respond to %{method}; can not render multiple instances to %{mime}") % 73 { klass: instances[0].class, method: render_multiple_method, mime: mime } 74 end
required_methods_present?(klass)
click to toggle source
# File lib/puppet/network/format.rb 76 def required_methods_present?(klass) 77 [:intern_method, :intern_multiple_method, :render_multiple_method].each do |name| 78 return false unless required_method_present?(name, klass, :class) 79 end 80 81 return false unless required_method_present?(:render_method, klass, :instance) 82 83 true 84 end
supported?(klass)
click to toggle source
# File lib/puppet/network/format.rb 86 def supported?(klass) 87 suitable? and required_methods_present?(klass) 88 end
to_s()
click to toggle source
# File lib/puppet/network/format.rb 90 def to_s 91 "Puppet::Network::Format[#{name}]" 92 end
Private Instance Methods
define_method_names()
click to toggle source
# File lib/puppet/network/format.rb 96 def define_method_names 97 @intern_method = "from_#{name}" 98 @render_method = "to_#{name}" 99 @intern_multiple_method = "from_multiple_#{name}" 100 @render_multiple_method = "to_multiple_#{name}" 101 end
required_method_present?(name, klass, type)
click to toggle source
# File lib/puppet/network/format.rb 103 def required_method_present?(name, klass, type) 104 return true unless required_methods.include?(name) 105 106 method = send(name) 107 108 return(type == :class ? klass.respond_to?(method) : klass.method_defined?(method)) 109 end