Module: Tripod::Predicates

Extended by:
ActiveSupport::Concern
Included in:
Components
Defined in:
lib/tripod/predicates.rb

Overview

This module defines behaviour for predicates.

Instance Method Summary collapse

Instance Method Details

#append_to_predicate(predicate_uri, object) ⇒ Object

Append the statement-values for a single predicate in this resource’s in-memory repository. Basically just adds a new statement for this ((resource’s uri)+predicate)

Examples:

Write the attribute.

person.append_to_predicate('http://title', "Mrs.")
person.append_to_predicate('http://title', "Ms.")

Parameters:

  • predicate_uri (String, RDF::URI)

    The uri of the attribute to update.

  • value (Object)

    The values to append for the attribute. Should compatible with RDF::Terms

Raises:



69
70
71
72
73
# File 'lib/tripod/predicates.rb', line 69

def append_to_predicate(predicate_uri, object )
  raise Tripod::Errors::UriNotSet.new() unless @uri

  @repository << RDF::Statement.new(self.uri, RDF::URI.new(predicate_uri.to_s), object)
end

#predicatesObject

returns a list of predicates (as RDF::URIs) for this resource



9
10
11
12
13
14
15
16
17
# File 'lib/tripod/predicates.rb', line 9

def predicates
  pred_uris = []

  @repository.query( [@uri, :predicate, :object] ) do |statement|
    pred_uris << statement.predicate unless pred_uris.include?(statement.predicate)
  end

  pred_uris
end

#read_predicate(predicate_uri) ⇒ Array

Reads values from this resource’s in-memory statement repository, where the predicate matches that of the uri passed in. Returns an Array of RDF::Terms object.

Examples:

Read the value associated with a predicate.

person.read_predicate('http://foo')
person.read_predicate(RDF::URI.new('http://foo'))

Parameters:

  • uri (String, RDF::URI)

    The uri of the predicate to get.

Returns:

  • (Array)

    An array of RDF::Terms.



29
30
31
32
33
34
35
# File 'lib/tripod/predicates.rb', line 29

def read_predicate(predicate_uri)
  values = []
  @repository.query( [@uri, RDF::URI.new(predicate_uri.to_s), :object] ) do |statement|
    values << statement.object
  end
  values
end

#remove_predicate(predicate_uri) ⇒ Object Also known as: delete



75
76
77
78
79
# File 'lib/tripod/predicates.rb', line 75

def remove_predicate(predicate_uri)
  @repository.query( [self.uri, RDF::URI.new(predicate_uri.to_s), :object] ) do |statement|
    @repository.delete( statement )
  end
end

#write_predicate(predicate_uri, objects) ⇒ Object

Replace the statement-values for a single predicate in this resource’s in-memory repository.

Examples:

Write the predicate.

person.write_predicate('http://title', "Mr.")
person.write_predicate('http://title', ["Mrs.", "Ms."])

Parameters:

  • predicate_uri (String, RDF::URI)

    The name of the attribute to update.

  • value (Object, Array)

    The values to set for the attribute. Can be an array, or single item. They should be compatible with RDF::Terms



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/tripod/predicates.rb', line 45

def write_predicate(predicate_uri, objects)
  # remove existing
  remove_predicate(predicate_uri)

  if objects
    # ... and replace
    objects = [objects] unless objects.kind_of?(Array)
    objects.each do |object|
      @repository << RDF::Statement.new( @uri, RDF::URI.new(predicate_uri.to_s), object )
    end
  end

  # returns the new values
  read_predicate(predicate_uri)
end