Sha256: f7e44c13873f39cb1c87b7bfd762b8cda551687e5a85b3082ddd44a86c27eec4

Contents?: true

Size: 1.1 KB

Versions: 3

Compression:

Stored size: 1.1 KB

Contents

module Clevic
  
  # Provides a nice way of getting to Sequel::Dataset's
  # opts[:order] information
  #
  # Including class must call dataset= before calling order_attributes
  module OrderedDataset
  
    # returns a collection of [ attribute, (1|-1) ]
    # where 1 is forward/asc (>) and -1 is backward/desc (<)
    def order_attributes
      if @order_attributes.nil?
        @order_attributes =
        dataset.opts[:order].map do |order_expr|
          case order_expr
          when Symbol
            [ order_expr, 1 ]
            
          when Sequel::SQL::OrderedExpression
            [ order_expr.expression, order_expr.descending ? -1 : 1 ]
            
          else
            raise "unknown order_expr: #{order_expr.inspect}"
          end
        end
      end
      @order_attributes
    end
  
    attr_reader :dataset
    
    # Set default dataset ordering to primary key if it doesn't
    # already have an order.
    def dataset=( other )
      @dataset =
      if other.opts[:order].nil?
        other.order( other.model.primary_key )
      else
        other
      end
    end
  
  end

end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
clevic-0.13.0.b9 lib/clevic/ordered_dataset.rb
clevic-0.13.0.b6 lib/clevic/ordered_dataset.rb
clevic-0.13.0.b5 lib/clevic/ordered_dataset.rb