Class: RDF::Triple

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

Overview

A triple.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(subject, predicate, object) ⇒ Triple

Creates a new triple with the specified subject, predicate, and object.

Raises:

ArgumentError

if any of the parameters is nil.

LiteralNodeSubjectError

if subject is a LiteralNode.

LiteralNodePredicateError

if predicate is a LiteralNode.

BlankNodePredicateError

if predicate is a BlankNode.

Raises:

  • (ArgumentError)


28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/rdf/triple.rb', line 28

def initialize(subject, predicate, object)
  @subject = subject
  @predicate = predicate
  @object = object
  
  raise ArgumentError, 'subject must be a node' unless RDF::Node?(@subject)
  raise ArgumentError, 'predicate must be a node' unless RDF::Node?(@predicate)
  raise ArgumentError, 'object must be a node' unless RDF::Node?(@object)
  
  raise RDF::InvalidSubjectError, "#{@subject} must be a blank or uri node" unless RDF::BlankNode?(@subject) || RDF::UriNode?(@subject)
  raise RDF::InvalidPredicateError, "#{@predicate} must be a uri node" unless RDF::UriNode?(@predicate)
end

Instance Attribute Details

#objectObject (readonly)

the object of this triple



19
20
21
# File 'lib/rdf/triple.rb', line 19

def object
  @object
end

#predicateObject (readonly)

the predicate of this triple



17
18
19
# File 'lib/rdf/triple.rb', line 17

def predicate
  @predicate
end

#subjectObject (readonly)

the subject of this triple



15
16
17
# File 'lib/rdf/triple.rb', line 15

def subject
  @subject
end

Class Method Details

.construct(*a) ⇒ Object



4
5
6
7
8
9
10
11
12
# File 'lib/rdf/triple.rb', line 4

def self.construct(*a)
  if a.size == 1
    a[0] if a[0].is_a?(Triple)
  elsif a.size == 3
    Triple.new(*a)
  else
    raise ArgumentError, 'Expected triple or subject, predicate, and object'
  end
end

Instance Method Details

#==(o) ⇒ Object Also known as: eql?

Returns true if o is a Triple with the same subject, object, and predicate.



42
43
44
45
46
47
48
# File 'lib/rdf/triple.rb', line 42

def ==(o)
  if RDF::Triple?(o)
    subject == o.subject &&
    predicate == o.predicate &&
    object == o.object
  end
end

#hashObject

Returns a hash value for this Triple.



53
54
55
# File 'lib/rdf/triple.rb', line 53

def hash
  [-78640401, subject.hash, predicate.hash, object.hash].hash
end

#to_sObject



57
58
59
# File 'lib/rdf/triple.rb', line 57

def to_s
  "#{subject} #{predicate} #{object}."
end