Skip to content

Instantly share code, notes, and snippets.

@moustaki
Created July 5, 2012 12:50
Show Gist options
  • Save moustaki/3053508 to your computer and use it in GitHub Desktop.
Save moustaki/3053508 to your computer and use it in GitHub Desktop.
# Monkey-patching sparql-client to add count support
module SPARQL; class Client
class Query < RDF::Query
def self.count(*variables)
options = variables.last.is_a?(Hash) ? variables.pop : {}
unless variables.size == 1 and variables.first.is_a?(Symbol)
raise Exception.new "Only one symbol must be provided for count"
end
self.new(:select, options).select(:count => variables.first)
end
end
def count(*args)
call_query_method(:count, *args)
end
end; end
class RDF::Query
class Variable
def initialize(name = nil, value = nil)
@count = false
if name.is_a?(Hash) and name.has_key?(:count)
@count = true
name = name[:count]
end
@name = (name || "g#{__id__.to_i.abs}").to_sym
@value = value
end
def count?
@count
end
def to_s
prefix = distinguished? ? '?' : "??"
var_s = "#{prefix}#{name}"
var_s = "(COUNT(#{var_s}) AS ?count)" if count?
unbound? ? var_s : "#{var_s}=#{value}"
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment