class Tripod::ResourceCollection

 class that wraps a collection of resources, and allows them to be serialized

Attributes

criteria[R]
resources[R]
sparql_query_str[R]

Public Class Methods

new(resources, opts={}) click to toggle source

 options:

:criteria - the criteria used to create this collection
:sparql_query_str - the sparql used to create this collection
:return_graph - whether the original query returned the graphs or not.
# File lib/tripod/resource_collection.rb, line 18
def initialize(resources, opts={})
  @resources = resources
  @criteria = opts[:criteria]
  @sparql_query_str = opts[:sparql_query_str]
  @resource_class = opts[:resource_class]
  @return_graph = opts[:return_graph]
end

Public Instance Methods

==(other) click to toggle source
# File lib/tripod/resource_collection.rb, line 44
def ==(other)
  self.to_nt == other.to_nt
end
[](*args) click to toggle source

allow index operator to act on underlying array of resources.

# File lib/tripod/resource_collection.rb, line 40
def [](*args)
  resources[*args]
end
each() { |e| ... } click to toggle source
# File lib/tripod/resource_collection.rb, line 30
def each
  self.resources.each { |e| yield(e) }
end
length() click to toggle source
# File lib/tripod/resource_collection.rb, line 26
def length
  self.resources.length
end
to_a() click to toggle source

return the underlying array

# File lib/tripod/resource_collection.rb, line 35
def to_a
  resources
end
to_json(opts={}) click to toggle source
# File lib/tripod/resource_collection.rb, line 71
def to_json(opts={})
  # most databases don't have a native json-ld implementation.
  time_serialization('json') do
    get_graph.dump(:jsonld)
  end
end
to_nt() click to toggle source

 for n-triples we can just concatenate them

# File lib/tripod/resource_collection.rb, line 53
def to_nt
  time_serialization('nt') do
    if @criteria
      @criteria.serialize(:return_graph => @return_graph, :accept_header => Tripod.ntriples_header_str)
    elsif @sparql_query_str && @resource_class
      # run the query as a describe.
      @resource_class._raw_describe_select_results(@sparql_query_str, :accept_header => Tripod.ntriples_header_str)
    else
      # for n-triples we can just concatenate them
      nt = ""
      resources.each do |resource|
        nt += resource.to_nt
      end
      nt
    end
  end
end
to_rdf() click to toggle source
# File lib/tripod/resource_collection.rb, line 78
def to_rdf
  time_serialization('rdf') do
    if @criteria
      @criteria.serialize(:return_graph => @return_graph, :accept_header => "application/rdf+xml")
    elsif @sparql_query_str && @resource_class
      # run the query as a describe.
      @resource_class._raw_describe_select_results(@sparql_query_str, :accept_header => "application/rdf+xml")
    else
      get_graph.dump(:rdf)
    end
  end
end
to_text() click to toggle source
# File lib/tripod/resource_collection.rb, line 48
def to_text
  to_nt
end
to_ttl() click to toggle source
# File lib/tripod/resource_collection.rb, line 91
def to_ttl
  time_serialization('ttl') do
    if @criteria
      @criteria.serialize(:return_graph => @return_graph, :accept_header => "text/turtle")
    elsif @sparql_query_str && @resource_class
      # run the query as a describe.
      @resource_class._raw_describe_select_results(@sparql_query_str, :accept_header =>"text/turtle")
    else
      get_graph.dump(:turtle)
    end
  end
end

Private Instance Methods

get_graph() click to toggle source
# File lib/tripod/resource_collection.rb, line 114
def get_graph
  graph = RDF::Graph.new
  RDF::Reader.for(:ntriples).new(self.to_nt) do |reader|
    reader.each_statement do |statement|
      graph << statement
    end
  end
end
time_serialization(format) { || ... } click to toggle source
# File lib/tripod/resource_collection.rb, line 106
def time_serialization(format)
  start_serializing = Time.now if Tripod.logger.debug?
  result = yield if block_given?
  serializing_duration = Time.now - start_serializing if Tripod.logger.debug?
  Tripod.logger.debug( "TRIPOD: Serializing collection to #{format} took #{serializing_duration} secs" )
  result
end