mime-types 2.99.3 → 3.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (56) hide show
  1. checksums.yaml +4 -4
  2. data/.autotest +35 -0
  3. data/.gemtest +0 -0
  4. data/.gitignore +17 -0
  5. data/.hoerc +20 -0
  6. data/Code-of-Conduct.rdoc +27 -60
  7. data/Contributing.rdoc +0 -1
  8. data/History.rdoc +75 -36
  9. data/Licence.rdoc +2 -16
  10. data/Manifest.txt +10 -18
  11. data/README.rdoc +46 -46
  12. data/Rakefile +112 -58
  13. data/lib/mime-types.rb +0 -2
  14. data/lib/mime/type.rb +183 -415
  15. data/lib/mime/type/columnar.rb +27 -62
  16. data/lib/mime/types.rb +37 -135
  17. data/lib/mime/types/cache.rb +49 -73
  18. data/lib/mime/types/columnar.rb +42 -48
  19. data/lib/mime/types/container.rb +30 -0
  20. data/lib/mime/types/deprecations.rb +1 -22
  21. data/lib/mime/types/full.rb +17 -0
  22. data/lib/mime/types/loader.rb +10 -137
  23. data/lib/mime/types/logger.rb +2 -0
  24. data/lib/mime/types/registry.rb +81 -0
  25. data/support/benchmarks/load.rb +27 -26
  26. data/support/benchmarks/load_allocations.rb +14 -7
  27. data/support/benchmarks/object_counts.rb +6 -4
  28. data/support/profile/columnar.rb +5 -0
  29. data/support/profile/columnar_full.rb +5 -0
  30. data/support/profile/full.rb +5 -0
  31. data/test/minitest_helper.rb +3 -12
  32. data/test/test_mime_type.rb +461 -454
  33. data/test/test_mime_types.rb +126 -86
  34. data/test/test_mime_types_cache.rb +55 -45
  35. data/test/test_mime_types_class.rb +113 -97
  36. data/test/test_mime_types_lazy.rb +19 -23
  37. data/test/test_mime_types_loader.rb +5 -32
  38. metadata +67 -44
  39. data/History-Types.rdoc +0 -454
  40. data/data/mime-types.json +0 -1
  41. data/data/mime.content_type.column +0 -1980
  42. data/data/mime.docs.column +0 -1980
  43. data/data/mime.encoding.column +0 -1980
  44. data/data/mime.friendly.column +0 -1980
  45. data/data/mime.obsolete.column +0 -1980
  46. data/data/mime.registered.column +0 -1980
  47. data/data/mime.signature.column +0 -1980
  48. data/data/mime.use_instead.column +0 -1980
  49. data/data/mime.xrefs.column +0 -1980
  50. data/docs/COPYING.txt +0 -339
  51. data/docs/artistic.txt +0 -127
  52. data/lib/mime/types/loader_path.rb +0 -15
  53. data/support/apache_mime_types.rb +0 -108
  54. data/support/convert.rb +0 -158
  55. data/support/convert/columnar.rb +0 -88
  56. data/support/iana_registry.rb +0 -172
@@ -11,80 +11,45 @@ require 'mime/type'
11
11
  # MIME::Type::Columnar is *not* intended to be created except by
12
12
  # MIME::Types::Columnar containers.
13
13
  class MIME::Type::Columnar < MIME::Type
14
- attr_writer :friendly # :nodoc:
15
-
16
14
  def initialize(container, content_type, extensions) # :nodoc:
17
15
  @container = container
18
16
  self.content_type = content_type
19
17
  self.extensions = extensions
20
18
  end
21
19
 
22
- def friendly(*) # :nodoc:
23
- @container.send(:load_friendly) unless defined?(@friendly)
24
- super if @friendly
25
- end
20
+ def self.column(*methods, file: nil) # :nodoc:
21
+ file = methods.first unless file
26
22
 
27
- def encoding # :nodoc:
28
- @container.send(:load_encoding) unless defined?(@encoding)
29
- @encoding
23
+ file_method = :"load_#{file}"
24
+ methods.each do |m|
25
+ define_method m do |*args|
26
+ @container.send(file_method)
27
+ super(*args)
28
+ end
29
+ end
30
30
  end
31
31
 
