Class: MongoModel::MongoOrder::Clause

Inherits:
Object
  • Object
show all
Defined in:
lib/mongomodel/support/mongo_order.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(field, order = :ascending) ⇒ Clause

Returns a new instance of Clause.



54
55
56
# File 'lib/mongomodel/support/mongo_order.rb', line 54

def initialize(field, order=:ascending)
  @field, @order = field.to_sym, order.to_sym
end

Instance Attribute Details

#fieldObject (readonly)

Returns the value of attribute field.



52
53
54
# File 'lib/mongomodel/support/mongo_order.rb', line 52

def field
  @field
end

#orderObject (readonly)

Returns the value of attribute order.



52
53
54
# File 'lib/mongomodel/support/mongo_order.rb', line 52

def order
  @order
end

Class Method Details

.parse(clause) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
# File 'lib/mongomodel/support/mongo_order.rb', line 79

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)
  else
    clause.to_mongo_order_clause if clause.respond_to?(:to_mongo_order_clause)
  end
end

Instance Method Details

#==(other) ⇒ Object Also known as: eql?



70
71
72
# File 'lib/mongomodel/support/mongo_order.rb', line 70

def ==(other)
  other.is_a?(self.class) && field == other.field && order == other.order
end

#hashObject



75
76
77
# File 'lib/mongomodel/support/mongo_order.rb', line 75

def hash
  [field, order].hash
end

#reverseObject



66
67
68
# File 'lib/mongomodel/support/mongo_order.rb', line 66

def reverse
  self.class.new(field, order == :ascending ? :descending : :ascending)
end

#to_sObject



58
59
60
# File 'lib/mongomodel/support/mongo_order.rb', line 58

def to_s
  "#{field} #{order}"
end

#to_sort(property) ⇒ Object



62
63
64
# File 'lib/mongomodel/support/mongo_order.rb', line 62

def to_sort(property)
  [property ? property.as : field.to_s, order]
end