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 36 37 |
# File 'lib/mongo_mapper/finder_options.rb', line 18 def initialize(model, ) raise ArgumentError, "Options must be a hash" unless .is_a?(Hash) @model = model @options = {} @conditions = {} .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
#criteria ⇒ Object
39 40 41 |
# File 'lib/mongo_mapper/finder_options.rb', line 39 def criteria to_mongo_criteria(@conditions) end |
#options ⇒ Object
43 44 45 46 47 48 49 50 |
# File 'lib/mongo_mapper/finder_options.rb', line 43 def 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_a ⇒ Object
52 53 54 |
# File 'lib/mongo_mapper/finder_options.rb', line 52 def to_a [criteria, ] end |