32
- def docs # :nodoc:
33
- @container.send(:load_docs) unless defined?(@docs)
34
- @docs
35
- end
32
+ column :friendly
33
+ column :encoding, :encoding=
34
+ column :docs, :docs=
35
+ column :preferred_extension, :preferred_extension=
36
+ column :obsolete, :obsolete=, :obsolete?, :registered, :registered=,
37
+ :registered?, :signature, :signature=, :signature?, file: 'flags'
38
+ column :xrefs, :xrefs=, :xref_urls
39
+ column :use_instead, :use_instead=
36
40
 
37
- def obsolete? # :nodoc:
38
- @container.send(:load_obsolete) unless defined?(@obsolete)
39
- super
40
- end
41
-
42
- def registered? # :nodoc:
43
- @container.send(:load_registered) unless defined?(@registered)
44
- super
45
- end
46
-
47
- def signature? # :nodoc:
48
- @container.send(:load_signature) unless defined?(@signature)
49
- super
50
- end
51
-
52
- def xrefs # :nodoc:
53
- @container.send(:load_xrefs) unless defined?(@xrefs)
54
- @xrefs
55
- end
56
-
57
- def use_instead # :nodoc:
58
- @container.send(:load_use_instead) unless defined?(@use_instead)
59
- @use_instead
60
- end
61
-
62
- def binary? # :nodoc:
63
- @container.send(:load_encoding) unless defined?(@encoding)
64
- super
65
- end
66
-
67
- def to_a # :nodoc:
68
- @container.send(:load_encoding) unless defined?(@encoding)
69
- @container.send(:load_docs) unless defined?(@docs)
70
- super
71
- end
72
-
73
- def to_hash # :nodoc:
74
- @container.send(:load_encoding) unless defined?(@encoding)
75
- @container.send(:load_docs) unless defined?(@docs)
41
+ def encode_with(coder) # :nodoc:
42
+ @container.send(:load_friendly)
43
+ @container.send(:load_encoding)
44
+ @container.send(:load_docs)
45
+ @container.send(:load_flags)
46
+ @container.send(:load_use_instead)
47
+ @container.send(:load_xrefs)
48
+ @container.send(:load_preferred_extension)
76
49
  super
77
50
  end
78
51
 
79
- def encode_with(coder) # :nodoc:
80
- @container.send(:load_friendly) unless defined?(@friendly)
81
- @container.send(:load_encoding) unless defined?(@encoding)
82
- @container.send(:load_docs) unless defined?(@docs)
83
- @container.send(:load_obsolete) unless defined?(@obsolete)
84
- @container.send(:load_use_instead) unless defined?(@use_instead)
85
- @container.send(:load_xrefs) unless defined?(@xrefs)
86
- @container.send(:load_registered) unless defined?(@registered)
87
- @container.send(:load_signature) unless defined?(@signature)
88
- super
52
+ class << self
53
+ undef column
89
54
  end
90
55
  end
@@ -1,6 +1,12 @@
1
- # -*- ruby encoding: utf-8 -*-
1
+ ##
2
+ module MIME
3
+ ##
4
+ class Types
5
+ end
6
+ end
2
7
 
3
- require 'mime/types/deprecations'
8
+ require 'mime/types/logger'
9
+ require 'mime/types/container'
4
10
  require 'mime/type'
5
11
  require 'mime/types/cache'
6
12
  require 'mime/types/loader'
@@ -66,37 +72,19 @@ class MIME::Types
66
72
 
67
73
  include Enumerable
68
74
 
69
- # The data version.
70
- attr_reader :data_version
71
-
72
75
  # Creates a new MIME::Types registry.
73
76
  def initialize
74
77
  @type_variants = Container.new
75
78
  @extension_index = Container.new
76
- @data_version = VERSION.dup.freeze
77
- end
78
-
79
- # This method is deprecated and will be removed in mime-types 3.0.
80
- def add_type_variant(mime_type) # :nodoc:
81
- MIME::Types.deprecated(self, __method__, :private)
82
- add_type_variant!(mime_type)
83
- end
84
-
85
- # This method is deprecated and will be removed in mime-types 3.0.
86
- def index_extensions(mime_type) # :nodoc:
87
- MIME::Types.deprecated(self, __method__, :private)
88
- index_extensions!(mime_type)
89
- end
90
-
91
- # This method is deprecated and will be removed in mime-types 3.0.
92
- def defined_types # :nodoc:
93
- MIME::Types.deprecated(self, __method__)
94
- @type_variants.values.flatten
95
79
  end
96
80
 
97
81
  # Returns the number of known type variants.
