Module: RDF::Enumerable
- Extended by:
- Util::Aliasing::LateBound
- Includes:
- Enumerable, Countable
- Included in:
- Graph, List, Query::Solution, Reader, Repository
- Defined in:
- lib/rdf/mixin/enumerable.rb
Overview
An RDF statement enumeration mixin.
Classes that include this module must implement an ‘#each` method that yields RDF statements.
Instance Method Summary collapse
-
#contexts(options = {}) ⇒ Enumerator<RDF::Resource>
Returns all unique RDF contexts.
-
#dump(*args) ⇒ String
Returns a serialized string representation of ‘self`.
-
#each_context(&block) ⇒ void
Iterates the given block for each unique RDF context.
-
#each_graph(&block) ⇒ void
Iterates the given block for each RDF graph in ‘self`.
-
#each_object(&block) ⇒ void
Iterates the given block for each unique RDF object term.
-
#each_predicate(&block) ⇒ void
Iterates the given block for each unique RDF predicate term.
-
#each_quad(&block) ⇒ void
Iterates the given block for each RDF quad.
-
#each_statement(&block) ⇒ void
Iterates the given block for each RDF statement.
-
#each_subject(&block) ⇒ void
Iterates the given block for each unique RDF subject term.
-
#each_triple(&block) ⇒ void
Iterates the given block for each RDF triple.
-
#enum_context ⇒ Enumerator
(also: #enum_contexts)
Returns an enumerator for #each_context.
-
#enum_graph ⇒ Enumerator
(also: #enum_graphs)
Returns an enumerator for #each_graph.
-
#enum_object ⇒ Enumerator
(also: #enum_objects)
Returns an enumerator for #each_object.
-
#enum_predicate ⇒ Enumerator
(also: #enum_predicates)
Returns an enumerator for #each_predicate.
-
#enum_quad ⇒ Enumerator
(also: #enum_quads)
Returns an enumerator for #each_quad.
-
#enum_statement ⇒ Enumerator
(also: #enum_statements)
Returns an enumerator for #each_statement.
-
#enum_subject ⇒ Enumerator
(also: #enum_subjects)
Returns an enumerator for #each_subject.
-
#enum_triple ⇒ Enumerator
(also: #enum_triples)
Returns an enumerator for #each_triple.
-
#has_context?(value) ⇒ Boolean
Returns ‘true` if `self` contains the given RDF context.
-
#has_object?(value) ⇒ Boolean
Returns ‘true` if `self` contains the given RDF object term.
-
#has_predicate?(value) ⇒ Boolean
Returns ‘true` if `self` contains the given RDF predicate term.
-
#has_quad?(quad) ⇒ Boolean
Returns ‘true` if `self` contains the given RDF quad.
-
#has_statement?(statement) ⇒ Boolean
(also: #include?)
Returns ‘true` if `self` contains the given RDF statement.
-
#has_subject?(value) ⇒ Boolean
Returns ‘true` if `self` contains the given RDF subject term.
-
#has_triple?(triple) ⇒ Boolean
Returns ‘true` if `self` contains the given RDF triple.
-
#objects(options = {}) ⇒ Enumerator<RDF::Term>
Returns all unique RDF object terms.
-
#predicates(options = {}) ⇒ Enumerator<RDF::URI>
Returns all unique RDF predicate terms.
-
#quads(options = {}) ⇒ Enumerator<Array(RDF::Resource, RDF::URI, RDF::Term, RDF::Resource)>
Returns all RDF quads.
-
#statements(options = {}) ⇒ Enumerator<RDF::Statement>
Returns all RDF statements.
-
#subjects(options = {}) ⇒ Enumerator<RDF::Resource>
Returns all unique RDF subject terms.
-
#supports?(feature) ⇒ Boolean
Returns ‘true` if this repository supports the given `feature`.
-
#to_a ⇒ Array
Returns all RDF statements in ‘self` as an array.
-
#to_hash ⇒ Hash
Returns all RDF object terms indexed by their subject and predicate terms.
-
#to_set ⇒ Set
Returns all RDF statements in ‘self` as a set.
-
#triples(options = {}) ⇒ Enumerator<Array(RDF::Resource, RDF::URI, RDF::Term)>
Returns all RDF triples.
Methods included from Util::Aliasing::LateBound
Methods included from Countable
Instance Method Details
#contexts(options = {}) ⇒ Enumerator<RDF::Resource>
Returns all unique RDF contexts.
469 470 471 472 473 474 475 |
# File 'lib/rdf/mixin/enumerable.rb', line 469 def contexts( = {}) if [:unique] == false enum_statement.map(&:context).to_enum # TODO: optimize else enum_context end end |
#dump(*args) ⇒ String
Returns a serialized string representation of ‘self`.
Before calling this method you may need to explicitly require a serialization plugin for the specified format.
630 631 632 633 634 635 |
# File 'lib/rdf/mixin/enumerable.rb', line 630 def dump(*args) = args.last.is_a?(Hash) ? args.pop : {} writer = RDF::Writer.for(*args) raise RDF::WriterError, "No writer found using #{args.inspect}" unless writer writer.dump(self, nil, ) end |
#each_context {|context| ... } ⇒ void #each_context ⇒ Enumerator
This method returns an undefined value.
Iterates the given block for each unique RDF context.
If no block was given, returns an enumerator.
The order in which values are yielded is undefined.
505 506 507 508 509 510 511 512 513 514 515 516 517 |
# File 'lib/rdf/mixin/enumerable.rb', line 505 def each_context(&block) if block_given? values = {} each_statement do |statement| value = statement.context unless value.nil? || values.include?(value) values[value] = true block.call(value) end end end enum_context end |
#each_graph {|graph| ... } ⇒ void #each_graph ⇒ Enumerator
This method returns an undefined value.
Iterates the given block for each RDF graph in ‘self`.
If no block was given, returns an enumerator.
The order in which graphs are yielded is undefined.
549 550 551 552 553 554 555 556 557 |
# File 'lib/rdf/mixin/enumerable.rb', line 549 def each_graph(&block) if block_given? block.call(RDF::Graph.new(nil, :data => self)) each_context do |context| block.call(RDF::Graph.new(context, :data => self)) end end enum_graph end |
#each_object {|object| ... } ⇒ void #each_object ⇒ Enumerator
This method returns an undefined value.
Iterates the given block for each unique RDF object term.
If no block was given, returns an enumerator.
The order in which values are yielded is undefined.
437 438 439 440 441 442 443 444 445 446 447 448 449 |
# File 'lib/rdf/mixin/enumerable.rb', line 437 def each_object(&block) # FIXME: deduplication if block_given? values = {} each_statement do |statement| value = statement.object unless value.nil? || values.include?(value) values[value] = true block.call(value) end end end enum_object end |
#each_predicate {|predicate| ... } ⇒ void #each_predicate ⇒ Enumerator
This method returns an undefined value.
Iterates the given block for each unique RDF predicate term.
If no block was given, returns an enumerator.
The order in which values are yielded is undefined.
369 370 371 372 373 374 375 376 377 378 379 380 381 |
# File 'lib/rdf/mixin/enumerable.rb', line 369 def each_predicate(&block) if block_given? values = {} each_statement do |statement| value = statement.predicate unless value.nil? || values.include?(value.to_s) values[value.to_s] = true block.call(value) end end end enum_predicate end |
#each_quad {|subject, predicate, object, context| ... } ⇒ void #each_quad ⇒ Enumerator
This method returns an undefined value.
Iterates the given block for each RDF quad.
If no block was given, returns an enumerator.
The order in which quads are yielded is undefined.
238 239 240 241 242 243 244 245 |
# File 'lib/rdf/mixin/enumerable.rb', line 238 def each_quad(&block) if block_given? each_statement do |statement| block.call(*statement.to_quad) end end enum_quad end |
#each_statement {|statement| ... } ⇒ void #each_statement ⇒ Enumerator
This method returns an undefined value.
Iterates the given block for each RDF statement.
If no block was given, returns an enumerator.
The order in which statements are yielded is undefined.
118 119 120 121 122 123 124 |
# File 'lib/rdf/mixin/enumerable.rb', line 118 def each_statement(&block) if block_given? # Invoke {#each} in the containing class: each(&block) end enum_statement end |
#each_subject {|subject| ... } ⇒ void #each_subject ⇒ Enumerator
This method returns an undefined value.
Iterates the given block for each unique RDF subject term.
If no block was given, returns an enumerator.
The order in which values are yielded is undefined.
301 302 303 304 305 306 307 308 309 310 311 312 313 |
# File 'lib/rdf/mixin/enumerable.rb', line 301 def each_subject(&block) if block_given? values = {} each_statement do |statement| value = statement.subject unless value.nil? || values.include?(value.to_s) values[value.to_s] = true block.call(value) end end end enum_subject end |
#each_triple {|subject, predicate, object| ... } ⇒ void #each_triple ⇒ Enumerator
This method returns an undefined value.
Iterates the given block for each RDF triple.
If no block was given, returns an enumerator.
The order in which triples are yielded is undefined.
177 178 179 180 181 182 183 184 |
# File 'lib/rdf/mixin/enumerable.rb', line 177 def each_triple(&block) if block_given? each_statement do |statement| block.call(*statement.to_triple) end end enum_triple end |
#enum_context ⇒ Enumerator Also known as: enum_contexts
Returns an enumerator for #each_context.
524 525 526 |
# File 'lib/rdf/mixin/enumerable.rb', line 524 def enum_context enum_for(:each_context) end |
#enum_graph ⇒ Enumerator Also known as: enum_graphs
Returns an enumerator for #each_graph.
565 566 567 |
# File 'lib/rdf/mixin/enumerable.rb', line 565 def enum_graph enum_for(:each_graph) end |
#enum_object ⇒ Enumerator Also known as: enum_objects
Returns an enumerator for #each_object.
456 457 458 |
# File 'lib/rdf/mixin/enumerable.rb', line 456 def enum_object enum_for(:each_object) end |
#enum_predicate ⇒ Enumerator Also known as: enum_predicates
Returns an enumerator for #each_predicate.
388 389 390 |
# File 'lib/rdf/mixin/enumerable.rb', line 388 def enum_predicate enum_for(:each_predicate) end |
#enum_quad ⇒ Enumerator Also known as: enum_quads
Returns an enumerator for #each_quad.
252 253 254 |
# File 'lib/rdf/mixin/enumerable.rb', line 252 def enum_quad enum_for(:each_quad) end |
#enum_statement ⇒ Enumerator Also known as: enum_statements
Returns an enumerator for #each_statement.
131 132 133 |
# File 'lib/rdf/mixin/enumerable.rb', line 131 def enum_statement enum_for(:each_statement).extend(RDF::Queryable, RDF::Enumerable) end |
#enum_subject ⇒ Enumerator Also known as: enum_subjects
Returns an enumerator for #each_subject.
320 321 322 |
# File 'lib/rdf/mixin/enumerable.rb', line 320 def enum_subject enum_for(:each_subject) end |
#enum_triple ⇒ Enumerator Also known as: enum_triples
Returns an enumerator for #each_triple.
191 192 193 |
# File 'lib/rdf/mixin/enumerable.rb', line 191 def enum_triple enum_for(:each_triple) end |
#has_context?(value) ⇒ Boolean
Returns ‘true` if `self` contains the given RDF context.
482 483 484 |
# File 'lib/rdf/mixin/enumerable.rb', line 482 def has_context?(value) enum_context.include?(value) end |
#has_object?(value) ⇒ Boolean
Returns ‘true` if `self` contains the given RDF object term.
414 415 416 |
# File 'lib/rdf/mixin/enumerable.rb', line 414 def has_object?(value) enum_object.include?(value) end |
#has_predicate?(value) ⇒ Boolean
Returns ‘true` if `self` contains the given RDF predicate term.
346 347 348 |
# File 'lib/rdf/mixin/enumerable.rb', line 346 def has_predicate?(value) enum_predicate.include?(value) end |
#has_quad?(quad) ⇒ Boolean
Returns ‘true` if `self` contains the given RDF quad.
212 213 214 |
# File 'lib/rdf/mixin/enumerable.rb', line 212 def has_quad?(quad) quads.include?(quad) end |
#has_statement?(statement) ⇒ Boolean Also known as: include?
Returns ‘true` if `self` contains the given RDF statement.
94 95 96 |
# File 'lib/rdf/mixin/enumerable.rb', line 94 def has_statement?(statement) !enum_statement.find { |s| s.eql?(statement) }.nil? end |
#has_subject?(value) ⇒ Boolean
Returns ‘true` if `self` contains the given RDF subject term.
278 279 280 |
# File 'lib/rdf/mixin/enumerable.rb', line 278 def has_subject?(value) enum_subject.include?(value) end |
#has_triple?(triple) ⇒ Boolean
Returns ‘true` if `self` contains the given RDF triple.
152 153 154 |
# File 'lib/rdf/mixin/enumerable.rb', line 152 def has_triple?(triple) triples.include?(triple) end |
#objects(options = {}) ⇒ Enumerator<RDF::Term>
Returns all unique RDF object terms.
401 402 403 404 405 406 407 |
# File 'lib/rdf/mixin/enumerable.rb', line 401 def objects( = {}) if [:unique] == false enum_statement.map(&:object).to_enum # TODO: optimize else enum_object end end |
#predicates(options = {}) ⇒ Enumerator<RDF::URI>
Returns all unique RDF predicate terms.
333 334 335 336 337 338 339 |
# File 'lib/rdf/mixin/enumerable.rb', line 333 def predicates( = {}) if [:unique] == false enum_statement.map(&:predicate).to_enum # TODO: optimize else enum_predicate end end |
#quads(options = {}) ⇒ Enumerator<Array(RDF::Resource, RDF::URI, RDF::Term, RDF::Resource)>
Returns all RDF quads.
203 204 205 |
# File 'lib/rdf/mixin/enumerable.rb', line 203 def quads( = {}) enum_statement.map(&:to_quad).to_enum # TODO: optimize end |
#statements(options = {}) ⇒ Enumerator<RDF::Statement>
Returns all RDF statements.
85 86 87 |
# File 'lib/rdf/mixin/enumerable.rb', line 85 def statements( = {}) enum_statement end |
#subjects(options = {}) ⇒ Enumerator<RDF::Resource>
Returns all unique RDF subject terms.
265 266 267 268 269 270 271 |
# File 'lib/rdf/mixin/enumerable.rb', line 265 def subjects( = {}) if [:unique] == false enum_statement.map(&:subject).to_enum # TODO: optimize else enum_subject end end |
#supports?(feature) ⇒ Boolean
Returns ‘true` if this repository supports the given `feature`.
Supported features include:
* `:context` supports statements with a context, allowing multiple contexts
* `:inferrence` supports RDFS inferrence of queryable contents.
74 75 76 |
# File 'lib/rdf/mixin/enumerable.rb', line 74 def supports?(feature) false end |
#to_a ⇒ Array
Returns all RDF statements in ‘self` as an array.
Mixes in ‘RDF::Enumerable` into the returned object.
577 578 579 |
# File 'lib/rdf/mixin/enumerable.rb', line 577 def to_a super.extend(RDF::Enumerable) end |
#to_hash ⇒ Hash
Returns all RDF object terms indexed by their subject and predicate terms.
The return value is a ‘Hash` instance that has the structure: `=> {predicate => [*objects]}`.
601 602 603 604 605 606 607 608 609 610 611 |
# File 'lib/rdf/mixin/enumerable.rb', line 601 def to_hash result = {} each_statement do |statement| next if statement.invalid? # skip any incomplete statements result[statement.subject] ||= {} values = (result[statement.subject][statement.predicate] ||= []) values << statement.object unless values.include?(statement.object) end result end |
#to_set ⇒ Set
Returns all RDF statements in ‘self` as a set.
Mixes in ‘RDF::Enumerable` into the returned object.
588 589 590 591 |
# File 'lib/rdf/mixin/enumerable.rb', line 588 def to_set require 'set' unless defined?(::Set) super.extend(RDF::Enumerable) end |
#triples(options = {}) ⇒ Enumerator<Array(RDF::Resource, RDF::URI, RDF::Term)>
Returns all RDF triples.
143 144 145 |
# File 'lib/rdf/mixin/enumerable.rb', line 143 def triples( = {}) enum_statement.map(&:to_triple).to_enum # TODO: optimize end |