Skip to content

Commit

Permalink
Cleaning up some code climate issues.
Browse files Browse the repository at this point in the history
  • Loading branch information
halostatue committed Apr 30, 2013
1 parent 04a6694 commit be28c7f
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 55 deletions.
7 changes: 7 additions & 0 deletions History.rdoc
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
== 1.24 / YYYY-MM-DD

* Code Climate:
* Working on improving the quality of the mime-types codebase through the use
of Code Climate. https://codeclimate.com/github/halostatue/mime-types
* Simplified MIME::Type.from_array to make more assumptions about assignment.

== 1.23 / 2013-04-20

* New Feature:
Expand Down
6 changes: 2 additions & 4 deletions README.rdoc
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ home :: http://mime-types.rubyforge.org/
code :: https://github.com/halostatue/mime-types/
bugs :: https://github.com/halostatue/mime-types/issues
rdoc :: http://mime-types.rubyforge.org/
code climate :: {<img src="https://codeclimate.com/github/halostatue/mime-types.png" />}[https://codeclimate.com/github/halostatue/mime-types]
continuous integration :: {<img src="https://travis-ci.org/halostatue/mime-types.png" />}[https://travis-ci.org/halostatue/mime-types]

== Description

Expand Down Expand Up @@ -54,10 +56,6 @@ files). A MIME::Type stores the known information about one MIME type.

puts MIME::Types.any? { |type| type.content_type == 'text/plain' }

== Continuous Integration Status

{<img src="https://travis-ci.org/halostatue/mime-types.png" />}[https://travis-ci.org/halostatue/mime-types]

:include: Contributing.rdoc

:include: Licence.rdoc
88 changes: 38 additions & 50 deletions lib/mime/types.rb
Original file line number Diff line number Diff line change
Expand Up @@ -74,39 +74,35 @@ def <=>(other)
#
# 1. self.simplified <=> other.simplified (ensures that we
# don't try to compare different types)
# 2. IANA-registered definitions > other definitions.
# 3. Generic definitions > platform definitions.
# 3. Complete definitions > incomplete definitions.
# 4. Current definitions > obsolete definitions.
# 5. Obselete with use-instead references > obsolete without.
# 2. IANA-registered definitions < other definitions.
# 3. Generic definitions < platform definitions.
# 3. Complete definitions < incomplete definitions.
# 4. Current definitions < obsolete definitions.
# 5. Obselete with use-instead references < obsolete without.
# 6. Obsolete use-instead definitions are compared.
def priority_compare(other)
pc = simplified <=> other.simplified

if pc.zero? and registered? != other.registered?
pc = registered? ? -1 : 1
end

if pc.zero? and platform? != other.platform?
pc = platform? ? 1 : -1
end

if pc.zero? and complete? != other.complete?
pc = complete? ? -1 : 1
end

if pc.zero? and obsolete? != other.obsolete?
pc = obsolete? ? 1 : -1
end

if pc.zero? and obsolete? and (use_instead != other.use_instead)
pc = if use_instead.nil?
-1
elsif other.use_instead.nil?
1
else
use_instead <=> other.use_instead
if pc.zero?
pc = if registered? != other.registered?
registered? ? -1 : 1 # registered < unregistered
elsif platform? != other.platform?
platform? ? 1 : -1 # generic < platform
elsif complete? != other.complete?
pc = complete? ? -1 : 1 # complete < incomplete
elsif obsolete? != other.obsolete?
pc = obsolete? ? 1 : -1 # current < obsolete
end

if pc.zero? and obsolete? and (use_instead != other.use_instead)
pc = if use_instead.nil?
-1
elsif other.use_instead.nil?
1
else
use_instead <=> other.use_instead
end
end
end

pc
Expand Down Expand Up @@ -317,23 +313,17 @@ def simplified(content_type)
# end
def from_array(*args) #:yields MIME::Type.new:
# Dereferences the array one level, if necessary.
args = args[0] if args[0].kind_of?(Array)

if args.size.between?(1, 8)
m = MIME::Type.new(args[0]) do |t|
t.extensions = args[1] if args.size > 1
t.encoding = args[2] if args.size > 2
t.system = args[3] if args.size > 3
t.obsolete = args[4] if args.size > 4
t.docs = args[5] if args.size > 5
t.url = args[6] if args.size > 6
t.registered = args[7] if args.size > 7
end
yield m if block_given?
else
args = args.first if args.first.kind_of? Array

unless args.size.between?(1, 8)
raise ArgumentError, "Array provided must contain between one and eight elements."
end
m

MIME::Type.new(args.shift) do |t|
t.extensions, t.encoding, t.system, t.obsolete, t.docs, t.url,
t.registered = *args
yield t if block_given?
end
end

# Creates a MIME::Type from a hash. Keys are case-insensitive,
Expand Down Expand Up @@ -364,18 +354,17 @@ def from_hash(hash) #:yields MIME::Type.new:
type[k.to_s.tr('A-Z', 'a-z').gsub(/-/, '_').to_sym] = v
end

m = MIME::Type.new(type[:content_type]) do |t|
MIME::Type.new(type[:content_type]) do |t|
t.extensions = type[:extensions]
t.encoding = type[:content_transfer_encoding]
t.system = type[:system]
t.obsolete = type[:obsolete]
t.docs = type[:docs]
t.url = type[:url]
t.registered = type[:registered]
end

yield m if block_given?
m
yield t if block_given?
end
end

# Essentially a copy constructor.
Expand All @@ -390,7 +379,7 @@ def from_hash(hash) #:yields MIME::Type.new:
# t.encoding = plaintext.encoding.dup
# end
def from_mime_type(mime_type) #:yields the new MIME::Type:
m = MIME::Type.new(mime_type.content_type.dup) do |t|
MIME::Type.new(mime_type.content_type.dup) do |t|
t.extensions = mime_type.extensions.map { |e| e.dup }
t.url = mime_type.url && mime_type.url.map { |e| e.dup }

Expand All @@ -402,9 +391,8 @@ def from_mime_type(mime_type) #:yields the new MIME::Type:

mime_type.docs && t.docs = mime_type.docs.dup

yield t if block_given?
end

yield m if block_given?
end
end

Expand Down
2 changes: 1 addition & 1 deletion mime-types.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ Gem::Specification.new do |s|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
s.authors = ["Austin Ziegler"]
s.cert_chain = ["/Users/AZiegler/.gem/gem-public_cert.pem"]
s.date = "2013-04-21"
s.date = "2013-04-30"
s.description = "This library allows for the identification of a file's likely MIME content\ntype. This is release 1.23 that adds the ability to enumerate over the\ncollection of MIME types and updates the sources of a few MIME types. The\nidentification of MIME content type is based on a file's filename extensions.\n\nMIME types are used in MIME-compliant communications, as in e-mail or HTTP\ntraffic, to indicate the type of content which is transmitted. MIME::Types\nprovides the ability for detailed information about MIME entities (provided as\na set of MIME::Type objects) to be determined and used programmatically. There\nare many types defined by RFCs and vendors, so the list is long but not\ncomplete; don't hesitate to ask to add additional information. This library\nfollows the IANA collection of MIME types (see below for reference).\n\nMIME::Types for Ruby was originally based on and synchronized with MIME::Types\nfor Perl by Mark Overmeer, copyright 2001 - 2009. As of version 1.15, the data\nformat for the MIME::Type list has changed and the synchronization will no\nlonger happen.\n\nMIME::Types is built to conform to the MIME types of RFCs 2045 and 2231. It\nfollows the official {IANA registry}[http://www.iana.org/assignments/media-types/]\n({ftp}[ftp://ftp.iana.org/assignments/media-types]) with some unofficial types\nadded from the the {LTSW collection}[http://www.ltsw.se/knbase/internet/mime.htp]."
s.email = ["[email protected]"]
s.extra_rdoc_files = ["Contributing.rdoc", "History.rdoc", "Licence.rdoc", "Manifest.txt", "README.rdoc", "docs/COPYING.txt", "docs/artistic.txt", "Contributing.rdoc", "History.rdoc", "Licence.rdoc", "README.rdoc"]
Expand Down

0 comments on commit be28c7f

Please sign in to comment.