Class: RDF::Repository

Inherits:
Object
  • Object
show all
Includes:
Countable, Durable, Enumerable, Mutable, Queryable
Defined in:
lib/rdf/repository.rb

Overview

An RDF repository.

Examples:

Creating a transient in-memory repository

repository = RDF::Repository.new

Checking whether a repository is readable/writable

repository.readable?
repository.writable?

Checking whether a repository is persistent or transient

repository.persistent?
repository.transient?

Checking whether a repository is empty

repository.empty?

Checking how many statements a repository contains

repository.count

Checking whether a repository contains a specific statement

repository.has_statement?(statement)

Enumerating statements in a repository

repository.each_statement { |statement| statement.inspect! }

Inserting statements into a repository

repository.insert(*statements)
repository.insert(statement)
repository.insert([subject, predicate, object])
repository << statement
repository << [subject, predicate, object]

Deleting statements from a repository

repository.delete(*statements)
repository.delete(statement)
repository.delete([subject, predicate, object])

Deleting all statements from a repository

repository.clear!

Defined Under Namespace

Modules: Implementation

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Durable

#durable?, #nondurable?

Methods included from Util::Aliasing::LateBound

#alias_method

Methods included from Mutable

#<<, #clear, #delete, #immutable?, #insert, #load, #mutable?, #update

Methods included from Writable

#<<, #insert, #writable?

Methods included from Readable

#readable?

Methods included from Queryable

#first, #first_literal, #first_object, #first_predicate, #first_subject, #first_value, #query

Methods included from Enumerable

#contexts, #dump, #each_context, #each_graph, #each_object, #each_predicate, #each_quad, #each_statement, #each_subject, #each_triple, #enum_context, #enum_graph, #enum_object, #enum_predicate, #enum_quad, #enum_statement, #enum_subject, #enum_triple, #has_context?, #has_object?, #has_predicate?, #has_quad?, #has_statement?, #has_subject?, #has_triple?, #objects, #predicates, #quads, #statements, #subjects, #supports?, #to_a, #to_hash, #to_set, #triples

Methods included from Countable

#count, #empty?, #enum_for

Constructor Details

#initialize(options = {}) {|repository| ... } ⇒ Repository

Initializes this repository instance.

Parameters:

  • options (Hash{Symbol => Object}) (defaults to: {})

Options Hash (options):

  • :uri (URI, #to_s) — default: nil
  • :title (String, #to_s) — default: nil

Yields:

  • (repository)

Yield Parameters:



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/rdf/repository.rb', line 101

def initialize(options = {}, &block)
  @options = options.dup
  @uri     = @options.delete(:uri)
  @title   = @options.delete(:title)

  # Provide a default in-memory implementation:
  send(:extend, Implementation) if self.class.equal?(RDF::Repository)

  if block_given?
    case block.arity
      when 1 then block.call(self)
      else instance_eval(&block)
    end
  end
end

Instance Attribute Details

#optionsHash{Symbol => Object} (readonly)

Returns the options passed to this repository when it was constructed.

Returns:

  • (Hash{Symbol => Object})


54
55
56
# File 'lib/rdf/repository.rb', line 54

def options
  @options
end

#titleString (readonly)

Returns the title of this repository.

Returns:

  • (String)


67
68
69
# File 'lib/rdf/repository.rb', line 67

def title
  @title
end

#uriURI (readonly) Also known as: url

Returns the URI of this repository.

Returns:



60
61
62
# File 'lib/rdf/repository.rb', line 60

def uri
  @uri
end

Class Method Details

.load(filenames, options = {}) {|repository| ... } ⇒ void

This method returns an undefined value.

Loads one or more RDF files into a new transient in-memory repository.

Parameters:

Yields:

  • (repository)

Yield Parameters:



78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/rdf/repository.rb', line 78

def self.load(filenames, options = {}, &block)
  self.new(options) do |repository|
    [filenames].flatten.each do |filename|
      repository.load(filename, options)
    end

    if block_given?
      case block.arity
        when 1 then block.call(repository)
        else repository.instance_eval(&block)
      end
    end
  end
end

Instance Method Details

#inspectString

Returns a developer-friendly representation of this object.

Returns:

  • (String)


121
122
123
# File 'lib/rdf/repository.rb', line 121

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

#inspect!void

This method returns an undefined value.

Outputs a developer-friendly representation of this object to ‘stderr`.



130
131
132
133
# File 'lib/rdf/repository.rb', line 130

def inspect!
  each_statement { |statement| statement.inspect! }
  nil
end

#transaction(context = nil) {|tx| ... } ⇒ void Also known as: transact

This method returns an undefined value.

Executes the given block in a transaction.

Examples:

repository.transaction do |tx|
  tx.insert [RDF::URI("http://rdf.rubyforge.org/"), RDF::DC.title, "RDF.rb"]
end

Parameters:

Yields:

  • (tx)

Yield Parameters:

Yield Returns:

  • (void)

    ignored

See Also:

Since:

  • 0.3.0



150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/rdf/repository.rb', line 150

def transaction(context = nil, &block)
  tx = begin_transaction(context)
  begin
    case block.arity
      when 1 then block.call(tx)
      else tx.instance_eval(&block)
    end
  rescue => error
    rollback_transaction(tx)
    raise error
  end
  commit_transaction(tx)
  self
end