Sha256: 78e9d4cad93c6f7c5af9531873e61a87766cc6318a58fa9a63e146240b4ed9e1

Contents?: true

Size: 1.64 KB

Versions: 26

Compression:

Stored size: 1.64 KB

Contents

module MongoModel
  class MongoOrder
    attr_reader :clauses
    
    def initialize(*clauses)
      @clauses = clauses
    end
    
    def to_a
      clauses
    end
    
    def to_s
      clauses.map { |c| c.to_s }.join(', ')
    end
    
    def to_sort(model)
      clauses.map { |c| c.to_sort(model.properties[c.field]) }
    end
    
    def ==(other)
      other.is_a?(self.class) && clauses == other.clauses
    end
    
    def reverse
      self.class.new(*clauses.map { |c| c.reverse })
    end
    
    def self.parse(order)
      case order
      when MongoOrder
        order
      when Clause
        new(order)
      when Symbol
        new(Clause.new(order))
      when String
        new(*order.split(',').map { |c| Clause.parse(c) })
      when Array
        new(*order.map { |c| Clause.parse(c) })
      end
    end
    
    class Clause
      attr_reader :field, :order
      
      def initialize(field, order=:ascending)
        @field, @order = field.to_sym, order.to_sym
      end
      
      def to_s
        "#{field} #{order}"
      end
      
      def to_sort(property)
        [property ? property.as : field.to_s, order]
      end
      
      def reverse
        self.class.new(field, order == :ascending ? :descending : :ascending)
      end
      
      def ==(other)
        other.is_a?(self.class) && field == other.field && order == other.order
      end
      
      def self.parse(clause)
        case clause
        when Clause
          clause
        when String, Symbol
          field, order = clause.to_s.strip.split(/ /)
          new(field, order =~ /^desc/i ? :descending : :ascending)
        end
      end
    end
  end
end

Version data entries

26 entries across 26 versions & 1 rubygems

Version Path
mongomodel-0.3.4 lib/mongomodel/support/mongo_order.rb
mongomodel-0.3.3 lib/mongomodel/support/mongo_order.rb
mongomodel-0.3.2 lib/mongomodel/support/mongo_order.rb
mongomodel-0.3.1 lib/mongomodel/support/mongo_order.rb
mongomodel-0.3.0 lib/mongomodel/support/mongo_order.rb
mongomodel-0.2.20 lib/mongomodel/support/mongo_order.rb
mongomodel-0.2.19 lib/mongomodel/support/mongo_order.rb
mongomodel-0.2.18 lib/mongomodel/support/mongo_order.rb
mongomodel-0.2.17 lib/mongomodel/support/mongo_order.rb
mongomodel-0.2.16 lib/mongomodel/support/mongo_order.rb
mongomodel-0.2.15 lib/mongomodel/support/mongo_order.rb
mongomodel-0.2.14 lib/mongomodel/support/mongo_order.rb
mongomodel-0.2.13 lib/mongomodel/support/mongo_order.rb
mongomodel-0.2.12 lib/mongomodel/support/mongo_order.rb
mongomodel-0.2.11 lib/mongomodel/support/mongo_order.rb
mongomodel-0.2.10 lib/mongomodel/support/mongo_order.rb
mongomodel-0.2.9 lib/mongomodel/support/mongo_order.rb
mongomodel-0.2.8 lib/mongomodel/support/mongo_order.rb
mongomodel-0.2.7 lib/mongomodel/support/mongo_order.rb
mongomodel-0.2.6 lib/mongomodel/support/mongo_order.rb