lib/mini_mime.rb in mini_mime-0.1.2 vs lib/mini_mime.rb in mini_mime-0.1.3
- old
+ new
@@ -1,10 +1,9 @@
require "mini_mime/version"
require "thread"
module MiniMime
-
def self.lookup_by_filename(filename)
Db.lookup_by_filename(filename)
end
def self.lookup_by_content_type(mime)
@@ -13,12 +12,13 @@
class Info
BINARY_ENCODINGS = %w(base64 8bit)
attr_accessor :extension, :content_type, :encoding
+
def initialize(buffer)
- @extension,@content_type,@encoding = buffer.split(/\s+/).map!(&:freeze)
+ @extension, @content_type, @encoding = buffer.split(/\s+/).map!(&:freeze)
end
def [](idx)
if idx == 0
@extension
@@ -58,32 +58,28 @@
@db ||= new
@db.lookup_by_content_type(content_type)
end
end
-
class Cache
def initialize(size)
@size = size
@hash = {}
end
- def []=(key,val)
+ def []=(key, val)
rval = @hash[key] = val
- if @hash.length > @size
- @hash.shift
- end
+ @hash.shift if @hash.length > @size
rval
end
def fetch(key, &blk)
@hash.fetch(key, &blk)
end
end
class RandomAccessDb
-
MAX_CACHED = 100
def initialize(name, sort_order)
@path = File.expand_path("../db/#{name}", __FILE__)
@file = File.open(@path)
@@ -111,11 +107,11 @@
data
end
end
end
- #lifted from marcandre/backports
+ # lifted from marcandre/backports
def lookup_uncached(val)
from = 0
to = @rows - 1
result = nil
@@ -137,11 +133,10 @@
def resolve(row)
@file.seek(row*@row_length)
Info.new(@file.readline)
end
-
end
def initialize
@ext_db = RandomAccessDb.new("ext_mime.db", 0)
@content_type_db = RandomAccessDb.new("content_type_mime.db", 1)
@@ -152,8 +147,7 @@
end
def lookup_by_content_type(content_type)
@content_type_db.lookup(content_type)
end
-
end
end