Sha256: 3e26062d056cd6ada355ee2cf92906d1a0865f82042672eebce878fe6cba7cbe

Contents?: true

Size: 1.16 KB

Versions: 12

Compression:

Stored size: 1.16 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
      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

12 entries across 12 versions & 1 rubygems

Version Path
mwmitchell-rsolr-ext-0.5.9 lib/rsolr-ext/request/queryable.rb
mwmitchell-rsolr-ext-0.5.95 lib/rsolr-ext/request/queryable.rb
mwmitchell-rsolr-ext-0.6.0 lib/rsolr-ext/request/queryable.rb
mwmitchell-rsolr-ext-0.6.1 lib/rsolr-ext/request/queryable.rb
mwmitchell-rsolr-ext-0.7.0 lib/rsolr-ext/request/queryable.rb
mwmitchell-rsolr-ext-0.7.1 lib/rsolr-ext/request/queryable.rb
mwmitchell-rsolr-ext-0.7.10 lib/rsolr-ext/request/queryable.rb
mwmitchell-rsolr-ext-0.7.5 lib/rsolr-ext/request/queryable.rb
mwmitchell-rsolr-ext-0.7.6 lib/rsolr-ext/request/queryable.rb
mwmitchell-rsolr-ext-0.7.7 lib/rsolr-ext/request/queryable.rb
mwmitchell-rsolr-ext-0.7.8 lib/rsolr-ext/request/queryable.rb
mwmitchell-rsolr-ext-0.7.9 lib/rsolr-ext/request/queryable.rb