Module: Tripod::Finders::ClassMethods
- Defined in:
- lib/tripod/finders.rb
Instance Method Summary collapse
-
#_create_and_hydrate_resources(uris_and_graphs) ⇒ Object
create and hydrate the resources identified in uris_and_graphs.
-
#_select_uris_and_graphs(sparql, opts = {}) ⇒ Object
based on the query passed in, build a hash of uris->graphs.
-
#all ⇒ Object
execute a query to return all objects (restricted by this class’s rdf_type if specified) returns a criteria object.
- #count ⇒ Object
-
#describe_uris(uris) ⇒ Object
returns a graph of triples which describe the uris passed in.
-
#find(uri, graph_uri = nil) ⇒ Resource
Find a
Resource
by its uri (and, optionally, by its graph if there are more than one). -
#find_by_sparql(sparql_query, opts = {}) ⇒ Array
Find a collection of Resources by a SPARQL select statement which returns their uris.
- #first ⇒ Object
-
#where(sparql_snippet) ⇒ Object
execute a where clause on this resource.
Instance Method Details
#_create_and_hydrate_resources(uris_and_graphs) ⇒ Object
create and hydrate the resources identified in uris_and_graphs. Note: if any of the graphs are not set, those resources can still be constructed, but not persisted back to DB.
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 |
# File 'lib/tripod/finders.rb', line 115 def _create_and_hydrate_resources(uris_and_graphs) graph = describe_uris(uris_and_graphs.keys) #uses the resource_class on the criteria object repo = add_data_to_repository(graph) resources = [] uris_and_graphs.each_pair do |u,g| # instantiate a new resource r = self.new(u,g) # make a graph of data for this resource's uri data_graph = RDF::Graph.new repo.query( [RDF::URI.new(u), :predicate, :object] ) do |statement| data_graph << statement end # use it to hydrate this resource r.hydrate!(:graph => data_graph) r.new_record = false resources << r end resources end |
#_select_uris_and_graphs(sparql, opts = {}) ⇒ Object
based on the query passed in, build a hash of uris->graphs
149 150 151 152 153 154 155 156 157 158 159 160 161 |
# File 'lib/tripod/finders.rb', line 149 def _select_uris_and_graphs(sparql, opts={}) select_results = Tripod::SparqlClient::Query.select(sparql) uris_and_graphs = {} select_results.each do |r| uri_variable = opts[:uri_variable] || 'uri' graph_variable = opts[:graph_variable] || 'graph' uris_and_graphs[ r[uri_variable]["value"] ] = r[graph_variable]["value"] end uris_and_graphs end |
#all ⇒ Object
execute a query to return all objects (restricted by this class’s rdf_type if specified) returns a criteria object
78 79 80 |
# File 'lib/tripod/finders.rb', line 78 def all where('?uri ?p ?o') end |
#count ⇒ Object
82 83 84 |
# File 'lib/tripod/finders.rb', line 82 def count self.all.count end |
#describe_uris(uris) ⇒ Object
returns a graph of triples which describe the uris passed in.
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/tripod/finders.rb', line 91 def describe_uris(uris) graph = RDF::Graph.new if uris.length > 0 uris_sparql_str = uris.map{ |u| "<#{u.to_s}>" }.join(" ") # Do a big describe statement, and read the results into an in-memory repo triples_string = Tripod::SparqlClient::Query.query("DESCRIBE #{uris_sparql_str}", "application/n-triples") RDF::Reader.for(:ntriples).new(triples_string) do |reader| reader.each_statement do |statement| graph << statement end end end graph end |
#find(uri, graph_uri = nil) ⇒ Resource
Find a Resource
by its uri (and, optionally, by its graph if there are more than one).
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/tripod/finders.rb', line 24 def find(uri, graph_uri=nil) unless graph_uri # do a quick select to see what graph to use. select_query = "SELECT ?g WHERE { GRAPH ?g {<#{uri.to_s}> ?p ?o } } LIMIT 1" result = Tripod::SparqlClient::Query.select(select_query) if result.length > 0 graph_uri = result[0]["g"]["value"] else raise Tripod::Errors::ResourceNotFound.new(uri) end end # instantiate and hydrate the resource resource = self.new(uri, graph_uri.to_s) resource.hydrate! resource.new_record = false # check that there are triples for the resource (catches case when someone has deleted data # between our original check for the graph and hydrating the object. raise Tripod::Errors::ResourceNotFound.new(uri) if resource.repository.empty? # return the instantiated, hydrated resource resource end |
#find_by_sparql(sparql_query, opts = {}) ⇒ Array
Find a collection of Resources by a SPARQL select statement which returns their uris. Under the hood, this only executes two queries: a select, then a describe.
64 65 66 67 |
# File 'lib/tripod/finders.rb', line 64 def find_by_sparql(sparql_query, opts={}) uris_and_graphs = _select_uris_and_graphs(sparql_query, opts) _create_and_hydrate_resources(uris_and_graphs) end |
#first ⇒ Object
86 87 88 |
# File 'lib/tripod/finders.rb', line 86 def first self.all.first end |