Class: RDF::Vocabulary::Term

Inherits:
URI
  • Object
show all
Defined in:
lib/rdf/vocabulary.rb

Overview

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

Constant Summary

Constants inherited from URI

URI::CACHE_SIZE, URI::GEN_DELIMS, URI::IAUTHORITY, URI::IFRAGMENT, URI::IHIER_PART, URI::IHOST, URI::IPATH_ABEMPTY, URI::IPATH_ABSOLUTE, URI::IPATH_EMPTY, URI::IPATH_NOSCHEME, URI::IPATH_ROOTLESS, URI::IPCHAR, URI::IPRIVATE, URI::IP_literal, URI::IQUERY, URI::IREG_NAME, URI::IRELATIVE_PART, URI::IRELATIVE_REF, URI::IRI, URI::IRI_PARTS, URI::ISEGMENT, URI::ISEGMENT_NZ, URI::ISEGMENT_NZ_NC, URI::IUNRESERVED, URI::IUSERINFO, URI::NON_HIER_SCHEMES, URI::PCT_ENCODED, URI::PORT, URI::PORT_MAPPING, URI::RDS_2A, URI::RDS_2B1, URI::RDS_2B2, URI::RDS_2C1, URI::RDS_2C2, URI::RDS_2D, URI::RDS_2E, URI::RESERVED, URI::SCHEME, URI::SUB_DELIMS, URI::UCSCHAR, URI::UNRESERVED

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from URI

#+, #/, #==, #===, #=~, #absolute?, #authority, #authority=, cache, #canonicalize, #canonicalize!, #end_with?, #eql?, #fragment, #fragment=, #freeze, #has_parent?, #hash, #hier?, #host, #host=, intern, #join, #length, normalize_path, #normalized_authority, #normalized_fragment, #normalized_host, #normalized_password, #normalized_path, #normalized_port, #normalized_query, #normalized_scheme, #normalized_user, #normalized_userinfo, #object, #parent, parse, #parse, #password, #password=, #path, #path=, #pname, #port, #port=, #qname, #query, #query=, #query_values, #query_values=, #relative?, #relativize, #request_uri, #root, #root?, #scheme, #scheme=, #start_with?, #to_str, #to_uri, #uri?, #url?, #urn?, #user, #user=, #userinfo, #userinfo=, #validate!, #value

Methods included from Resource

new, #resource?

Methods included from Term

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

Methods included from RDF::Value

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

Constructor Details

#URI(uri, options = {}) ⇒ Term #URI(options = {}) ⇒ Term

Returns a new instance of Term.

