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
36
37
# 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)

  @model      = model
  @options    = {}
  @conditions = {}

  options.each_pair do |key, value|
    key = key.respond_to?(:to_sym) ? key.to_sym : key
    if OptionKeys.include?(key)
      @options[key] = value
    elsif key == :conditions
      @conditions.merge!(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



39
40
41
# File 'lib/mongo_mapper/finder_options.rb', line 39

def criteria
  to_mongo_criteria(@conditions)
end

#optionsObject



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

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

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

#to_aObject



52
53
54
# File 'lib/mongo_mapper/finder_options.rb', line 52

def to_a
  [criteria, options]
end