98
82
  def count
99
- @type_variants.values.reduce(0) { |m, o| m + o.size }
83
+ @type_variants.values.inject(0) { |a, e| a + e.size }
84
+ end
85
+
86
+ def inspect # :nodoc:
87
+ "#<#{self.class}: #{count} variants, #{@extension_index.count} extensions>"
100
88
  end
101
89
 
102
90
  # Iterates through the type variants.
@@ -108,7 +96,7 @@ class MIME::Types
108
96
  end
109
97
  end
110
98
 
111
- @__types__ = nil
99
+ @__types__ = nil
112
100
 
113
101
  # Returns a list of MIME::Type objects, which may be empty. The optional
114
102
  # flag parameters are <tt>:complete</tt> (finds only complete MIME::Type
@@ -135,13 +123,7 @@ class MIME::Types
135
123
  # without;
136
124
  # 5. Obsolete definitions use-instead clauses are compared.
137
125
  # 6. Sort on name.
138
- #
139
- # The previously supported (but deprecated) <tt>:platform</tt> flag is now ignored.
140
- def [](type_id, flags = {})
141
- if flags.key?(:platform)
142
- MIME::Types.deprecated(self, __method__, 'using the :platform flag')
143
- end
144
-
126
+ def [](type_id, complete: false, registered: false)
145
127
  matches = case type_id
146
128
  when MIME::Type
147
129
  @type_variants[type_id.simplified]
@@ -151,7 +133,9 @@ class MIME::Types
151
133
  @type_variants[MIME::Type.simplified(type_id)]
152
134
  end
153
135
 
154
- prune_matches(matches, flags).sort { |a, b| a.priority_compare(b) }
136
+ prune_matches(matches, complete, registered).sort { |a, b|
137
+ a.priority_compare(b)
138
+ }
155
139
  end
156
140
 
157
141
  # Return the list of MIME::Types which belongs to the file based on its
@@ -166,17 +150,12 @@ class MIME::Types
166
150
  # => [image/gif]
167
151
  # puts MIME::Types.type_for(%w(citydesk.xml citydesk.gif))
168
152
  # => [application/xml, image/gif, text/xml]
169
- #
170
- # The deprecated +platform+ flag is ignored.
171
- def type_for(filename, platform = :deprecated)
172
- types = Array(filename).flat_map { |fn|
153
+ def type_for(filename)
154
+ Array(filename).flat_map { |fn|
173
155
  @extension_index[fn.chomp.downcase[/\.?([^.]*?)$/, 1]]
174
- }.compact.sort { |a, b| a.priority_compare(b) }.uniq
175
-
176
- unless platform == :deprecated
177
- MIME::Types.deprecated(self, __method__, 'using the platform parameter')
178
- end
179
- types
156
+ }.compact.inject(:+).sort { |a, b|
157
+ a.priority_compare(b)
158
+ }
180
159
  end
181
160
  alias_method :of, :type_for
182
161
 
@@ -194,7 +173,7 @@ class MIME::Types
194
173
  nil
195
174
  when MIME::Types
196
175
  variants = mime_type.instance_variable_get(:@type_variants)
197
- add(*variants.values.flatten, quiet)
176
+ add(*variants.values.inject(:+).to_a, quiet)
198
177
  when Array
199
178
  add(*mime_type, quiet)
200
179
  else
@@ -217,110 +196,33 @@ Type #{type} is already registered as a variant of #{type.simplified}.
217
196
  index_extensions!(type)
218
197
  end
219
198
 
