Module: RDF::Vocabulary::Term

Includes:
Resource
Defined in:
lib/rdf/vocabulary.rb

Overview

A Vocabulary Term is a Resource that can also act as an Enumerable to generate the RDF definition of vocabulary terms as defined within the vocabulary definition.

Terms include #attributes where values a embedded resources, lists or other terms. This allows, for example, navigation of a concept heirarchy.

Term attributes can also be accessed using #properties where the attribute values are transformed into different types of RDF::Value. Properties can be indexed by key, where a key is defined (See ATTR_URIs), absolute URI, or PName, where the prefix is associated with a loaded vocabulary.

Constant Summary collapse

ATTR_URIs =

Look up URIs for attribute symbols

Returns:

{
  allValuesFrom:      RDF::URI("http://www.w3.org/2002/07/owl#allValuesFrom"),
  altLabel:           RDF::URI("http://www.w3.org/2004/02/skos/core#altLabel"),
  broader:            RDF::URI("http://www.w3.org/2004/02/skos/core#broader"),
  cardinality:        RDF::URI("http://www.w3.org/2002/07/owl#cardinality"),
  comment:            RDF::URI("http://www.w3.org/2000/01/rdf-schema#comment"),
  definition:         RDF::URI("http://www.w3.org/2004/02/skos/core#definition"),
  domain:             RDF::URI("http://www.w3.org/2000/01/rdf-schema#domain"),
  domainIncludes:     RDF::URI("http://schema.org/domainIncludes"),
  editorialNote:      RDF::URI("http://www.w3.org/2004/02/skos/core#editorialNote"),
  equivalentClass:    RDF::URI("http://www.w3.org/2002/07/owl#equivalentClass"),
  equivalentProperty: RDF::URI("http://www.w3.org/2002/07/owl#equivalentProperty"),
  exactMatch:         RDF::URI("http://www.w3.org/2004/02/skos/core#exactMatch"),
  hasTopConcept:      RDF::URI("http://www.w3.org/2004/02/skos/core#hasTopConcept"),
  inScheme:           RDF::URI("http://www.w3.org/2004/02/skos/core#inScheme"),
  intersectionOf:     RDF::URI("http://www.w3.org/2002/07/owl#intersectionOf"),
  inverseOf:          RDF::URI("http://www.w3.org/2002/07/owl#inverseOf"),
  isDefinedBy:        RDF::URI("http://www.w3.org/2000/01/rdf-schema#isDefinedBy"),
  label:              RDF::URI("http://www.w3.org/2000/01/rdf-schema#label"),
  maxCardinality:     RDF::URI("http://www.w3.org/2002/07/owl#maxCardinality"),
  member:             RDF::URI("http://www.w3.org/2004/02/skos/core#member"),
  minCardinality:     RDF::URI("http://www.w3.org/2002/07/owl#minCardinality"),
  narrower:           RDF::URI("http://www.w3.org/2004/02/skos/core#narrower"),
  notation:           RDF::URI("http://www.w3.org/2004/02/skos/core#notation"),
  note:               RDF::URI("http://www.w3.org/2004/02/skos/core#note"),
  onProperty:         RDF::URI("http://www.w3.org/2002/07/owl#onProperty"),
  prefLabel:          RDF::URI("http://www.w3.org/2004/02/skos/core#prefLabel"),
  range:              RDF::URI("http://www.w3.org/2000/01/rdf-schema#range"),
  rangeIncludes:      RDF::URI("http://schema.org/rangeIncludes"),
  related:            RDF::URI("http://www.w3.org/2004/02/skos/core#related"),
  someValuesFrom:     RDF::URI("http://www.w3.org/2002/07/owl#someValuesFrom"),
  subClassOf:         RDF::URI("http://www.w3.org/2000/01/rdf-schema#subClassOf"),
  subPropertyOf:      RDF::URI("http://www.w3.org/2000/01/rdf-schema#subPropertyOf"),
  type:               RDF::URI("http://www.w3.org/1999/02/22-rdf-syntax-ns#type"),
  unionOf:            RDF::URI("http://www.w3.org/2002/07/owl#unionOf"),
}.freeze
URI_ATTRs =

Look up attribute symbols from URIs

Returns:

ATTR_URIs.invert.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Resource

#resource?

Methods included from Term

#<=>, #==, #compatible?, #eql?, #escape, #term?, #terms, #to_base, #to_term

Methods included from RDF::Value

