Class: RDF::Format Abstract
Overview
The base class for RDF serialization formats.
Direct Known Subclasses
Class Method Summary collapse
-
.content_type(type = nil, options = {}) ⇒ Object
Retrieves or defines MIME content types for this RDF serialization format.
-
.content_types ⇒ Hash{String => Array<Class>}
Returns MIME content types for known RDF serialization formats.
-
.detect(sample) ⇒ Boolean
Use a text sample to detect the format of an input file.
-
.each {|klass| ... } ⇒ Enumerator
Enumerates known RDF serialization format classes.
-
.file_extensions ⇒ Hash{Symbol => Array<Class>}
Returns file extensions for known RDF serialization formats.
-
.for(options = {}) ⇒ Class
Finds an RDF serialization format class based on the given criteria.
-
.reader(klass = nil, &block) ⇒ void
(also: reader_class)
Retrieves or defines the reader class for this RDF serialization format.
-
.to_sym ⇒ Symbol
Returns a symbol appropriate to use with RDF::Format.for().
-
.writer(klass = nil, &block) ⇒ void
(also: writer_class)
Retrieves or defines the writer class for this RDF serialization format.
Class Method Details
.content_type(type, options) ⇒ void .content_type ⇒ Array<String>
Retrieves or defines MIME content types for this RDF serialization format.
315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 |
# File 'lib/rdf/format.rb', line 315 def self.content_type(type = nil, = {}) if type.nil? [@@content_type[self], @@content_types.map { |ct, cl| (cl.include?(self) && ct != @@content_type[self]) ? ct : nil }].flatten.compact else @@content_type[self] = type (@@content_types[type] ||= []) << self if extensions = ([:extension] || [:extensions]) extensions = [extensions].flatten.map(&:to_sym) extensions.each { |ext| (@@file_extensions[ext] ||= []) << self } end if aliases = ([:alias] || [:aliases]) aliases = [aliases].flatten.each { |a| (@@content_types[a] ||= []) << self } end end end |
.content_types ⇒ Hash{String => Array<Class>}
Returns MIME content types for known RDF serialization formats.
157 158 159 |
# File 'lib/rdf/format.rb', line 157 def self.content_types @@content_types end |
.detect(sample) ⇒ Boolean
Use a text sample to detect the format of an input file. Sub-classes implement a matcher sufficient to detect probably format matches, including disambiguating between other similar formats.
Used to determine format class from loaded formats by for when a match cannot be unambigiously found otherwise.
279 280 281 |
# File 'lib/rdf/format.rb', line 279 def self.detect(sample) false end |
.each {|klass| ... } ⇒ Enumerator
Enumerates known RDF serialization format classes.
54 55 56 |
# File 'lib/rdf/format.rb', line 54 def self.each(&block) @@subclasses.each(&block) end |
.file_extensions ⇒ Hash{Symbol => Array<Class>}
Returns file extensions for known RDF serialization formats.
165 166 167 |
# File 'lib/rdf/format.rb', line 165 def self.file_extensions @@file_extensions end |
.for(format) ⇒ Class .for(filename) ⇒ Class .for(options = {}) ⇒ Class
Finds an RDF serialization format class based on the given criteria.
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 |
# File 'lib/rdf/format.rb', line 94 def self.for( = {}) format = case when String # Find a format based on the file name self.for(:file_name => ) when Hash case # Find a format based on the MIME content type: when mime_type = [:content_type] # @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 # remove any media type parameters content_types[mime_type] # Find a format based on the file name: when file_name = [:file_name] self.for(:file_extension => File.extname(file_name.to_s)[1..-1]) # Find a format based on the file extension: when file_ext = [:file_extension] file_extensions[file_ext.to_sym] end when Symbol case format = # Special case, since we want this to work despite autoloading when :ntriples RDF::NTriples::Format # For anything else, find a match based on the full class name else @@subclasses.detect { |klass| klass.to_sym == format } end end if format.is_a?(Array) format = format.select {|f| f.reader} if [:has_reader] format = format.select {|f| f.writer} if [:has_writer] return format.first if format.uniq.length == 1 elsif !format.nil? return format end # If we have a sample, use that for format detection if sample = ([:sample] if .is_a?(Hash)) || (yield if block_given?) # Given a sample, perform format detection across the appropriate formats, choosing # the first that matches format ||= @@subclasses # Return first format that has a positive detection format.detect {|f| f.detect(sample)} || format.first elsif format.is_a?(Array) # Otherwise, just return the first matching format format.first else nil end end |
.reader(klass) ⇒ void .reader { ... } ⇒ void .reader ⇒ Class Also known as: reader_class
This method returns an undefined value.
Retrieves or defines the reader class for this RDF serialization format.
209 210 211 212 213 214 215 216 217 218 219 220 |
# File 'lib/rdf/format.rb', line 209 def self.reader(klass = nil, &block) case when klass @@readers[self] = klass when block_given? @@readers[self] = block else klass = @@readers[self] klass = @@readers[self] = klass.call if klass.is_a?(Proc) klass end end |
.to_sym ⇒ Symbol
Returns a symbol appropriate to use with RDF::Format.for()
172 173 174 175 176 177 |
# File 'lib/rdf/format.rb', line 172 def self.to_sym elements = self.to_s.split("::") sym = elements.pop sym = elements.pop if sym == 'Format' sym.downcase.to_s.to_sym end |
.writer(klass) ⇒ void .writer { ... } ⇒ void .writer ⇒ Class Also known as: writer_class
This method returns an undefined value.
Retrieves or defines the writer class for this RDF serialization format.
252 253 254 255 256 257 258 259 260 261 262 263 |
# File 'lib/rdf/format.rb', line 252 def self.writer(klass = nil, &block) case when klass @@writers[self] = klass when block_given? @@writers[self] = block else klass = @@writers[self] klass = @@writers[self] = klass.call if klass.is_a?(Proc) klass end end |