Sha256: 0cbc4da3a7623461dc871f718569c5621679ef08784d0bed527b19392bd96425

Contents?: true

Size: 884 Bytes

Versions: 9

Compression:

Stored size: 884 Bytes

Contents

module Sunspot
  class Query
    # 
    # The Sort class is a query component representing a sort by a given field.
    # 
    class Sort #:nodoc:
      ASCENDING = Set.new([:asc, :ascending])
      DESCENDING = Set.new([:desc, :descending])

      def initialize(field, direction = nil)
        @field, @direction = field, (direction || :asc).to_sym
      end

      def to_params
        { :sort => [{ @field.indexed_name.to_sym => direction_for_solr }] }
      end

      private

      def direction_for_solr
        case
        when ASCENDING.include?(@direction)
          :ascending
        when DESCENDING.include?(@direction)
          :descending
        else
          raise ArgumentError,
                "Unknown sort direction #{@direction}. Acceptable input is: #{(ASCENDING + DESCENDING).map { |input| input.inspect } * ', '}"
        end
      end
    end
  end
end

Version data entries

9 entries across 9 versions & 2 rubygems

Version Path
outoftime-sunspot-0.8.0 lib/sunspot/query/sort.rb
outoftime-sunspot-0.8.1 lib/sunspot/query/sort.rb
outoftime-sunspot-0.8.2 lib/sunspot/query/sort.rb
outoftime-sunspot-0.8.3 lib/sunspot/query/sort.rb
outoftime-sunspot-0.8.4 lib/sunspot/query/sort.rb
outoftime-sunspot-0.8.5 lib/sunspot/query/sort.rb
outoftime-sunspot-0.8.8 lib/sunspot/query/sort.rb
outoftime-sunspot-0.8.9 lib/sunspot/query/sort.rb
pallan-sunspot-0.8.3 lib/sunspot/query/sort.rb