Permalink
Browse files

More checking in Format.for, include :has_reader and :has_writer opti…

…ons so that the appropriate format is used. Also guard against format detection finding no formats.
  • Loading branch information...
1 parent 2f48416 commit 41fb613cf187bfc844e1668ed5cb818c658bf51d @gkellogg committed Nov 24, 2011
Showing with 15 additions and 2 deletions.
  1. +13 −2 lib/rdf/format.rb
  2. +1 −0 lib/rdf/reader.rb
  3. +1 −0 lib/rdf/writer.rb
View
@@ -78,6 +78,10 @@ def self.each(&block)
# @option options [Symbol, #to_sym] :file_extension (nil)
# @option options [String, #to_s] :content_type (nil)
# Note that content_type will be taken from a URL opened using {RDF::Util::File.open_file}.
+ # @option options [Boolean] :has_reader (false)
+ # Only return a format having a reader.
+ # @option options [Boolean] :has_writer (false)
+ # Only return a format having a writer.
# @option options [String] :sample (nil)
# A sample of input used for performing format detection.
# If we find no formats, or we find more than one, and we have a sample, we can
@@ -100,7 +104,7 @@ def self.for(options = {})
# @see http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.17
# @see http://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html#sec3.7
mime_type = mime_type.to_s
- mime_type = mime_type.split(';').first if mime_type.include?(?;) # remove any media type parameters
+ mime_type = mime_type.split(';').first # remove any media type parameters
content_types[mime_type]
# Find a format based on the file name:
when file_name = options[:file_name]
@@ -127,7 +131,11 @@ def self.for(options = {})
end
end
+
if format.is_a?(Array)
+ format = format.select {|f| f.reader} if options[:has_reader]
+ format = format.select {|f| f.writer} if options[:has_writer]
+
return format.first if format.uniq.length == 1
elsif !format.nil?
return format
@@ -140,7 +148,10 @@ def self.for(options = {})
format ||= @@subclasses
# Return first format that has a positive detection
- format.detect {|f| f.detect(sample)}
+ format.detect {|f| f.detect(sample)} || format.first
+ else
+ # Otherwise, just return the first matching format
+ format.first
end
end
View
@@ -87,6 +87,7 @@ def self.each(&block)
#
# @return [Class]
def self.for(options = {}, &block)
+ options = options.merge(:has_reader => true) if options.is_a?(Hash)
if format = self.format || Format.for(options, &block)
format.reader
end
View
@@ -77,6 +77,7 @@ def self.each(&block)
#
# @return [Class]
def self.for(options = {})
+ options = options.merge(:has_writer => true) if options.is_a?(Hash)
if format = self.format || Format.for(options)
format.writer
end

0 comments on commit 41fb613

Please sign in to comment.