aboutsummaryrefslogblamecommitdiffstats
path: root/lib/arel/algebra/ordering.rb
blob: c1a4ef8b70ff4aa2cb02be96ff18f1454d5ddeeb (plain) (tree)
1
2
3
4
5
6
7
8
           
                                         
                                         
 


                                              
 


                   
 






                                                                 
     
 
                             

                                
     
 
                             

                                 
     
   
module Arel
  class Ordering < Struct.new(:attribute)
    delegate :relation, :to => :attribute

    def bind(relation)
      self.class.new(attribute.bind(relation))
    end

    def to_ordering
      self
    end

    def eval(row1, row2)
      (attribute.eval(row1) <=> attribute.eval(row2)) * direction
    end

    def to_sql(formatter = Sql::OrderClause.new(relation))
      formatter.ordering self
    end
  end

  class Ascending  < Ordering
    def direction; 1 end
    def direction_sql; 'ASC' end
  end

  class Descending < Ordering
    def direction_sql; 'DESC' end
    def direction; -1 end
  end
end