Module: Puppet::Network::FormatHandler

Defined in:
lib/vendor/puppet/network/format_handler.rb

Defined Under Namespace

Modules: ClassMethods, InstanceMethods Classes: FormatError, FormatProtector

Class Method Summary collapse

Class Method Details

.create(*args, &block) ⇒ Object



32
33
34
35
36
37
38
# File 'lib/vendor/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

.create_serialized_formats(name, options = {}, &block) ⇒ Object



40
41
42
43
44
# File 'lib/vendor/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

.extended(klass) ⇒ Object



46
47
48
49
50
51
52
# File 'lib/vendor/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

.format(name) ⇒ Object



54
55
56
# File 'lib/vendor/puppet/network/format_handler.rb', line 54

def self.format(name)
  @formats[name.to_s.downcase.intern]
end

.format_by_extension(ext) ⇒ Object



58
59
60
61
62
63
# File 'lib/vendor/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

.format_to_canonical_name(format) ⇒ Object

Return a format name given:

* a format name
* a mime-type
* a format instance

Raises:

  • (ArgumentError)


89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/vendor/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

.formatsObject

Provide a list of all formats.



66
67
68
# File 'lib/vendor/puppet/network/format_handler.rb', line 66

def self.formats
  @formats.keys
end

.mime(mimetype) ⇒ Object

Return a format capable of handling the provided mime type.



71
72
73
74
# File 'lib/vendor/puppet/network/format_handler.rb', line 71

def self.mime(mimetype)
  mimetype = mimetype.to_s.downcase
  @formats.values.find { |format| format.mime == mimetype }
end

.protected_format(name) ⇒ Object

Use a delegator to make sure any exceptions generated by our formats are handled intelligently.



78
79
80
81
82
83
# File 'lib/vendor/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