Class: MongoMapper::FinderOptions
- 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
- #criteria ⇒ Object
-
#initialize(model, options) ⇒ FinderOptions
constructor
A new instance of FinderOptions.
- #options ⇒ Object
- #to_a ⇒ Object
Constructor Details
#initialize(model, options) ⇒ FinderOptions
Returns a new instance of FinderOptions.
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, ) raise ArgumentError, "Options must be a hash" unless .is_a?(Hash) = .symbolize_keys @model = model @options = {} @conditions = .delete(:conditions) || {} .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
#criteria ⇒ Object
37 38 39 |
# File 'lib/mongo_mapper/finder_options.rb', line 37 def criteria to_mongo_criteria(@conditions) end |
#options ⇒ Object
41 42 43 44 45 46 47 48 |
# File 'lib/mongo_mapper/finder_options.rb', line 41 def 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_a ⇒ Object
50 51 52 |
# File 'lib/mongo_mapper/finder_options.rb', line 50 def to_a [criteria, ] end |