Sha256: 5045c29b2413d61d859d19e0089efc6267e50a60a687e2b2c2624d0edd053ba1

Contents?: true

Size: 1.17 KB

Versions: 6

Compression:

Stored size: 1.17 KB

Contents

# a module to help the creation of solr queries.
module RSolr::Ext::Request::Queryable
  
  # Wraps a string around double quotes
  def quote(value)
    %("#{value}")
  end
  
  # builds a solr range query from a Range object
  def build_range(r)
    "[#{r.min} TO #{r.max}]"
  end
  
  # builds a solr query fragment
  # if "quote_string" is true, the values will be quoted.
  # if "value" is a string/symbol, the #to_s method is called
  # if the "value" is an array, each item in the array is 
  # send to build_query (recursive)
  # if the "value" is a Hash, a fielded query is built
  # where the keys are used as the field names and
  # the values are either processed as a Range or
  # passed back into build_query (recursive)
  def build_query(value, quote_string=false)
    case value
    when String,Symbol
      return quote_string ? quote(value.to_s) : value.to_s
    when Array
      value.collect do |v|
        build_query(v, quote_string)
      end.flatten
    when Hash
      return value.collect do |(k,v)|
        if v.is_a?(Range)
          "#{k}:#{build_range(v)}"
        else
          "#{k}:#{build_query(v, quote_string)}"
        end
      end.flatten
    end
  end
  
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
mwmitchell-rsolr-ext-0.5.3 lib/rsolr-ext/request/queryable.rb
mwmitchell-rsolr-ext-0.5.4 lib/rsolr-ext/request/queryable.rb
mwmitchell-rsolr-ext-0.5.5 lib/rsolr-ext/request/queryable.rb
mwmitchell-rsolr-ext-0.5.6 lib/rsolr-ext/request/queryable.rb
mwmitchell-rsolr-ext-0.5.7 lib/rsolr-ext/request/queryable.rb
mwmitchell-rsolr-ext-0.5.8 lib/rsolr-ext/request/queryable.rb