Sha256: 3c0b0e3bd05484a7224c5c9ce4c22f57485188f57b1ad223b42db29686395a16

Contents?: true

Size: 1.06 KB

Versions: 10

Compression:

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

10 entries across 10 versions & 1 rubygems

Version Path
clevic-0.14.6 lib/clevic/ordered_dataset.rb
clevic-0.14.5 lib/clevic/ordered_dataset.rb
clevic-0.14.4 lib/clevic/ordered_dataset.rb
clevic-0.14.3 lib/clevic/ordered_dataset.rb
clevic-0.14.2 lib/clevic/ordered_dataset.rb
clevic-0.14.1 lib/clevic/ordered_dataset.rb
clevic-0.14.0 lib/clevic/ordered_dataset.rb
clevic-0.13.0.b12 lib/clevic/ordered_dataset.rb
clevic-0.13.0.b11 lib/clevic/ordered_dataset.rb
clevic-0.13.0.b10 lib/clevic/ordered_dataset.rb