Class: Raw::FormatManager

Inherits:
Object
  • Object
show all
Defined in:
lib/raw/dispatcher/format.rb

Overview

A Format Manager. Provides useful methods for fast Format lookup.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*formats) ⇒ FormatManager

Returns a new instance of FormatManager.



67
68
69
70
71
72
73
74
75
# File 'lib/raw/dispatcher/format.rb', line 67

def initialize(*formats)
  @by_name = {}
  @by_mime_type = {}
  @by_extension = {}
  
  for format in formats.flatten
    put(format)
  end
end

Instance Attribute Details

#by_extensionObject

Formats indexed by extension.



65
66
67
# File 'lib/raw/dispatcher/format.rb', line 65

def by_extension
  @by_extension
end

#by_mime_typeObject

Formats indexed by mime type.



61
62
63
# File 'lib/raw/dispatcher/format.rb', line 61

def by_mime_type
  @by_mime_type
end

#by_nameObject

Formats indexed by name.



57
58
59
# File 'lib/raw/dispatcher/format.rb', line 57

def by_name
  @by_name
end

Instance Method Details

#[](name) ⇒ Object

Lookup a format by name.



92
93
94
# File 'lib/raw/dispatcher/format.rb', line 92

def [](name)
  @by_name[name]
end

#put(format) ⇒ Object Also known as: <<

Add a new format to the manager.



79
80
81
82
83
84
85
86
87
# File 'lib/raw/dispatcher/format.rb', line 79

def put(format)
  if format.is_a? Class
    format = format.new
  end
  
  @by_name[format.name] = format          
  @by_mime_type[format.mime_type] = format          
  @by_extension[format.extension] = format          
end