Class: Tripod::ResourceCollection

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/tripod/resource_collection.rb

Overview

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(resources, opts = {}) ⇒ ResourceCollection

 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.


18
19
20
21
22
23
24
# 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

Instance Attribute Details

#criteriaObject (readonly)

the criteria used to generate this collection



11
12
13
# File 'lib/tripod/resource_collection.rb', line 11

def criteria
  @criteria
end

#resourcesObject (readonly)

Returns the value of attribute resources.



10
11
12
# File 'lib/tripod/resource_collection.rb', line 10

def resources
  @resources
end

#sparql_query_strObject (readonly)

 the sparql query used to generate this collection



12
13
14
# File 'lib/tripod/resource_collection.rb', line 12

def sparql_query_str
  @sparql_query_str
end

Instance Method Details

#==(other) ⇒ Object



44
45
46
# File 'lib/tripod/resource_collection.rb', line 44

def ==(other)
  self.to_nt == other.to_nt
end

#[](*args) ⇒ Object

allow index operator to act on underlying array of resources.



40
41
42
# File 'lib/tripod/resource_collection.rb', line 40

def [](*args)
  resources[*args]
end

#eachObject



30
31
32
# File 'lib/tripod/resource_collection.rb', line 30

def each
  self.resources.each { |e| yield(e) }
end

#lengthObject



26
27
28
# File 'lib/tripod/resource_collection.rb', line 26

def length
  self.resources.length
end

#to_aObject

return the underlying array



35
36
37
# File 'lib/tripod/resource_collection.rb', line 35

def to_a
  resources
end

#to_json(opts = {}) ⇒ Object



71
72
73
74
75
76
# 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_ntObject

 for n-triples we can just concatenate them



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# 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_rdfObject



78
79
80
81
82
83
84
85
86
87
88
89
# 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_textObject



48
49
50
# File 'lib/tripod/resource_collection.rb', line 48

def to_text
  to_nt
end

#to_ttlObject



91
92
93
94
95
96
97
98
99
100
101
102
# 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