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
:return_graph - whether the original query returned the graphs or not.


16
17
18
19
20
# File 'lib/tripod/resource_collection.rb', line 16

def initialize(resources, opts={})
  @resources = resources
  @criteria = opts[:criteria]
  @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

Instance Method Details

#==(other) ⇒ Object



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

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

#[](*args) ⇒ Object

allow index operator to act on underlying array of resources.



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

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

#eachObject



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

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

#lengthObject



22
23
24
# File 'lib/tripod/resource_collection.rb', line 22

def length
  self.resources.length
end

#to_aObject

return the underlying array



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

def to_a
  resources
end

#to_json(opts = {}) ⇒ Object



64
65
66
67
68
69
# File 'lib/tripod/resource_collection.rb', line 64

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



49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/tripod/resource_collection.rb', line 49

def to_nt
  time_serialization('nt') do
    if @criteria
      @criteria.serialize(:return_graph => @return_graph, :accept_header => "application/n-triples")
    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



71
72
73
74
75
76
77
78
79
# File 'lib/tripod/resource_collection.rb', line 71

def to_rdf
  time_serialization('rdf') do
    if @criteria
      @criteria.serialize(:return_graph => @return_graph, :accept_header => "application/rdf+xml")
    else
      get_graph.dump(:rdf)
    end
  end
end

#to_textObject



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

def to_text
  to_nt
end

#to_ttlObject



81
82
83
84
85
86
87
88
89
# File 'lib/tripod/resource_collection.rb', line 81

def to_ttl
  time_serialization('ttl') do
    if @criteria
      @criteria.serialize(:return_graph => @return_graph, :accept_header => "text/turtle")
    else
      get_graph.dump(:turtle)
    end
  end
end