Class: RDF::Format Abstract
- Extended by:
- Enumerable
- Defined in:
- vendor/bundler/ruby/3.3.0/bundler/gems/rdf-884e3ef78084/lib/rdf/format.rb
Overview
The base class for RDF serialization formats.
Direct Known Subclasses
JSON::LD::Format, LD::Patch::Format, Canonicalize::Format, JSON::Format, Microdata::Format, N3::Format, NQuads::Format, NTriples::Format, Normalize::Format, RDFXML::Format, RDFa::Format, Reasoner::Format, Tabular::Format, TriG::Format, TriX::Format, Turtle::Format, Vocabulary::Format, SHACL::Format, ShEx::Format, YAML_LD::Format
Class Method Summary collapse
-
.accept_type ⇒ Array<String>
Returns an array of values appropriate for an Accept header.
-
.accept_types ⇒ Array<String>
Returns the set of content types with quality for available RDF::Reader subclasses.
-
.cli_commands ⇒ Hash{Symbol => {description: String, lambda: Lambda(Array, Hash)}}
Hash of CLI commands appropriate for this format.
-
.content_encoding(encoding = nil)
protected
Defines the content encoding for this RDF serialization 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(file_name: nil, file_extension: nil, content_type: nil, has_reader: false, has_writer: false, sample: nil, all_if_none: true, **options) {|klass| ... } ⇒ Enumerator
Enumerates known RDF serialization format classes.
-
.file_extension ⇒ Array<String>
Retrieves file extensions for this RDF serialization format.
-
.file_extensions ⇒ Hash{Symbol => Array<Class>}
Returns file extensions for known RDF serialization formats.
-
.for(*arg, &block) ⇒ 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)
(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.
-
.require(library)
protected
Defines a required Ruby library for this RDF serialization format.
-
.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()
. -
.uri ⇒ URI
(also: to_uri)
Retrieves any format URI defined for this format..
-
.uris ⇒ Hash{Symbol => URI}
Returns the unique URI for the format.
-
.writer(klass = nil, &block)
(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
.accept_type ⇒ Array<String>
Returns an array of values appropriate for an Accept header.
Same as self.content_type
, if no parameter is given when defined.
535 536 537 |
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-884e3ef78084/lib/rdf/format.rb', line 535 def self.accept_type @@accept_types.map {|t, formats| t if formats.include?(self)}.compact end |
.accept_types ⇒ Array<String>
Returns the set of content types with quality for available RDF::Reader subclasses.
275 276 277 |
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-884e3ef78084/lib/rdf/format.rb', line 275 def self.accept_types reader_symbols.flat_map {|s| RDF::Format.for(s).accept_type}.uniq end |
.cli_commands ⇒ Hash{Symbol => {description: String, lambda: Lambda(Array, Hash)}}
Hash of CLI commands appropriate for this format
434 435 436 |
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-884e3ef78084/lib/rdf/format.rb', line 434 def self.cli_commands {} end |
.content_encoding(encoding = nil) (protected)
This method returns an undefined value.
Defines the content encoding for this RDF serialization format.
When called without an encoding, it returns the currently defined content encoding for this format
584 585 586 587 |
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-884e3ef78084/lib/rdf/format.rb', line 584 def self.content_encoding(encoding = nil) @@content_encoding[self] = encoding.to_sym if encoding @@content_encoding[self] || "utf-8" end |
.content_type(type, options) .content_type ⇒ Array<String>
Retrieves or defines MIME content types for this RDF serialization format.
493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 |
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-884e3ef78084/lib/rdf/format.rb', line 493 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 accept_type, type = type, type.split(';').first @@content_type[self] = type @@content_types[type] ||= [] @@content_types[type] << self unless @@content_types[type].include?(self) @@accept_types[accept_type] ||= [] @@accept_types[accept_type] << self unless @@accept_types[accept_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| aa = a.split(';').first @@accept_types[a] ||= [] @@accept_types[a] << self unless @@accept_types[a].include?(self) @@content_types[aa] ||= [] @@content_types[aa] << self unless @@content_types[aa].include?(self) end end # URI identifying this format if uri = [:uri] @@uris[RDF::URI(uri)] = self end end end |
.content_types ⇒ Hash{String => Array<Class>}
Returns MIME content types for known RDF serialization formats.
211 212 213 |
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-884e3ef78084/lib/rdf/format.rb', line 211 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.
451 452 453 |
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-884e3ef78084/lib/rdf/format.rb', line 451 def self.detect(sample) false end |
.each(file_name: nil, file_extension: nil, content_type: nil, has_reader: false, has_writer: false, sample: nil, all_if_none: true, **options) {|klass| ... } ⇒ Enumerator
Enumerates known RDF serialization format classes.
Given options from for, it returns just those formats that match the specified criteria.
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 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 |
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-884e3ef78084/lib/rdf/format.rb', line 75 def self.each(file_name: nil, file_extension: nil, content_type: nil, has_reader: false, has_writer: false, sample: nil, all_if_none: true, **, &block) formats = case # Find a format based on the MIME content type: when 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 = content_type.to_s.split(';').first.to_s # 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. if (mime_type == 'text/plain' && sample) || mime_type == '*/*' # All content types @@subclasses elsif mime_type.end_with?('/*') # All content types that have the first part of the mime-type as a prefix prefix = mime_type[0..-3] content_types.map do |ct, fmts| ct.start_with?(prefix) ? fmts : [] end.flatten.uniq else content_types[mime_type] end # Find a format based on the file name: when file_name ext = File.extname(RDF::URI(file_name).path.to_s)[1..-1].to_s file_extensions[ext.to_sym] # Find a format based on the file extension: when file_extension file_extensions[file_extension.to_sym] else all_if_none ? @@subclasses : nil end || (sample ? @@subclasses : []) # If we can sample, check all classes # Subset by available reader or writer formats = formats.select do |f| has_reader ? f.reader : (has_writer ? f.writer : true) end # If we have multiple formats and a sample, use that for format detection if formats.length != 1 && sample sample = case sample when Proc then sample.call.to_s else sample.dup.to_s end.dup.force_encoding(Encoding::ASCII_8BIT) # Given a sample, perform format detection across the appropriate formats, choosing the last that matches # Return last format that has a positive detection formats = formats.select {|f| f.detect(sample)} end formats.each(&block) end |
.file_extension ⇒ Array<String>
Retrieves 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.
546 547 548 |
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-884e3ef78084/lib/rdf/format.rb', line 546 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.
223 224 225 |
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-884e3ef78084/lib/rdf/format.rb', line 223 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.
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 |
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-884e3ef78084/lib/rdf/format.rb', line 169 def self.for(*arg, &block) case arg.length when 0 then arg = nil when 1 then arg = arg.first else raise ArgumentError, "Format.for accepts zero or one argument, got #{arg.length}." end = arg.is_a?(Hash) ? arg : {} = {sample: block}.merge() if block_given? formats = case arg when String, RDF::URI # Find a format based on the file name self.each(file_name: arg, **).to_a when Symbol # Try to find a match based on the full class name # We want this to work even if autoloading fails classes = self.each(**).select {|f| f.symbols.include?(arg)} if classes.empty? classes = case arg when :ntriples then [RDF::NTriples::Format] when :nquads then [RDF::NQuads::Format] else [] end end classes else self.each(**.merge(all_if_none: false)).to_a end # Return the last detected format formats.last end |
.name ⇒ Symbol
Returns a human-readable name for the format. Subclasses should override this to use something difererent than the Class name.
338 339 340 341 342 343 |
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-884e3ef78084/lib/rdf/format.rb', line 338 def self.name elements = self.to_s.split("::") name = elements.pop name = elements.pop if name == 'Format' name.to_s end |
.reader(klass) .reader { ... } .reader ⇒ Class Also known as: reader_class
This method returns an undefined value.
Retrieves or defines the reader class for this RDF serialization format.
375 376 377 378 379 380 381 382 383 384 385 386 |
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-884e3ef78084/lib/rdf/format.rb', line 375 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.
249 250 251 |
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-884e3ef78084/lib/rdf/format.rb', line 249 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.
262 263 264 |
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-884e3ef78084/lib/rdf/format.rb', line 262 def self.reader_types reader_symbols.flat_map {|s| RDF::Format.for(s).content_type}.uniq end |
.require(library) (protected)
This method returns an undefined value.
Defines a required Ruby library for this RDF serialization format.
The given library will be required lazily, i.e. only when it is actually first needed, such as when instantiating a reader or parser instance for this format.
572 573 574 |
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-884e3ef78084/lib/rdf/format.rb', line 572 def self.require(library) (@@requires[self] ||= []) << library.to_s 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()
324 325 326 |
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-884e3ef78084/lib/rdf/format.rb', line 324 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()
310 311 312 313 314 315 |
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-884e3ef78084/lib/rdf/format.rb', line 310 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 |
.uri ⇒ URI Also known as: to_uri
Retrieves any format URI defined for this format..
554 555 556 |
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-884e3ef78084/lib/rdf/format.rb', line 554 def self.uri @@uris.invert[self] end |
.uris ⇒ Hash{Symbol => URI}
Returns the unique URI for the format.
236 237 238 |
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-884e3ef78084/lib/rdf/format.rb', line 236 def self.uris @@uris end |
.writer(klass) .writer { ... } .writer ⇒ Class Also known as: writer_class
This method returns an undefined value.
Retrieves or defines the writer class for this RDF serialization format.
418 419 420 421 422 423 424 425 426 427 428 429 |
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-884e3ef78084/lib/rdf/format.rb', line 418 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 |