Overloads:

  • #URI(uri, options = {}) ⇒ Term

    Options Hash (options):

    • :validate (Boolean) — default: false
    • :canonicalize (Boolean) — default: false
    • :attributes (Hash{Symbol,Resource => Term, #to_s})

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

  • #URI(options = {}) ⇒ Term

    Options Hash (options):

    • options (Boolean)

      :validate (false)

    • options (Boolean)

      :canonicalize (false)

    • :vocab (Vocabulary)

      The RDF::Vocabulary associated with this term.

    • :scheme (String, #to_s)

      The scheme component.

    • :user (String, #to_s)

      The user component.

    • :password (String, #to_s)

      The password component.

    • :userinfo (String, #to_s)

      The u optionsserinfo component. If this is supplied, the user and password compo optionsnents must be omitted.

    • :host (String, #to_s)

      The host component.

    • :port (String, #to_s)

      The port component.

    • :authority (String, #to_s)

      The a optionsuthority component. If this is supplied, the user, password, useri optionsnfo, host, and port components must be omitted.

    • :path (String, #to_s)

      The path component.

    • :query (String, #to_s)

      The query component.

    • :fragment (String, #to_s)

      The fragment component.

    • :attributes (Hash{Symbol,Resource => Term, #to_s})

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



528
529
530
531
532
533
534
535
536
537
# File 'lib/rdf/vocabulary.rb', line 528

def initialize(*args, **options)
  @attributes = options.fetch(:attributes)
  if RUBY_ENGINE == "rbx"
    # FIXME: Somehow, this gets messed up in Rubinius
    args << options
    super(*args)
  else
    super
  end
end

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



701
702
703
704
705
706
707
708
709
710
711
712
# File 'lib/rdf/vocabulary.rb', line 701

def method_missing(method, *args, &block)
  case method
  when :comment
    @attributes.fetch(method, "")
  when :label
    @attributes.fetch(method, to_s.split(/[\/\#]/).last)
  when :type, :subClassOf, :subPropertyOf, :domain, :range, :inverseOf, :domainIncludes, :rangeIncludes
    Array(@attributes[method]).map {|v| RDF::Vocabulary.expand_pname(v)}
  else
    super
  end
end

Instance Attribute Details

#attributesHash{Symbol,Resource => Term, #to_s}

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



496
497
498
# File 'lib/rdf/vocabulary.rb', line 496

def attributes
  @attributes
end

Instance Method Details

#class?Boolean

Is this a class term?



566
567
568
# File 'lib/rdf/vocabulary.rb', line 566

def class?
  !!(self.type.to_s =~ /Class/)
end

#commentString

‘rdfs:comment` accessor



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

attr_accessor :attributes

#datatype?Boolean

Is this a class term?



580
581
582
# File 'lib/rdf/vocabulary.rb', line 580

def datatype?
  !!(self.type.to_s =~ /Datatype/)
end

#domainRDF::URI

‘rdfs:domain` accessor



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

attr_accessor :attributes

#domain_includesRDF::URI

Accessor for ‘schema:domainIncludes`



689
690
691
# File 'lib/rdf/vocabulary.rb', line 689

def domain_includes
  Array(@attributes[:domainIncludes]).map  {|v| RDF::Vocabulary.expand_pname(v)}
end

#domainIncludesRDF::URI

‘schema:domainIncludes` accessor



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

attr_accessor :attributes

#dupRDF::URI

Returns a duplicate copy of ‘self`.



549
550
551
# File 'lib/rdf/vocabulary.rb', line 549

def dup
  self.class.new((@value || @object).dup, attributes: @attributes)
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:



598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
# File 'lib/rdf/vocabulary.rb', line 598

def each_statement
  attributes.reject {|p| p == :vocab}.each do |prop, values|
    Array(values).each do |value|
      begin
        case prop
        when :type
          prop = RDF.type
          value = RDF::Vocabulary.expand_pname(value)
        when :subClassOf
          prop = RDFS.subClassOf
          value = RDF::Vocabulary.expand_pname(value)
        when :subPropertyOf
          prop = RDFS.subPropertyOf
          value = RDF::Vocabulary.expand_pname(value)
        when :domain
          prop = RDFS.domain
          value = RDF::Vocabulary.expand_pname(value)
        when :range
          prop = RDFS.range
          value = RDF::Vocabulary.expand_pname(value)
        when :inverseOf
          prop = RDF::URI("http://schema.org/inverseOf")
          value = RDF::Vocabulary.expand_pname(value)
        when :domainIncludes
          prop = RDF::URI("http://schema.org/domainIncludes")
          value = RDF::Vocabulary.expand_pname(value)
        when :rangeIncludes
          prop = RDF::URI("http://schema.org/rangeIncludes")
          value = RDF::Vocabulary.expand_pname(value)
        when :label
          prop = RDF::RDFS.label
        when :comment
          prop = RDF::RDFS.comment
        else
          prop = RDF::Vocabulary.expand_pname(prop.to_s)
          next unless prop

          v = value.to_s
          value = RDF::Vocabulary.expand_pname(v)
          unless value && value.valid?
            # Use as most appropriate literal
            value = [
              RDF::Literal::Date,
              RDF::Literal::DateTime,
              RDF::Literal::Integer,
              RDF::Literal::Decimal,
              RDF::Literal::Double,
              RDF::Literal::Boolean,
              RDF::Literal
            ].inject(nil) do |memo, klass|
              l = klass.new(v)
              memo || (l if l.valid?)
            end
          end
        end
        yield RDF::Statement(self, prop, value)
      rescue KeyError
        # Skip things eroneously defined in the vocabulary
      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.

See Also:

  • Object#enum_for


665
666
667
668
669
670
671
# File 'lib/rdf/vocabulary.rb', line 665

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.



678
679
680
# File 'lib/rdf/vocabulary.rb', line 678

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

#inverseOfRDF::URI

‘owl:inverseOf` accessor



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

attr_accessor :attributes

#labelString

‘rdfs:label` accessor



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

attr_accessor :attributes

#other?Boolean

Is this neither a class, property or datatype term?



587
588
589
# File 'lib/rdf/vocabulary.rb', line 587

def other?
  !!(self.type.to_s !~ /(Class|Property|Datatype)/)
end

#property?Boolean

Is this a class term?



573
574
575
# File 'lib/rdf/vocabulary.rb', line 573

def property?
  !!(self.type.to_s =~ /Property/)
end

#rangeRDF::URI

‘rdfs:range` accessor



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

attr_accessor :attributes

#range_includesRDF::URI

Accessor for ‘schema:rangeIncludes`



695
696
697
# File 'lib/rdf/vocabulary.rb', line 695

def range_includes
  Array(@attributes[:rangeIncludes]).map  {|v| RDF::Vocabulary.expand_pname(v)}
end

#rangeIncludesRDF::URI

‘schema:rangeIncludes` accoessor



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

attr_accessor :attributes

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

Implement accessor to symbol attributes



683
684
685
# File 'lib/rdf/vocabulary.rb', line 683

def respond_to?(method, include_all = false)
  @attributes.has_key?(method) || super
end

#subClassOfRDF::URI

‘rdfs:subClassOf` accessor



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

attr_accessor :attributes

#subPropertyOfRDF::URI

‘rdfs:subPropertyOf` accessor



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

attr_accessor :attributes

#typeRDF::URI

‘rdf:type` accessor



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

attr_accessor :attributes

#valid?Boolean

Determine if the URI is a valid according to RFC3987

Since:

  • 0.3.9



558
559
560
561
# File 'lib/rdf/vocabulary.rb', line 558

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

#vocabRDF::Vocabulary

Vocabulary of this term.



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

def vocab; @attributes.fetch(:vocab); end