Class: MongoMapper::FinderOptions

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

Overview

Controls the parsing and handling of options used by finders.

Important Note

This class is private to MongoMapper and should not be considered part of MongoMapper’s public API. Some documentation herein, however, may prove useful for understanding how MongoMapper handles the parsing of finder conditions and options.

Constant Summary collapse

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

Instance Method Summary collapse

Constructor Details

#initialize(model, options) ⇒ FinderOptions

Returns a new instance of FinderOptions.

Raises:

  • (ArgumentError)


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

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

Instance Method Details

#criteriaHash

Returns Mongo compatible criteria options.

Returns:

  • (Hash)

    Mongo compatible criteria options

See Also:

  • #to_mongo_criteria


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

def criteria
  to_mongo_criteria(@conditions)
end

#optionsHash

Returns Mongo compatible options.

Returns:

  • (Hash)

    Mongo compatible options



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

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_aArray<Hash>

Returns Mongo criteria and options enclosed in an Array.

Returns:

  • (Array<Hash>)

    Mongo criteria and options enclosed in an Array



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

def to_a
  [criteria, options]
end