Class: RDF::Format Abstract
Overview
The base class for RDF serialization formats.
Direct Known Subclasses
Class Method Summary collapse
-
.cli_commands ⇒ Hash{Symbol => {description: String, lambda: Lambda(Array, Hash)}}
Hash of CLI commands appropriate for this format.
-
.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_extension ⇒ Array<String>
Retrieves or defines file extensions for this RDF serialization format.
-
.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.
-
.name ⇒ Symbol
Returns a human-readable name for the format.
-
.reader(klass = nil, &block) ⇒ void
(also: reader_class)
Retrieves or defines the reader class for this RDF serialization format.
-
.reader_symbols ⇒ Array<Symbol>
Returns the set of format symbols for available RDF::Reader subclasses.
-
.reader_types ⇒ Array<String>
Returns the set of content types for available RDF::Reader subclasses.
-
.symbols ⇒ Array<Symbol>
Returns the set of symbols for a writer appropriate for use with with ‘RDF::Format.for()`.
-
.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.
-
.writer_symbols ⇒ Array<Symbol>
Returns the set of format symbols for available RDF::Writer subclasses.
-
.writer_types ⇒ Array<String>
Returns the set of content types for available RDF::Writer subclasses.
Class Method Details
.cli_commands ⇒ Hash{Symbol => {description: String, lambda: Lambda(Array, Hash)}}
Hash of CLI commands appropriate for this format
365 366 367 |
# File 'lib/rdf/format.rb', line 365 def self.cli_commands {} end |
.content_type(type, options) ⇒ void .content_type ⇒ Array<String>
Retrieves or defines MIME content types for this RDF serialization format.
418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 |
# File 'lib/rdf/format.rb', line 418 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] ||= [] @@content_types[type] << self unless @@content_types[type].include?(self) if extensions = ([:extension] || [:extensions]) extensions = Array(extensions).map(&:to_sym) extensions.each do |ext| @@file_extensions[ext] ||= [] @@file_extensions[ext] << self unless @@file_extensions[ext].include?(self) end end if aliases = ([:alias] || [:aliases]) aliases = Array(aliases).each do |a| @@content_types[a] ||= [] @@content_types[a] << self unless @@content_types[a].include?(self) end end end end |
.content_types ⇒ Hash{String => Array<Class>}
Returns MIME content types for known RDF serialization formats.
168 169 170 |
# File 'lib/rdf/format.rb', line 168 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.
382 383 384 |
# File 'lib/rdf/format.rb', line 382 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_extension ⇒ Array<String>
Retrieves or defines file extensions for this RDF serialization format.
The return is an array where the first element is the cannonical file extension for the format and following elements are alias file extensions.
450 451 452 |
# File 'lib/rdf/format.rb', line 450 def self.file_extension @@file_extensions.map {|ext, formats| ext if formats.include?(self)}.compact end |
.file_extensions ⇒ Hash{Symbol => Array<Class>}
Returns file extensions for known RDF serialization formats.
180 181 182 |
# File 'lib/rdf/format.rb', line 180 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. If multiple formats are identified, the last one found is returned; this allows descrimination of equivalent formats based on load order.
91 92 93 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 152 153 154 155 156 157 158 |
# File 'lib/rdf/format.rb', line 91 def self.for( = {}) format = case when String, RDF::URI # Find a format based on the file name fn, = , {} self.for(file_name: fn) { yield if block_given? } 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 # Ignore text/plain, a historical encoding for N-Triples, which is # problematic in format detection, as many web servers will serve # content by default text/plain. content_types[mime_type] unless mime_type == 'text/plain' && ([:sample] || block_given?) # Find a format based on the file name: when file_name = [:file_name] self.for(file_extension: File.extname(RDF::URI(file_name).path)[1..-1]) { yield if block_given? } # Find a format based on the file extension: when file_ext = [:file_extension] file_extensions[file_ext.to_sym] end when Symbol # Try to find a match based on the full class name # We want this to work even if autoloading fails fmt, = , {} classes = @@subclasses.select { |klass| klass.symbols.include?(fmt) } if classes.empty? classes = case fmt when :ntriples then [RDF::NTriples::Format] when :nquads then [RDF::NQuads::Format] else [] end end classes 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.last 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?) sample = sample.dup.to_s sample.force_encoding(Encoding::ASCII_8BIT) if sample.respond_to?(:force_encoding) # Given a sample, perform format detection across the appropriate formats, choosing the last that matches format ||= @@subclasses # Return last format that has a positive detection format.reverse.detect {|f| f.detect(sample)} || format.last elsif format.is_a?(Array) # Otherwise, just return the last matching format format.last else nil end end |
.name ⇒ Symbol
Returns a human-readable name for the format. Subclasses should override this to use something difererent than the Class name.
269 270 271 272 273 274 |
# File 'lib/rdf/format.rb', line 269 def self.name elements = self.to_s.split("::") name = elements.pop name = elements.pop if name == 'Format' name.to_s 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.
306 307 308 309 310 311 312 313 314 315 316 317 |
# File 'lib/rdf/format.rb', line 306 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 |
.reader_symbols ⇒ Array<Symbol>
Returns the set of format symbols for available RDF::Reader subclasses.
193 194 195 |
# File 'lib/rdf/format.rb', line 193 def self.reader_symbols @@readers.keys.map(&:symbols).flatten.uniq end |
.reader_types ⇒ Array<String>
Returns the set of content types for available RDF::Reader subclasses.
206 207 208 |
# File 'lib/rdf/format.rb', line 206 def self.reader_types reader_symbols.flat_map {|s| RDF::Format.for(s).content_type}.uniq end |
.symbols ⇒ Array<Symbol>
Individual formats can override this to provide an array of symbols; otherwise, it uses ‘self.to_sym`
Returns the set of symbols for a writer appropriate for use with with ‘RDF::Format.for()`
255 256 257 |
# File 'lib/rdf/format.rb', line 255 def self.symbols [self.to_sym] end |
.to_sym ⇒ Symbol
Defaults to the last element of the class name before ‘Format` downcased and made a symbol. Individual formats can override this.
Returns a symbol appropriate to use with ‘RDF::Format.for()`
241 242 243 244 245 246 |
# File 'lib/rdf/format.rb', line 241 def self.to_sym elements = self.to_s.split("::") sym = elements.pop sym = elements.pop if sym == 'Format' sym.downcase.to_s.to_sym if sym.is_a?(String) 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.
349 350 351 352 353 354 355 356 357 358 359 360 |
# File 'lib/rdf/format.rb', line 349 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 |
.writer_symbols ⇒ Array<Symbol>
Returns the set of format symbols for available RDF::Writer subclasses.
219 220 221 |
# File 'lib/rdf/format.rb', line 219 def self.writer_symbols @@writers.keys.map(&:symbols).flatten.uniq end |
.writer_types ⇒ Array<String>
Returns the set of content types for available RDF::Writer subclasses.
232 233 234 |
# File 'lib/rdf/format.rb', line 232 def self.writer_types writer_symbols.flat_map {|s| RDF::Format.for(s).content_type}.uniq end |