Class: RDF::Vocabulary

Inherits:
Object
  • Object
show all
Extended by:
Enumerable
Defined in:
lib/rdf/vocab.rb

Overview

A Vocabulary represents an RDFS or OWL vocabulary.

### Vocabularies:

The following vocabularies are pre-defined for your convenience:

  • RDF - Resource Description Framework (RDF)

  • CC - Creative Commons (CC)

  • CERT - W3 Authentication Certificate (CERT)

  • DC - Dublin Core (DC)

  • DC11 - Dublin Core 1.1 (DC11) deprecated

  • DOAP - Description of a Project (DOAP)

  • EXIF - Exchangeable Image File Format (EXIF)

  • FOAF - Friend of a Friend (FOAF)

  • GEO - WGS84 Geo Positioning (GEO)

  • HTTP - Hypertext Transfer Protocol (HTTP)

  • OWL - Web Ontology Language (OWL)

  • RDFS - RDF Schema (RDFS)

  • RSA - W3 RSA Keys (RSA)

  • RSS - RDF Site Summary (RSS)

  • SIOC - Semantically-Interlinked Online Communities (SIOC)

  • SKOS - Simple Knowledge Organization System (SKOS)

  • WOT - Web of Trust (WOT)

  • XHTML - Extensible HyperText Markup Language (XHTML)

  • XSD - XML Schema (XSD)

Examples:

Using pre-defined RDF vocabularies

include RDF

DC.title      #=> RDF::URI("http://purl.org/dc/terms/title")
FOAF.knows    #=> RDF::URI("http://xmlns.com/foaf/0.1/knows")
RDF.type      #=> RDF::URI("http://www.w3.org/1999/02/22-rdf-syntax-ns#type")
RDFS.seeAlso  #=> RDF::URI("http://www.w3.org/2000/01/rdf-schema#seeAlso")
RSS.title     #=> RDF::URI("http://purl.org/rss/1.0/title")
OWL.sameAs    #=> RDF::URI("http://www.w3.org/2002/07/owl#sameAs")
XSD.dateTime  #=> RDF::URI("http://www.w3.org/2001/XMLSchema#dateTime")

Using ad-hoc RDF vocabularies

foaf = RDF::Vocabulary.new("http://xmlns.com/foaf/0.1/")
foaf.knows    #=> RDF::URI("http://xmlns.com/foaf/0.1/knows")
foaf[:name]   #=> RDF::URI("http://xmlns.com/foaf/0.1/name")
foaf['mbox']  #=> RDF::URI("http://xmlns.com/foaf/0.1/mbox")

See Also:

Direct Known Subclasses

CC, CERT, DC, DC11, DOAP, EXIF, FOAF, GEO, HTTP, OWL, RDFS, RSA, RSS, SIOC, SKOS, WOT, XHTML, XSD

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(uri) ⇒ Vocabulary

Returns a new instance of Vocabulary.

Parameters:



136
137
138
139
140
141
# File 'lib/rdf/vocab.rb', line 136

def initialize(uri)
  @uri = case uri
    when RDF::URI then uri.to_s
    else RDF::URI.parse(uri.to_s) ? uri.to_s : nil
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(property, *args, &block) ⇒ Object (protected)



201
202
203
204
205
206
207
# File 'lib/rdf/vocab.rb', line 201

def method_missing(property, *args, &block)
  if args.empty?
    self[property]
  else
    raise ArgumentError.new("wrong number of arguments (#{args.size} for 0)")
  end
end

Class Method Details

.[](property) ⇒ RDF::URI

Returns the URI for the term ‘property` in this vocabulary.

Parameters:

Returns:



84
85
86
# File 'lib/rdf/vocab.rb', line 84

def self.[](property)
  RDF::URI.intern([to_s, property.to_s].join(''))
end

.__prefix__Symbol

Returns a suggested CURIE/QName prefix for this vocabulary class.

Returns:

  • (Symbol)

Since:

  • 0.3.0



127
128
129
# File 'lib/rdf/vocab.rb', line 127

def self.__prefix__
  self.__name__.split('::').last.downcase.to_sym
end

.each {|klass| ... } ⇒ Enumerator

Enumerates known RDF vocabulary classes.

Yields:

  • (klass)

Yield Parameters:

  • klass (Class)

Returns:



57
58
59
60
61
62
63
64
65
66
67
# File 'lib/rdf/vocab.rb', line 57

def self.each(&block)
  if self.equal?(Vocabulary)
    # This is needed since all vocabulary classes are defined using
    # Ruby's autoloading facility, meaning that `@@subclasses` will be
    # empty until each subclass has been touched or require'd.
    RDF::VOCABS.each { |v| require "rdf/vocab/#{v}" unless v == :rdf }
    @@subclasses.each(&block)
  else
    # TODO: should enumerate vocabulary-specific defined properties.
  end
end

.inspectString

Returns a developer-friendly representation of this vocabulary class.

Returns:

  • (String)


108
109
110
111
112
113
114
# File 'lib/rdf/vocab.rb', line 108

def self.inspect
  if self == Vocabulary
    self.to_s
  else
    sprintf("%s(%s)", superclass.to_s, to_s)
  end
end

.property(property) ⇒ void

This method returns an undefined value.

Defines a vocabulary term called ‘property`.

Parameters:

  • (Symbol)


74
75
76
77
# File 'lib/rdf/vocab.rb', line 74

def self.property(property)
  metaclass = class << self; self; end
  metaclass.send(:define_method, property) { self[property] } # class method
end

.to_sString

Returns a string representation of this vocabulary class.

Returns:

  • (String)


100
101
102
# File 'lib/rdf/vocab.rb', line 100

def self.to_s
  @@uris.has_key?(self) ? @@uris[self].to_s : super
end

.to_uriRDF::URI

Returns the base URI for this vocabulary class.

Returns:



92
93
94
# File 'lib/rdf/vocab.rb', line 92

def self.to_uri
  RDF::URI.intern(to_s)
end

Instance Method Details

#[](property) ⇒ URI

Returns the URI for the term ‘property` in this vocabulary.

Parameters:

Returns:



148
149
150
# File 'lib/rdf/vocab.rb', line 148

def [](property)
  RDF::URI.intern([to_s, property.to_s].join(''))
end

#inspectString

Returns a developer-friendly representation of this vocabulary.

Returns:

  • (String)


172
173
174
# File 'lib/rdf/vocab.rb', line 172

def inspect
  sprintf("#<%s:%#0x(%s)>", self.class.name, __id__, to_s)
end

#to_sString

Returns a string representation of this vocabulary.

Returns:

  • (String)


164
165
166
# File 'lib/rdf/vocab.rb', line 164

def to_s
  @uri.to_s
end

#to_uriURI

Returns the base URI for this vocabulary.

Returns:



156
157
158
# File 'lib/rdf/vocab.rb', line 156

def to_uri
  RDF::URI.intern(to_s)
end