Class: MongoMapper::FinderOptions

Inherits:
Object
  • Object
show all
Defined in:
lib/mongo_mapper/finder_options.rb

Overview

Important Note

This class is private to MongoMapper and should not be considered part of MongoMapper’s public API.

Constant Summary collapse

OptionKeys =
[:fields, :select, :skip, :offset, :limit, :sort, :order]

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model, options) ⇒ FinderOptions

Returns a new instance of FinderOptions.

Raises:

  • (ArgumentError)


18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/mongo_mapper/finder_options.rb', line 18

def initialize(model, options)
  raise ArgumentError, "Options must be a hash" unless options.is_a?(Hash)
  options = options.symbolize_keys

  @model      = model
  @options    = {}
  @conditions = options.delete(:conditions) || {}

  options.each_pair do |key, value|
    if OptionKeys.include?(key)
      @options[key] = value
    else
      @conditions[key] = value
    end
  end

  add_sci_scope
end

Class Method Details

.normalized_field(field) ⇒ Object



9
10
11
# File 'lib/mongo_mapper/finder_options.rb', line 9

def self.normalized_field(field)
  field.to_s == 'id' ? :_id : field
end

.normalized_order_direction(direction) ⇒ Object



13
14
15
16
# File 'lib/mongo_mapper/finder_options.rb', line 13

def self.normalized_order_direction(direction)
  direction ||= 'ASC'
  direction.upcase == 'ASC' ? 1 : -1
end

Instance Method Details

#criteriaObject



37
38
39
# File 'lib/mongo_mapper/finder_options.rb', line 37

def criteria
  to_mongo_criteria(@conditions)
end

#optionsObject



41
42
43
44
45
46
47
48
# File 'lib/mongo_mapper/finder_options.rb', line 41

def options
  fields = @options.delete(:fields) || @options.delete(:select)
  skip   = @options.delete(:skip)   || @options.delete(:offset) || 0
  limit  = @options.delete(:limit)  || 0
  sort   = @options.delete(:sort)   || convert_order_to_sort(@options.delete(:order))

  {:fields => to_mongo_fields(fields), :skip => skip.to_i, :limit => limit.to_i, :sort => sort}
end

#to_aObject



50
51
52
# File 'lib/mongo_mapper/finder_options.rb', line 50

def to_a
  [criteria, options]
end