Class: Sasquatch::SparqlBuilder
- Inherits:
-
SPARQL::Client::Query
- Object
- SPARQL::Client::Query
- Sasquatch::SparqlBuilder
- Defined in:
- lib/sasquatch/sparql_builder.rb
Instance Attribute Summary collapse
-
#store ⇒ Object
readonly
Returns the value of attribute store.
-
#variables ⇒ Object
readonly
Returns the value of attribute variables.
Class Method Summary collapse
Instance Method Summary collapse
- #ask ⇒ Object
- #ask!(graph = :default) ⇒ Object
- #construct ⇒ Object
- #construct!(graph = :default) ⇒ Object
- #describe ⇒ Object
- #describe!(graph = :default) ⇒ Object
- #execute(graph) ⇒ Object
-
#initialize(store, options = {}, &block) ⇒ SparqlBuilder
constructor
A new instance of SparqlBuilder.
- #select ⇒ Object
- #select!(graph = :default) ⇒ Object
- #set_variables(*vars) ⇒ Object
- #sparql ⇒ Object
-
#to_s ⇒ String
Returns the string representation of this query.
Constructor Details
#initialize(store, options = {}, &block) ⇒ SparqlBuilder
Returns a new instance of SparqlBuilder.
9 10 11 12 13 |
# File 'lib/sasquatch/sparql_builder.rb', line 9 def initialize(store, ={}, &block) @store = store super(:sparql, , &block) end |
Instance Attribute Details
#store ⇒ Object (readonly)
Returns the value of attribute store.
3 4 5 |
# File 'lib/sasquatch/sparql_builder.rb', line 3 def store @store end |
#variables ⇒ Object (readonly)
Returns the value of attribute variables.
3 4 5 |
# File 'lib/sasquatch/sparql_builder.rb', line 3 def variables @variables end |
Class Method Details
.init(store, *variables) ⇒ Object
5 6 7 8 |
# File 'lib/sasquatch/sparql_builder.rb', line 5 def self.init(store, *variables) = variables.last.is_a?(Hash) ? variables.pop : {} self.new(store, ).set_variables(*variables) end |
Instance Method Details
#ask ⇒ Object
53 54 55 56 |
# File 'lib/sasquatch/sparql_builder.rb', line 53 def ask @form = :ask self end |
#ask!(graph = :default) ⇒ Object
48 49 50 51 |
# File 'lib/sasquatch/sparql_builder.rb', line 48 def ask!(graph=:default) ask execute(graph) end |
#construct ⇒ Object
63 64 65 66 67 |
# File 'lib/sasquatch/sparql_builder.rb', line 63 def construct @form = :construct [:template] = build_patterns(*@variables) self end |
#construct!(graph = :default) ⇒ Object
58 59 60 61 |
# File 'lib/sasquatch/sparql_builder.rb', line 58 def construct!(graph=:default) construct execute(graph) end |
#describe ⇒ Object
29 30 31 32 33 34 35 |
# File 'lib/sasquatch/sparql_builder.rb', line 29 def describe @form = :describe @values = *@variables.flatten.map { |var| [var, var.is_a?(RDF::URI) ? var : RDF::Query::Variable.new(var)] } self end |
#describe!(graph = :default) ⇒ Object
24 25 26 27 |
# File 'lib/sasquatch/sparql_builder.rb', line 24 def describe!(graph=:default) describe execute(graph) end |
#execute(graph) ⇒ Object
69 70 71 |
# File 'lib/sasquatch/sparql_builder.rb', line 69 def execute(graph) @store.send(:"sparql_#{@form}", self.to_s, graph) end |
#select ⇒ Object
42 43 44 45 46 |
# File 'lib/sasquatch/sparql_builder.rb', line 42 def select @form = :select @values = @variables.flatten.map { |var| [var, RDF::Query::Variable.new(var)] } self end |
#select!(graph = :default) ⇒ Object
37 38 39 40 |
# File 'lib/sasquatch/sparql_builder.rb', line 37 def select!(graph=:default) select execute(graph) end |
#set_variables(*vars) ⇒ Object
20 21 22 23 |
# File 'lib/sasquatch/sparql_builder.rb', line 20 def set_variables(*vars) @variables = vars self end |
#sparql ⇒ Object
15 16 17 18 |
# File 'lib/sasquatch/sparql_builder.rb', line 15 def sparql @form = "CHANGEME" self end |
#to_s ⇒ String
Returns the string representation of this query.
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 |
# File 'lib/sasquatch/sparql_builder.rb', line 77 def to_s buffer = [form.to_s.upcase] case form when :select, :describe buffer << 'DISTINCT' if [:distinct] buffer << 'REDUCED' if [:reduced] buffer << (values.empty? ? '*' : values.map { |v| serialize_value(v[1]) }.join(' ')) when :construct buffer << '{' buffer += serialize_patterns([:template]) buffer << '}' end buffer << "FROM #{serialize_value([:from])}" if [:from] unless (patterns.empty? && ([:optionals].nil? || [:optionals].empty?)) && form == :describe buffer << 'WHERE {' buffer += serialize_patterns(patterns) if [:optionals] [:optionals].each do |patterns| buffer << 'OPTIONAL {' buffer += serialize_patterns(patterns) buffer << '}' end end if [:filters] buffer += [:filters].map { |filter| "FILTER(#{filter})" } end buffer << '}' end if [:order_by] buffer << 'ORDER BY' buffer += [:order_by].map { |var| var.is_a?(String) ? var : "?#{var}" } end buffer << "OFFSET #{[:offset]}" if [:offset] buffer << "LIMIT #{[:limit]}" if [:limit] [:prefixes].reverse.each {|e| buffer.unshift("PREFIX #{e}") } if [:prefixes] buffer.join(' ') end |