# File lib/puppet/network/format_handler.rb, line 32 def self.create(*args, &block) instance = Puppet::Network::Format.new(*args) instance.instance_eval(&block) if block_given? @formats[instance.name] = instance instance end
# File lib/puppet/network/format_handler.rb, line 40 def self.create_serialized_formats(name,options = {},&block) ["application/x-#{name}", "application/#{name}", "text/x-#{name}", "text/#{name}"].each { |mime_type| create name, {:mime => mime_type}.update(options), &block } end
# File lib/puppet/network/format_handler.rb, line 46 def self.extended(klass) klass.extend(ClassMethods) # LAK:NOTE This won't work in 1.9 ('send' won't be able to send # private methods, but I don't know how else to do it. klass.send(:include, InstanceMethods) end
# File lib/puppet/network/format_handler.rb, line 54 def self.format(name) @formats[name.to_s.downcase.intern] end
# File lib/puppet/network/format_handler.rb, line 58 def self.format_by_extension(ext) @formats.each do |name, format| return format if format.extension == ext end nil end
Return a format name given:
* a format name * a mime-type * a format instance
# File lib/puppet/network/format_handler.rb, line 89 def self.format_to_canonical_name(format) case format when Puppet::Network::Format out = format when %r{\w+/\w+} out = mime(format) else out = format(format) end raise ArgumentError, "No format match the given format name or mime-type (#{format})" if out.nil? out.name end
Provide a list of all formats.
# File lib/puppet/network/format_handler.rb, line 66 def self.formats @formats.keys end
Return a format capable of handling the provided mime type.
# File lib/puppet/network/format_handler.rb, line 71 def self.mime(mimetype) mimetype = mimetype.to_s.downcase @formats.values.find { |format| format.mime == mimetype } end
Use a delegator to make sure any exceptions generated by our formats are handled intelligently.
# File lib/puppet/network/format_handler.rb, line 78 def self.protected_format(name) name = format_to_canonical_name(name) @format_protectors ||= {} @format_protectors[name] ||= FormatProtector.new(name) @format_protectors[name] end