#anonymous?, #canonicalize, #canonicalize!, #constant?, #graph?, #inspect!, #invalid?, #iri?, #list?, #literal?, #node?, #resource?, #start_with?, #statement?, #term?, #to_nquads, #to_ntriples, #to_rdf, #to_term, #type_error, #uri?, #validate!, #variable?

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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

Implement accessor to symbol attributes



1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
# File 'lib/rdf/vocabulary.rb', line 1274

def method_missing(method, *args, &block)
  case method
  when :comment, :notation, :note, :editorialNote, :definition
    attribute_value(method)
  when :label, :altLabel, :prefLabel
    # Defaults to URI fragment or path tail
    begin
      attribute_value(method)
    rescue KeyError
      to_s.split(/[\/\#]/).last
    end
  when :type, :subClassOf, :subPropertyOf, :domain, :range, :isDefinedBy,
       :allValuesFrom, :cardinality, :equivalentClass, :equivalentProperty,
       :imports, :intersectionOf, :inverseOf, :maxCardinality, :minCardinality,
       :onProperty, :someValuesFrom, :unionOf,
       :domainIncludes, :rangeIncludes,
       :broader, :exactMatch, :hasTopConcept, :inScheme, :member, :narrower, :related

    # Return value as an Array, unless it is a list
    case value = attribute_value(method)
    when Array, RDF::List then value
    else [value].compact
    end
  else
    super
  end
end

Instance Attribute Details

#allValuesFromArray<Term> (readonly)

owl:allValuesFrom accessor

Returns:



# File 'lib/rdf/vocabulary.rb', line 826

#altLabelLiteral+ (readonly)

skos:altLabel accessor

Returns:



# File 'lib/rdf/vocabulary.rb', line 867

#attributesHash{Symbol => String, Term, Hash{Symbol => String}, Array<String, Term, Hash{Symbol => String}>} (readonly)

Note:

lookup by PName is DEPRECATED and will be removed in a future version.

Attributes of this vocabulary term, used for finding label and comment and to serialize the term back to RDF.

Attributes are indexed by symbol. Symbols directly interpreted by a term are the accessors defined for the RDF::Vocabulary::Term class, also in ATTR_URIs. Other keys are interpreted as absolute URIs or PNames for properties defined on this term.

Symbols which are accessors may also be looked up by their associated URI.

Values may be Strings, Hash (Map), or Terms, or an Array including a combination of these. A Hash (Map) is used to create a datatype/language map to one or more string values which represent either datatyped literals, or language-tagged literals as interpreted by #attribute_value.

In general, this accessor is used internally. The #properties method interprets these values as RDF::Value.

Examples:

looking up term label

RDF::RDFS.Literal.attributes[:label] #=> "Literal"
RDF::RDFS.Literal.attributes[:"rdfs:label"] #=> "Literal"
RDF::RDFS.Literal.attributes[RDF::RDFS.label] #=> "Literal"
RDF::RDFS.Literal.attributes["http://www.w3.org/2000/01/rdf-schema#label"] #=> "Literal"
RDF::RDFS.Literal.attributes[:"http://www.w3.org/2000/01/rdf-schema#label"] #=> "Literal"

Returns:

  • (Hash{Symbol => String, Term, Hash{Symbol => String}, Array<String, Term, Hash{Symbol => String}>})

See Also:



933
934
935
# File 'lib/rdf/vocabulary.rb', line 933

def attributes
  @attributes
end

#broaderArray<Term> (readonly)

skos:broader accessor

Returns:



# File 'lib/rdf/vocabulary.rb', line 867

#cardinalityArray<Literal> (readonly)

owl:cardinality accessor

Returns:



# File 'lib/rdf/vocabulary.rb', line 826

#commentLiteral+ (readonly)

rdfs:comment accessor

Returns:



# File 'lib/rdf/vocabulary.rb', line 801

#definitionLiteral+ (readonly)

skos:definition accessor

Returns:



# File 'lib/rdf/vocabulary.rb', line 867

#domainArray<Term> (readonly)

rdfs:domain accessor

Returns:



# File 'lib/rdf/vocabulary.rb', line 801

#domainIncludesArray<Term> (readonly)

schema:domainIncludes accessor

Returns:



# File 'lib/rdf/vocabulary.rb', line 860

#editorialNoteLiteral+ (readonly)

skos:editorialNote accessor

Returns:



# File 'lib/rdf/vocabulary.rb', line 867

#equivalentClassArray<Term> (readonly)

owl:equivalentClass accessor

Returns:



# File 'lib/rdf/vocabulary.rb', line 826

#equivalentPropertyArray<Term> (readonly)

owl:equivalentProperty accessor

Returns:



# File 'lib/rdf/vocabulary.rb', line 826

#exactMatchArray<Term> (readonly)

skos:exactMatch accessor

Returns:



# File 'lib/rdf/vocabulary.rb', line 867

#hasTopConceptArray<Term> (readonly)

skos:hasTopConcept accessor

Returns:



# File 'lib/rdf/vocabulary.rb', line 867

#inSchemeArray<Term> (readonly)

skos:inScheme accessor

Returns:



# File 'lib/rdf/vocabulary.rb', line 867

#intersectionOfArray<Term> (readonly)

owl:intersectionOf accessor

Returns:



# File 'lib/rdf/vocabulary.rb', line 826

#inverseOfArray<Term> (readonly)

owl:inverseOf accessor

Returns:



# File 'lib/rdf/vocabulary.rb', line 826

#isDefinedByArray<Term> (readonly)

rdfs:isDefinedBy accessor

Returns:



# File 'lib/rdf/vocabulary.rb', line 801

#labelLiteral (readonly)

rdfs:label accessor

Returns:



# File 'lib/rdf/vocabulary.rb', line 801

#maxCardinalityArray<Literal> (readonly)

owl:maxCardinality accessor

Returns:



# File 'lib/rdf/vocabulary.rb', line 826

#memberArray<Term> (readonly)

skos:member accessor

Returns:



# File 'lib/rdf/vocabulary.rb', line 867

#minCardinalityArray<Literal> (readonly)

owl:minCardinality accessor

Returns:



# File 'lib/rdf/vocabulary.rb', line 826

#narrowerArray<Term> (readonly)

skos:narrower accessor

Returns:



# File 'lib/rdf/vocabulary.rb', line 867

#notationLiteral+ (readonly)

skos:notation accessor

Returns:



# File 'lib/rdf/vocabulary.rb', line 867

#noteLiteral+ (readonly)

skos:note accessor

Returns:



# File 'lib/rdf/vocabulary.rb', line 867

#onPropertyArray<Term> (readonly)

owl:onProperty accessor

Returns:



# File 'lib/rdf/vocabulary.rb', line 826

#prefLabelLiteral (readonly)

skos:prefLabel accessor

Returns:



# File 'lib/rdf/vocabulary.rb', line 867

#rangeArray<Term> (readonly)

rdfs:range accessor

Returns:



# File 'lib/rdf/vocabulary.rb', line 801

#rangeIncludesArray<Term> (readonly)

schema:rangeIncludes accessor

Returns:



# File 'lib/rdf/vocabulary.rb', line 860

skos:related accessor

Returns:



# File 'lib/rdf/vocabulary.rb', line 867

#someValuesFromArray<Term> (readonly)

owl:someValuesFrom accessor

Returns:



# File 'lib/rdf/vocabulary.rb', line 826

#subClassOfArray<Term> (readonly)

rdfs:subClassOf accessor

Returns:



# File 'lib/rdf/vocabulary.rb', line 801

#subPropertyOfArray<Term> (readonly)

rdfs:subPropertyOf accessor

Returns:



# File 'lib/rdf/vocabulary.rb', line 801

#typeArray<Term> (readonly)

rdf:type accessor

Returns:



# File 'lib/rdf/vocabulary.rb', line 801

#unionOfList<Term>, Array<Term> (readonly)

owl:unionOf accessor

Returns:



# File 'lib/rdf/vocabulary.rb', line 826

#vocabRDF::Vocabulary (readonly)

Vocabulary of this term.

Returns:



911
912
913
# File 'lib/rdf/vocabulary.rb', line 911

def vocab
  @vocab
end

Class Method Details

.intern(str, *args, **options) ⇒ RDF::URI

Returns an interned RDF::URI instance based on the given uri string.

The maximum number of cached interned URI references is given by the CACHE_SIZE constant. This value is unlimited by default, in which case an interned URI object will be purged only when the last strong reference to it is garbage collected (i.e., when its finalizer runs).

Excepting special memory-limited circumstances, it should always be safe and preferred to construct new URI references using RDF::URI.intern instead of RDF::URI.new, since if an interned object can't be returned for some reason, this method will fall back to returning a freshly-allocated one.

Parameters:

Returns:

  • (RDF::URI)

    an immutable, frozen URI object



1008
1009
1010
# File 'lib/rdf/vocabulary.rb', line 1008

def self.intern(str, *args, **options)
  (URI.cache[(str = str.to_s).to_sym] ||= self.new(str, *args, **options)).freeze
end

.new(uri, attributes: , vocab: , **options) ⇒ Object .new(attributes: , vocab: , **options) ⇒ Object

Overloads:

  • .new(uri, attributes: , vocab: , **options) ⇒ Object

    Parameters:

    • uri (URI, String, #to_s)
    • vocab (Vocabulary) (defaults to: )

      Vocabulary of this term.

    • attributes (Hash{Symbol => String,Term,Hash{Symbol=>String,Array<String>},Array<String>}) (defaults to: )

      ({}) Attributes of this vocabulary term, used for finding label and comment and to serialize the term back to RDF. See #attributes and #properties for other ways to access.

    • options (Hash{Symbol => Object})

      Options from URI#initialize

  • .new(attributes: , vocab: , **options) ⇒ Object

    Parameters:

    • vocab (Vocabulary) (defaults to: )

      Vocabulary of this term.

    • attributes (Hash{Symbol => String,Term,Hash{Symbol=>String,Array<String>},Array<String>}) (defaults to: )

      ({}) Attributes of this vocabulary term, used for finding label, comment and other term properties, and to serialize the term back to RDF. See #attributes and #properties for other ways to access.

    • options (Hash{Symbol => Object})

      Options from URI#initialize



950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
# File 'lib/rdf/vocabulary.rb', line 950

def self.new(*args, vocab: nil, attributes: {}, **options)
  klass = if args.first.nil?
    RDF::Node
  elsif args.first.is_a?(Hash)
    args.unshift(nil)
    RDF::Node
  elsif args.first.to_s.start_with?("_:")
    args = args[1..-1].unshift($1)
    RDF::Node
  else RDF::URI
  end

  # Create default proc on attributes to allow lookup by different key types.
  attributes = attributes.dup if attributes.frozen?
  attributes.default_proc = -> (hash, key) do
    sym = case key
    when RDF::URI
      URI_ATTRs.fetch(key, key.to_s.to_sym)
    when String
      URI_ATTRs.fetch(RDF::URI(key), key.to_s.to_sym)
    when Symbol
      case key.to_s
      when /^https?:/
        # Lookup by associated attribute, or pname
        URI_ATTRs.fetch(RDF::URI(key.to_s), RDF::URI(key).pname.to_sym)
      when /:/
        uri = RDF::Vocabulary.expand_pname(key)
        # Lookup by associated attribute or URI
        URI_ATTRs.fetch(uri, uri.to_s.to_sym)
      end
    end
    hash.fetch(sym, nil)
  end

  term = klass.allocate.extend(Term)
  term.send(:initialize, *args)
  term.instance_variable_set(:@vocab, vocab)
  term.instance_variable_set(:@attributes, attributes)
  term
end

Instance Method Details

#attribute_value(prop) ⇒ RDF::Value+

Values of an attributes as RDF::Value.

Attribute values are returned as either an RDF::Value or Array<RDf::Value if there is more than one value.

Attribute values which are not already a RDF::Value (including strings and symbols) are converted by a heuristic loookup as follows:

Parameters:

  • prop (Symbol)

Returns:



1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
# File 'lib/rdf/vocabulary.rb', line 1111

def attribute_value(prop)
  values = attributes[prop]
  return nil if values.nil?
  values = [values].compact unless values.is_a?(Array)
  prop_values = values.map do |value|
    v = value.is_a?(Symbol) ? value.to_s : value
    value = (RDF::Vocabulary.expand_pname(v) rescue nil) if v.is_a?(String) && v.include?(':')
    value = value.to_uri if value.respond_to?(:to_uri)
    value = if value.is_a?(RDF::Value) && value.valid?
      value
    elsif value.is_a?(Hash)
      # type/language map
      value.inject([]) do |memo, (k,v)|
        vv = [v] unless v.is_a?(Array)
        memo << if k.to_s.include?(':')
          dt = RDF::Vocabulary.expand_pname(v) rescue nil
          vv.map {|val| RDF::Literal(val, datatype: dt)}
        else
          vv.map {|val| RDF::Literal(val, language: k)}
        end
      end.flatten.compact.select(&:valid?)
    else
      # Use as most appropriate literal
      [
        RDF::Literal::Date,
        RDF::Literal::DateTime,
        RDF::Literal::Integer,
        RDF::Literal::Decimal,
        RDF::Literal::Double,
        RDF::Literal::Boolean,
        RDF::Literal
      ].inject(nil) do |m, klass|
        m || begin
          l = klass.new(v)
          l if l.valid?
        end
      end
    end
  end.flatten

  prop_values.length <= 1 ? prop_values.first : prop_values
end

#class?Boolean

Is this a class term?

Returns:

  • (Boolean)


1036
1037
1038
# File 'lib/rdf/vocabulary.rb', line 1036

def class?
  Array(self.type).any? {|t| t.to_s.include?('Class')}
end

#datatype?Boolean

Is this a class term?

Returns:

  • (Boolean)


1050
1051
1052
# File 'lib/rdf/vocabulary.rb', line 1050

def datatype?
  Array(self.type).any? {|t| t.to_s.include?('Datatype')}
end

#domain_includesRDF::URI

Accessor for schema:domainIncludes

Returns:



1223
1224
1225
# File 'lib/rdf/vocabulary.rb', line 1223

def domain_includes
  domainIncludes
end

#dupRDF::URI

Returns a duplicate copy of self.

Returns:



1016
1017
1018
1019
1020
1021
# File 'lib/rdf/vocabulary.rb', line 1016

def dup
  term = super.extend(Term)
  term.instance_variable_set(:@vocab, vocab)
  term.instance_variable_set(:@attributes, attributes)
  term
end

#each_statement {|| ... } ⇒ Object

Enumerate each statement constructed from the defined vocabulary terms

If a property value is known to be a URI, or expands to a URI, the object is a URI, otherwise, it will be a Literal.

Yields:

  • statement

Yield Parameters:



1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
# File 'lib/rdf/vocabulary.rb', line 1161

def each_statement
  attributes.keys.each do |p|
    prop = ATTR_URIs.fetch(p) { RDF::Vocabulary::expand_pname(p)}
    values = attribute_value(p)
    values = [values].compact unless values.is_a?(Array)
    values.each do |value|
      yield RDF::Statement(self, prop, value) if prop.is_a?(RDF::URI)

      # Enumerate over value statements, if enumerable
      if value.is_a?(RDF::Enumerable) || (value.is_a?(Term) && value.node?)
        value.each_statement {|s| yield s}
      end
    end
  end
end

#enum_for(method = :each_statement, *args) ⇒ RDF::Enumerable::Enumerator Also known as: to_enum

Return an enumerator over Statement defined for this vocabulary.

Returns:

See Also:

  • Object#enum_for


1181
1182
1183
1184
1185
1186
1187
# File 'lib/rdf/vocabulary.rb', line 1181

def enum_for(method = :each_statement, *args)
  # Ensure that enumerators are, themselves, queryable
  this = self
  Enumerable::Enumerator.new do |yielder|
    this.send(method, *args) {|*y| yielder << (y.length > 1 ? y : y.first)}
  end
end

#inspectString

Returns a String representation of the URI object's state.

Returns:

  • (String)

    The URI object's state, as a String.



1194
1195
1196
# File 'lib/rdf/vocabulary.rb', line 1194

def inspect
  sprintf("#<%s:%#0x ID:%s>", Term.to_s, self.object_id, self.to_s)
end

#other?Boolean

Is this neither a class, property or datatype term?

Returns:

  • (Boolean)


1064
1065
1066
# File 'lib/rdf/vocabulary.rb', line 1064

def other?
  Array(self.type).none? {|t| t.to_s.match?(/(Class|Property|Datatype|Restriction)/)}
end

#propertiesHash{Symbol => Array<RDF::Value>}

Note:

lookup by PName is DEPRECATED and will be removed in a future version.

Enumerate attributes with values transformed into RDF::Value instances Uses an empty hash with a default_proc which looks up values in attributes. The prevents specific attributes from being evaluated until acessed.

Properties are indexed by symbol. Symbols directly interpreted by a term are the accessors defined for the RDF::Vocabulary::Term class, also in ATTR_URIs. Other keys are interpreted as absolute URIs or PNames for properties defined on this term.

Symbols which are accessors may also be looked up by their associated URI.

Examples:

looking up term label

RDF::RDFS.Literal.label #=> RDF::Literal("Literal")
RDF::RDFS.Literal.properties[:label] #=> RDF::Literal("Literal")
RDF::RDFS.Literal.properties[:"rdfs:label"] #=> RDF::Literal("Literal")
RDF::RDFS.Literal.properties[RDF::RDFS.label] #=> RDF::Literal("Literal")
RDF::RDFS.Literal.properties["http://www.w3.org/2000/01/rdf-schema#label"] #=> RDF::Literal("Literal")
RDF::RDFS.Literal.properties[:"http://www.w3.org/2000/01/rdf-schema#label"] #=> RDF::Literal("Literal")

Returns:

See Also:



1088
1089
1090
# File 'lib/rdf/vocabulary.rb', line 1088

def properties
  Hash.new {|hash, key| attribute_value(key)}
end

#property?Boolean

Is this a class term?

Returns:

  • (Boolean)


1043
1044
1045
# File 'lib/rdf/vocabulary.rb', line 1043

def property?
  Array(self.type).any? {|t| t.to_s.include?('Property')}
end

#range_includesRDF::URI

Accessor for schema:rangeIncludes

Returns:



1229
1230
1231
# File 'lib/rdf/vocabulary.rb', line 1229

def range_includes
  rangeIncludes
end

#respond_to?(method, include_all = false) ⇒ Boolean

Implement accessor to symbol attributes

Returns:

  • (Boolean)


1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
# File 'lib/rdf/vocabulary.rb', line 1199

def respond_to?(method, include_all = false)
  case method
  when :comment, :notation, :note, :editorialNote, :definition,
       :label, :altLabel, :prefLabel, :type, :isDefinedBy
    true
  when :subClassOf, :subPropertyOf,
       :domainIncludes, :rangeIncludes,
       :equivalentClass, :intersectionOf, :unionOf
    self.class?
  when :domain, :range, :equivalentProperty, :inverseOf
    self.property?
  when :allValuesFrom, :cardinality,
       :maxCardinality, :minCardinality,
       :onProperty, :someValuesFrom
    self.restriction?
  when :broader, :exactMatch, :hasTopConcept, :inScheme, :member, :narrower, :related
    @attributes.key?(method)
  else
    super
  end
end

#restriction?Boolean

Is this a Restriction term?

Returns:

  • (Boolean)


1057
1058
1059
# File 'lib/rdf/vocabulary.rb', line 1057

def restriction?
  Array(self.type).any? {|t| t.to_s.include?('Restriction')}
end

#to_ruby(indent: "") ⇒ String

Serialize back to a Ruby source initializer. This is used primarily by Writer.

Parameters:

  • indent (String) (defaults to: "")

Returns:

  • (String)


1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
# File 'lib/rdf/vocabulary.rb', line 1238

def to_ruby(indent: "")
  "term(" +
  (self.uri? ? self.to_s.inspect + ",\n" : "\n") +
  "#{indent}  " +
  attributes.keys.sort.map do |k|
    values = attribute_value(k)
    values = [values].compact unless values.is_a?(Array)
    values = values.map do |value|
      if value.is_a?(Literal) && %w(: comment definition notation note editorialNote).include?(k.to_s)
        "%(#{value.to_s.gsub('(', '\(').gsub(')', '\)')})"
      elsif value.node? && value.is_a?(RDF::Vocabulary::Term)
        "#{value.to_ruby(indent: indent + "  ")}"
      elsif value.is_a?(RDF::Term)
        "#{value.to_s.inspect}"
      elsif value.is_a?(RDF::List)
        list_elements = value.map do |u|
          if u.uri?
            "#{u.to_s.inspect}"
          elsif u.node? && u.respond_to?(:to_ruby)
            u.to_ruby(indent: indent + "  ")
          else
            "#{u.to_s.inspect}"
          end
        end
        "list(#{list_elements.join(', ')})"
      else
        "#{value.inspect}"
      end
    end
    "#{k.to_s.include?(':') ? k.to_s.inspect : k}: " +
    (values.length == 1 ? values.first : ('[' + values.join(',') + ']'))
  end.join(",\n#{indent}  ") + "\n#{indent})"

end

#valid?Boolean

Determine if the URI is a valid according to RFC3987

Returns:

  • (Boolean)

    true or false

Since:

  • 0.3.9



1028
1029
1030
1031
# File 'lib/rdf/vocabulary.rb', line 1028

def valid?
  # Validate relative to RFC3987
  node? || RDF::URI::IRI.match?(to_s) || false
end