Class: Tripod::Criteria

Inherits:
Object
  • Object
show all
Includes:
CriteriaExecution
Defined in:
lib/tripod/criteria.rb

Overview

This module defines behaviour for criteria

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from CriteriaExecution

#as_query, #count, #first, #resources, #serialize

Constructor Details

#initialize(resource_class) ⇒ Criteria

Returns a new instance of Criteria.



22
23
24
25
26
27
28
29
30
31
32
# File 'lib/tripod/criteria.rb', line 22

def initialize(resource_class)
  self.resource_class = resource_class
  self.where_clauses = []
  self.extra_clauses = []

  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

Instance Attribute Details

#extra_clausesObject

Returns the value of attribute extra_clauses.



15
16
17
# File 'lib/tripod/criteria.rb', line 15

def extra_clauses
  @extra_clauses
end

#graph_uriObject

Returns the value of attribute graph_uri.



20
21
22
# File 'lib/tripod/criteria.rb', line 20

def graph_uri
  @graph_uri
end

#limit_clauseObject

Returns the value of attribute limit_clause.



17
18
19
# File 'lib/tripod/criteria.rb', line 17

def limit_clause
  @limit_clause
end

#offset_clauseObject

Returns the value of attribute offset_clause.



19
20
21
# File 'lib/tripod/criteria.rb', line 19

def offset_clause
  @offset_clause
end

#order_clauseObject

Returns the value of attribute order_clause.



18
19
20
# File 'lib/tripod/criteria.rb', line 18

def order_clause
  @order_clause
end

#resource_classObject

the resource class that this criteria is for.



12
13
14
# File 'lib/tripod/criteria.rb', line 12

def resource_class
  @resource_class
end

#where_clausesObject

Returns the value of attribute where_clauses.



14
15
16
# File 'lib/tripod/criteria.rb', line 14

def where_clauses
  @where_clauses
end

Instance Method Details

#==(other) ⇒ Object

they’re equal if they return the same query



35
36
37
# File 'lib/tripod/criteria.rb', line 35

def ==(other)
  as_query == other.send(:as_query)
end

#extras(sparql_snippet) ⇒ Object

 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?



64
65
66
67
# File 'lib/tripod/criteria.rb', line 64

def extras(sparql_snippet)
  extra_clauses << sparql_snippet
  self
end

#graph(graph_uri) ⇒ Tripod::Criteria

Restrict htis query to the graph uri passed in

Examples:

.graph(RDF::URI.new(‘graphoid’)

.graph(‘graphoid’)

Parameters:

  • The (Stirng, RDF::URI)

    graph uri

Returns:



95
96
97
98
# File 'lib/tripod/criteria.rb', line 95

def graph(graph_uri)
  self.graph_uri = graph_uri.to_s
  self
end

#limit(the_limit) ⇒ Object

 replaces this criteria’s limit clause



70
71
72
73
# File 'lib/tripod/criteria.rb', line 70

def limit(the_limit)
  self.limit_clause = "LIMIT #{the_limit.to_s}"
  self
end

#offset(the_offset) ⇒ Object

 replaces this criteria’s offset clause



76
77
78
79
# File 'lib/tripod/criteria.rb', line 76

def offset(the_offset)
  self.offset_clause = "OFFSET #{the_offset.to_s}"
  self
end

#order(param) ⇒ Object

 replaces this criteria’s order clause



82
83
84
85
# File 'lib/tripod/criteria.rb', line 82

def order(param)
  self.order_clause = "ORDER BY #{param}"
  self
end

#query_where_clausesObject



57
58
59
# File 'lib/tripod/criteria.rb', line 57

def query_where_clauses
  where_clauses.empty? ? ['?uri ?p ?o'] : where_clauses
end

#where(filter) ⇒ Object

 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>”)



44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/tripod/criteria.rb', line 44

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