class Tripod::Criteria
This module defines behaviour for criteria
Attributes
the resource class that this criteria is for.
Public Class Methods
# File lib/tripod/criteria.rb, line 23 def initialize(resource_class) self.resource_class = resource_class self.where_clauses = [] self.extra_clauses = [] self.graph_lambdas = [] if resource_class._RDF_TYPE self.where("?uri a <#{resource_class._RDF_TYPE.to_s}>") end self.graph_uri = resource_class._GRAPH_URI.to_s if resource_class._GRAPH_URI end
Public Instance Methods
they're equal if they return the same query
# File lib/tripod/criteria.rb, line 37 def ==(other) as_query == other.send(:as_query) end
takes a string and adds an extra clause to this criteria.
e.g. my_criteria.extras("LIMIT 10 OFFSET 20").extrass
TODO: make it also take a hash?
# File lib/tripod/criteria.rb, line 66 def extras(sparql_snippet) extra_clauses << sparql_snippet self end
Restrict this query to the graph uri passed in You may also pass a block to an unbound graph, ?g then chain a where clause to the criteria returned to bind ?g
@example .graph(RDF::URI.new('graphoid') @example .graph('graphoid') @example .graph(nil) { “?s ?p ?o” }.where(“?uri ?p ?g”)
@param [ String, RDF::URI ] The graph uri @param [ Block ] A string to be executed within an unbound graph, ?g
@return [ Tripod::Criteria
] A criteria object
# File lib/tripod/criteria.rb, line 101 def graph(graph_uri, &block) if block_given? self.graph_lambdas ||= [] self.graph_lambdas << block self else self.graph_uri = graph_uri.to_s self end end
replaces this criteria's limit clause
# File lib/tripod/criteria.rb, line 72 def limit(the_limit) self.limit_clause = "LIMIT #{the_limit.to_s}" self end
replaces this criteria's offset clause
# File lib/tripod/criteria.rb, line 78 def offset(the_offset) self.offset_clause = "OFFSET #{the_offset.to_s}" self end
replaces this criteria's order clause
# File lib/tripod/criteria.rb, line 84 def order(param) self.order_clause = "ORDER BY #{param}" self end
# File lib/tripod/criteria.rb, line 59 def query_where_clauses where_clauses.empty? ? ['?uri ?p ?o'] : where_clauses end
Takes a string and adds a where clause to this criteria.
Returns a criteria object. Note: the subject being returned by the query must be identified by ?uri
e.g. my_criteria.where(“?uri a <my-type>”)
# File lib/tripod/criteria.rb, line 46 def where(filter) if filter.is_a?(String) # we got a Sparql snippet where_clauses << filter elsif filter.is_a?(Hash) filter.each_pair do |key, value| field = resource_class.get_field(key) value = RDF::Literal.new(value) unless value.respond_to?(:to_base) where_clauses << "?uri <#{ field.predicate }> #{ value.to_base }" end end self end