220
- class << self
221
- include Enumerable
222
-
223
- # Load MIME::Types from a v1 file registry.
224
- #
225
- # This method has been deprecated and will be removed in mime-types 3.0.
226
- def load_from_file(filename)
227
- MIME::Types.deprecated(self, __method__)
228
- MIME::Types::Loader.load_from_v1(filename)
229
- end
230
-
231
- # MIME::Types#[] against the default MIME::Types registry.
232
- def [](type_id, flags = {})
233
- __types__[type_id, flags]
234
- end
235
-
236
- # MIME::Types#count against the default MIME::Types registry.
237
- def count
238
- __types__.count
239
- end
240
-
241
- # MIME::Types#each against the default MIME::Types registry.
242
- def each
243
- if block_given?
244
- __types__.each { |t| yield t }
245
- else
246
- enum_for(:each)
247
- end
248
- end
249
-
250
- # MIME::Types#type_for against the default MIME::Types registry.
251
- def type_for(filename, platform = :deprecated)
252
- __types__.type_for(filename, platform)
253
- end
254
- alias_method :of, :type_for
255
-
256
- # MIME::Types#add against the default MIME::Types registry.
257
- def add(*types)
258
- __types__.add(*types)
259
- end
260
-
261
- # Returns the currently defined cache file, if any.
262
- #
263
- # This method has been deprecated and will be removed in mime-types 3.0.
264
- def cache_file
265
- MIME::Types.deprecated(self, __method__)
266
- ENV['RUBY_MIME_TYPES_CACHE']
267
- end
268
-
269
- def add_type_variant(mime_type) # :nodoc:
270
- __types__.add_type_variant(mime_type)
271
- end
272
-
273
- def index_extensions(mime_type) # :nodoc:
274
- __types__.index_extensions(mime_type)
275
- end
276
-
277
- private
278
-
279
- def lazy_load?
280
- (lazy = ENV['RUBY_MIME_TYPES_LAZY_LOAD']) && (lazy != 'false')
281
- end
282
-
283
- def __types__
284
- (defined?(@__types__) and @__types__) or load_default_mime_types
285
- end
286
-
287
- unless private_method_defined?(:load_mode)
288
- def load_mode
289
- {}
290
- end
291
- end
292
-
293
- def load_default_mime_types(mode = load_mode())
294
- @__types__ = MIME::Types::Cache.load
295
- unless @__types__
296
- @__types__ = MIME::Types::Loader.load(mode)
297
- MIME::Types::Cache.save(@__types__)
298
- end
299
- @__types__
300
- end
301
- end
302
-
303
199
  private
304
200
 
305
201
  def add_type_variant!(mime_type)
306
202
  @type_variants[mime_type.simplified] << mime_type
307
203
  end
308
204
 
205
+ def reindex_extensions!(mime_type)
206
+ return unless @type_variants[mime_type.simplified].include?(mime_type)
207
+ index_extensions!(mime_type)
208
+ end
209
+
309
210
  def index_extensions!(mime_type)
310
211
  mime_type.extensions.each { |ext| @extension_index[ext] << mime_type }
311
212
  end
312
213
 
313
- def prune_matches(matches, flags)
314
- matches.delete_if { |e| !e.complete? } if flags[:complete]
315
- matches.delete_if { |e| !e.registered? } if flags[:registered]
214
+ def prune_matches(matches, complete, registered)
215
+ matches.delete_if { |e| !e.complete? } if complete
216
+ matches.delete_if { |e| !e.registered? } if registered
316
217
  matches
317
218
  end
318
219
 
319
220
  def match(pattern)
320
- @type_variants.select { |k, _| k =~ pattern }.values.flatten
221
+ @type_variants.select { |k, _|
222
+ k =~ pattern
223
+ }.values.inject(:+)
321
224
  end
322
-
323
- load_default_mime_types(load_mode) unless lazy_load?
324
225
  end
325
226
 
