class Solr4R::Query
Constants
- LOCAL_PARAMS_KEY
Attributes
escape[W]
query[R]
Public Class Methods
convert_value(value, escape = true)
click to toggle source
# File lib/solr4r/query.rb, line 51 def convert_value(value, escape = true) case value when DateTime convert_value(value.to_time, escape) when Time value.getutc.xmlschema.tap { |string| string.gsub!(/:/, '\\\&') if escape } when Range '[' << [value.begin, value.end].map { |v| convert_value(v, false) }.join(' TO ') << ']' else value.to_s end end
escape(string, escape = true)
click to toggle source
# File lib/solr4r/query.rb, line 43 def escape(string, escape = true) escape ? string.gsub('&', '%26') : string end
local_params_string(*args)
click to toggle source
# File lib/solr4r/query.rb, line 39 def local_params_string(*args) new.local_params_string(*args) end
new(query = '', escape = true)
click to toggle source
# File lib/solr4r/query.rb, line 68 def initialize(query = '', escape = true) @query, @escape = query, escape end
query_string(*args)
click to toggle source
# File lib/solr4r/query.rb, line 35 def query_string(*args) new(*args).to_s end
quote(string)
click to toggle source
# File lib/solr4r/query.rb, line 47 def quote(string) string =~ /\s/ ? %Q{"#{string}"} : string end
Public Instance Methods
convert_value(*args)
click to toggle source
# File lib/solr4r/query.rb, line 88 def convert_value(*args) self.class.convert_value(*args) end
escape(string, escape = escape?)
click to toggle source
# File lib/solr4r/query.rb, line 80 def escape(string, escape = escape?) self.class.escape(string, escape) end
escape?()
click to toggle source
# File lib/solr4r/query.rb, line 76 def escape? @escape end
local_params_string(local_params, hash = {}, escape = escape?)
click to toggle source
# File lib/solr4r/query.rb, line 118 def local_params_string(local_params, hash = {}, escape = escape?) case local_params = expand_local_params(local_params, hash) when nil # ignore when String escape("{!#{local_params}}", escape) unless local_params.empty? when Array local_params_string(local_params.join(' '), {}, escape) when Hash local_params_string(local_params.map { |key, value| "#{key}=#{quote(value)}" }, {}, escape) else type_error(local_params) end end
query_string(query = query(), escape = escape?)
click to toggle source
# File lib/solr4r/query.rb, line 96 def query_string(query = query(), escape = escape?) case query when nil # ignore when String escape(query, escape) unless query.empty? when Array if query.last.is_a?(Hash) query = query.dup local_params, query_string = query_from_hash(query.pop, escape) query << query_string if query_string end query_with_params(local_params, query_from_array(query, escape)) when Hash query_with_params(*query_from_hash(query, escape)) else type_error(query) end end
quote(string)
click to toggle source
# File lib/solr4r/query.rb, line 84 def quote(string) self.class.quote(string) end
to_s(escape = escape?)
click to toggle source
# File lib/solr4r/query.rb, line 92 def to_s(escape = escape?) query_string(query, escape) end
Private Instance Methods
expand_local_params(local_params, hash)
click to toggle source
# File lib/solr4r/query.rb, line 159 def expand_local_params(local_params, hash) case type = hash[:type] when nil local_params when String, Symbol type_error(local_params) unless local_params.is_a?(Array) hash = hash.dup local_params.each { |param| hash[param] = "$#{type}.#{param}" } hash else type_error(type) end end
query_from_array(query, escape)
click to toggle source
# File lib/solr4r/query.rb, line 136 def query_from_array(query, escape) query_string(query.map { |value| convert_value(value) }.join(' '), escape) end
query_from_hash(query, escape)
click to toggle source
# File lib/solr4r/query.rb, line 140 def query_from_hash(query, escape) if query.key?(LOCAL_PARAMS_KEY) query = query.dup local_params = local_params_string( query.delete(LOCAL_PARAMS_KEY), {}, escape) end query = query.flat_map { |key, values| block = lambda { |value| "#{key}:#{convert_value(value)}" } values.respond_to?(:to_ary) ? values.map(&block) : block[values] } [local_params, query_string(query, escape)] end
query_with_params(local_params, query_string)
click to toggle source
# File lib/solr4r/query.rb, line 155 def query_with_params(local_params, query_string) local_params ? local_params + query_string : query_string end
type_error(obj)
click to toggle source
# File lib/solr4r/query.rb, line 174 def type_error(obj) raise TypeError, "unexpected type #{obj.class}", caller(1) end