Class: RDFObject::Collection

Inherits:
Hash
  • Object
show all
Defined in:
lib/rdf_objects/collection.rb

Instance Method Summary collapse

Instance Method Details

#find_by_predicate(predicate) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/rdf_objects/collection.rb', line 6

def find_by_predicate(predicate)
  if predicate.could_be_a_safe_curie?
    predicate = Curie.parse predicate
  end      
  matches = self.find_all {|r| 
    if r[1][predicate]
      r[1]
    end
  }
  resources = Collection.new
  matches.each do | match |
    resources[match[0]] = match[1]
  end
  return resources
end

#find_by_predicate_and_object(predicate, object) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/rdf_objects/collection.rb', line 22

def find_by_predicate_and_object(predicate, object)
  if predicate.could_be_a_safe_curie?
    predicate = Curie.parse predicate
  end      
  if object.could_be_a_safe_curie?
    object = Curie.parse object
  end
  object = self[object] if self[object]      
  matches = self.find_all {|r| [*r[1][predicate]].index(object) }

  resources = Collection.new
  matches.each do | match |
    resources[match[0]] = match[1]
  end
  return resources
end

#find_or_create(uri) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/rdf_objects/collection.rb', line 39

def find_or_create(uri)
  if uri.could_be_a_safe_curie?
    uri = Curie.parse uri
  end
  if BlankNode.is_bnode_id?(uri)
    bnode = BlankNode.new(uri)
    self[bnode.uri] = bnode unless self[bnode.uri]
    return self[bnode.uri]
  else
    self[uri] = Resource.new(uri) unless self[uri]        
  end
  self[uri]
end

#parse(data, options = {}) ⇒ Object



57
58
59
60
61
62
# File 'lib/rdf_objects/collection.rb', line 57

def parse(data, options={})
  parser = Parser.init_parser(data, options)
  parser.collection = self
  parser.parse
  nil
end

#remove(resource) ⇒ Object



53
54
55
# File 'lib/rdf_objects/collection.rb', line 53

def remove(resource)
  self.delete(resource.uri)
end

#to_jsonObject



95
96
97
98
99
100
101
# File 'lib/rdf_objects/collection.rb', line 95

def to_json
  j_hash = {}
  self.values.each do |resource|
    j_hash.merge!(resource.to_json_hash)
  end
  JSON.generate(j_hash)
end

#to_ntriplesObject



64
65
66
67
68
69
70
# File 'lib/rdf_objects/collection.rb', line 64

def to_ntriples
  ntriples = ""
  self.each_pair do | uri, resource |
    ntriples << resource.to_ntriples
  end
  ntriples
end

#to_xml(depth = 0) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/rdf_objects/collection.rb', line 72

def to_xml(depth=0)
  namespaces = {}
  rdf_data = ""
  self.values.each do | resource |
    ns, desc = resource.rdf_description_block(depth)
    namespaces.merge!(ns)
    rdf_data << desc
  end
  unless namespaces["xmlns:rdf"]
    if  x = namespaces.index("http://www.w3.org/1999/02/22-rdf-syntax-ns#")
      namespaces.delete(x)
    end
    namespaces["xmlns:rdf"] = "http://www.w3.org/1999/02/22-rdf-syntax-ns#"
  end

  rdf = "<rdf:RDF"
  namespaces.each_pair {|key, value| rdf << " #{key}=\"#{value}\""}
  rdf <<">"
  rdf << rdf_data
  rdf << "</rdf:RDF>"
  rdf      
end

#urisObject



3
4
5
# File 'lib/rdf_objects/collection.rb', line 3

def uris
  return self.keys
end