Class Mime::Type
In: vendor/rails/actionpack/lib/action_controller/mime_type.rb
Parent: Object

Encapsulates the notion of a mime type. Can be used at render time, for example, with:

  class PostsController < ActionController::Base
    def show
      @post = Post.find(params[:id])

      respond_to do |format|
        format.html
        format.ics { render :text => post.to_ics, :mime_type => Mime::Type["text/calendar"]  }
        format.xml { render :xml => @people.to_xml }
      end
    end
  end

Methods

==   ===   lookup   new   parse   register   to_s   to_str   to_sym  

Public Class methods

[Source]

    # File vendor/rails/actionpack/lib/action_controller/mime_type.rb, line 43
43:       def lookup(string)
44:         LOOKUP[string]
45:       end

[Source]

     # File vendor/rails/actionpack/lib/action_controller/mime_type.rb, line 103
103:     def initialize(string, symbol = nil, synonyms = [])
104:       @symbol, @synonyms = symbol, synonyms
105:       @string = string
106:     end

[Source]

     # File vendor/rails/actionpack/lib/action_controller/mime_type.rb, line 53
 53:       def parse(accept_header)
 54:         # keep track of creation order to keep the subsequent sort stable
 55:         index = 0
 56:         list = accept_header.split(/,/).map! do |i| 
 57:           AcceptItem.new(index += 1, *i.split(/;\s*q=/))
 58:         end.sort!
 59: 
 60:         # Take care of the broken text/xml entry by renaming or deleting it
 61:         text_xml = list.index("text/xml")
 62:         app_xml = list.index("application/xml")
 63: 
 64:         if text_xml && app_xml
 65:           # set the q value to the max of the two
 66:           list[app_xml].q = [list[text_xml].q, list[app_xml].q].max
 67: 
 68:           # make sure app_xml is ahead of text_xml in the list
 69:           if app_xml > text_xml
 70:             list[app_xml], list[text_xml] = list[text_xml], list[app_xml]
 71:             app_xml, text_xml = text_xml, app_xml
 72:           end
 73: 
 74:           # delete text_xml from the list
 75:           list.delete_at(text_xml)
 76:   
 77:         elsif text_xml
 78:           list[text_xml].name = "application/xml"
 79:         end
 80: 
 81:         # Look for more specific xml-based types and sort them ahead of app/xml
 82: 
 83:         if app_xml
 84:           idx = app_xml
 85:           app_xml_type = list[app_xml]
 86: 
 87:           while(idx < list.length)
 88:             type = list[idx]
 89:             break if type.q < app_xml_type.q
 90:             if type.name =~ /\+xml$/
 91:               list[app_xml], list[idx] = list[idx], list[app_xml]
 92:               app_xml = idx
 93:             end
 94:             idx += 1
 95:           end
 96:         end
 97: 
 98:         list.map! { |i| Mime::Type.lookup(i.name) }.uniq!
 99:         list
100:       end

[Source]

    # File vendor/rails/actionpack/lib/action_controller/mime_type.rb, line 47
47:       def register(string, symbol, synonyms = [])
48:         Mime.send :const_set, symbol.to_s.upcase, Type.new(string, symbol, synonyms)
49:         SET << Mime.send(:const_get, symbol.to_s.upcase)
50:         LOOKUP[string] = EXTENSION_LOOKUP[symbol.to_s] = SET.last        
51:       end

Public Instance methods

[Source]

     # File vendor/rails/actionpack/lib/action_controller/mime_type.rb, line 128
128:     def ==(mime_type)
129:       (@synonyms + [ self ]).any? { |synonym| synonym.to_s == mime_type.to_s } if mime_type
130:     end

[Source]

     # File vendor/rails/actionpack/lib/action_controller/mime_type.rb, line 120
120:     def ===(list)
121:       if list.is_a?(Array)
122:         (@synonyms + [ self ]).any? { |synonym| list.include?(synonym) }
123:       else
124:         super
125:       end
126:     end

[Source]

     # File vendor/rails/actionpack/lib/action_controller/mime_type.rb, line 108
108:     def to_s
109:       @string
110:     end

[Source]

     # File vendor/rails/actionpack/lib/action_controller/mime_type.rb, line 112
112:     def to_str
113:       to_s
114:     end

[Source]

     # File vendor/rails/actionpack/lib/action_controller/mime_type.rb, line 116
116:     def to_sym
117:       @symbol || @string.to_sym
118:     end

[Validate]