326
- # vim: ft=ruby
227
+ require 'mime/types/columnar' unless defined?(MIME::Types::Columnar)
228
+ require 'mime/types/registry'
@@ -1,80 +1,56 @@
1
- # -*- ruby encoding: utf-8 -*-
2
-
3
- class MIME::Types
4
- Cache = Struct.new(:version, :data) # :nodoc:
5
-
6
- # Caching of MIME::Types registries is advisable if you will be loading
7
- # the default registry relatively frequently. With the class methods on
8
- # MIME::Types::Cache, any MIME::Types registry can be marshaled quickly
9
- # and easily.
10
- #
11
- # The cache is invalidated on a per-version basis; a cache file for
12
- # version 2.0 will not be reused with version 2.0.1.
13
- class Cache
14
- class << self
15
- # Attempts to load the cache from the file provided as a parameter or in
16
- # the environment variable +RUBY_MIME_TYPES_CACHE+. Returns +nil+ if the
17
- # file does not exist, if the file cannot be loaded, or if the data in
18
- # the cache version is different than this version.
19
- def load(cache_file = nil)
20
- cache_file ||= ENV['RUBY_MIME_TYPES_CACHE']
21
- return nil unless cache_file and File.exist?(cache_file)
22
-
23
- cache = Marshal.load(File.binread(cache_file))
24
- if cache.version == MIME::Types::VERSION
25
- Marshal.load(cache.data)
26
- else
27
- MIME::Types.logger.warn <<-warning.chomp
1
+ MIME::Types::Cache = Struct.new(:version, :data) # :nodoc:
2
+
3
+ # Caching of MIME::Types registries is advisable if you will be loading
4
+ # the default registry relatively frequently. With the class methods on
5
+ # MIME::Types::Cache, any MIME::Types registry can be marshaled quickly
6
+ # and easily.
7
+ #
8
+ # The cache is invalidated on a per-data-version basis; a cache file for
9
+ # version 3.2015.1118 will not be reused with version 3.2015.1201.
10
+ class << MIME::Types::Cache
11
+ # Attempts to load the cache from the file provided as a parameter or in
12
+ # the environment variable +RUBY_MIME_TYPES_CACHE+. Returns +nil+ if the
13
+ # file does not exist, if the file cannot be loaded, or if the data in
14
+ # the cache version is different than this version.
15
+ def load(cache_file = nil)
16
+ cache_file ||= ENV['RUBY_MIME_TYPES_CACHE']
17
+ return nil unless cache_file and File.exist?(cache_file)
18
+
19
+ cache = Marshal.load(File.binread(cache_file))
20
+ if cache.version == MIME::Types::Data::VERSION
21
+ Marshal.load(cache.data)
22
+ else
23
+ MIME::Types.logger.warn <<-warning.chomp
28
24
  Could not load MIME::Types cache: invalid version
29
- warning
30
- nil
31
- end
32
- rescue => e
33
- MIME::Types.logger.warn <<-warning.chomp
34
- Could not load MIME::Types cache: #{e}
35
- warning
36
- return nil
37
- end
38
-
39
- # Attempts to save the types provided to the cache file provided.
40
- #
41
- # If +types+ is not provided or is +nil+, the cache will contain the
42
- # current MIME::Types default registry.
43
- #
44
- # If +cache_file+ is not provided or is +nil+, the cache will be written
45
- # to the file specified in the environment variable
46
- # +RUBY_MIME_TYPES_CACHE+. If there is no cache file specified either
47
- # directly or through the environment, this method will return +nil+
48
- def save(types = nil, cache_file = nil)
49
- cache_file ||= ENV['RUBY_MIME_TYPES_CACHE']
50
- return nil unless cache_file
51
-
52
- types ||= MIME::Types.send(:__types__)
53
-
54
- File.open(cache_file, 'wb') do |f|
55
- f.write(Marshal.dump(new(types.data_version, Marshal.dump(types))))
56
- end
57
- end
25
+ warning
26
+ nil
58
27
  end
28
+ rescue => e
29
+ MIME::Types.logger.warn <<-warning.chomp
30
+ Could not load MIME::Types cache: #{e}
31
+ warning
32
+ return nil
59
33
  end
60
34
 
61
- # MIME::Types requires a container Hash with a default values for keys
62
- # resulting in an empty array (<tt>[]</tt>), but this cannot be dumped
63
- # through Marshal because of the presence of that default Proc. This class
64
- # exists solely to satisfy that need.
65
- class Container < Hash # :nodoc:
66
- def initialize
67
- super
68
- self.default_proc = ->(h, k) { h[k] = [] }
69
- end
70
-
71
- def marshal_dump
72
- {}.merge(self)
73
- end
74
-
75
- def marshal_load(hash)
76
- self.default_proc = ->(h, k) { h[k] = [] }
77
- self.merge!(hash)
35
+ # Attempts to save the types provided to the cache file provided.
36
+ #
37
+ # If +types+ is not provided or is +nil+, the cache will contain the
38
+ # current MIME::Types default registry.
39
+ #
40
+ # If +cache_file+ is not provided or is +nil+, the cache will be written
41
+ # to the file specified in the environment variable
42
+ # +RUBY_MIME_TYPES_CACHE+. If there is no cache file specified either
43
+ # directly or through the environment, this method will return +nil+
44
+ def save(types = nil, cache_file = nil)
45
+ cache_file ||= ENV['RUBY_MIME_TYPES_CACHE']
46
+ return nil unless cache_file
47
+
48
+ types ||= MIME::Types.send(:__types__)
49
+
50
+ File.open(cache_file, 'wb') do |f|
51
+ f.write(
52
+ Marshal.dump(new(MIME::Types::Data::VERSION, Marshal.dump(types)))
53
+ )
78
54
  end
79
55
  end
80
56
  end