class Tripod::Criteria

This module defines behaviour for criteria

Attributes

extra_clauses[RW]
graph_lambdas[RW]
graph_uri[RW]
limit_clause[RW]
offset_clause[RW]
order_clause[RW]
resource_class[RW]

the resource class that this criteria is for.

where_clauses[RW]

Public Class Methods

new(resource_class) click to toggle source
# 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

==(other) click to toggle source

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
extras(sparql_snippet) click to toggle source

 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
graph(graph_uri, &block) click to toggle source

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
limit(the_limit) click to toggle source

 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
offset(the_offset) click to toggle source

 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
order(param) click to toggle source

 replaces this criteria's order clause

# File lib/tripod/criteria.rb, line 84
def order(param)
  self.order_clause = "ORDER BY #{param}"
  self
end
query_where_clauses() click to toggle source
# File lib/tripod/criteria.rb, line 59
def query_where_clauses
  where_clauses.empty? ? ['?uri ?p ?o'] : where_clauses
end
where(filter) click to toggle source

 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