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