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.



23
24
25
26
27
28
29
30
31
32
33
34
# 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

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_lambdasObject

Returns the value of attribute graph_lambdas.



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

def graph_lambdas
  @graph_lambdas
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



37
38
39
# File 'lib/tripod/criteria.rb', line 37

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?



66
67
68
69
# File 'lib/tripod/criteria.rb', line 66

def extras(sparql_snippet)
  extra_clauses << sparql_snippet
  self
end

#graph(graph_uri, &block) ⇒ Tripod::Criteria

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

Examples:

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

.graph(‘graphoid’)

.graph(nil) { “?s ?p ?o” }.where(“?uri ?p ?g”)

Parameters:

  • The (String, RDF::URI)

    graph uri

  • A (Block)

    string to be executed within an unbound graph, ?g

Returns:



101
102
103
104
105
106
107
108
109
110
111
# 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) ⇒ Object

 replaces this criteria’s limit clause



72
73
74
75
# File 'lib/tripod/criteria.rb', line 72

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

#offset(the_offset) ⇒ Object

 replaces this criteria’s offset clause



78
79
80
81
# File 'lib/tripod/criteria.rb', line 78

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

#order(param) ⇒ Object

 replaces this criteria’s order clause



84
85
86
87
# File 'lib/tripod/criteria.rb', line 84

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

#query_where_clausesObject



59
60
61
# File 'lib/tripod/criteria.rb', line 59

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



46
47
48
49
50
51
52
53
54
55
56
57
# 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