Class: MongoMapper::FinderOptions
- Defined in:
- lib/mongomapper/finder_options.rb
Instance Attribute Summary collapse
-
#options ⇒ Object
readonly
Returns the value of attribute options.
Class Method Summary collapse
- .to_mongo_criteria(conditions, parent_key = nil) ⇒ Object
- .to_mongo_fields(fields) ⇒ Object
- .to_mongo_options(options) ⇒ Object
- .to_mongo_sort(sort) ⇒ Object
- .to_mongo_sort_piece(str) ⇒ Object
Instance Method Summary collapse
- #criteria ⇒ Object
-
#initialize(options) ⇒ FinderOptions
constructor
A new instance of FinderOptions.
- #to_a ⇒ Object
Constructor Details
#initialize(options) ⇒ FinderOptions
Returns a new instance of FinderOptions.
36 37 38 39 40 |
# File 'lib/mongomapper/finder_options.rb', line 36 def initialize() raise ArgumentError, "FinderOptions must be a hash" unless .is_a?(Hash) = .symbolize_keys @conditions = .delete(:conditions) || {} end |
Instance Attribute Details
#options ⇒ Object (readonly)
Returns the value of attribute options.
3 4 5 |
# File 'lib/mongomapper/finder_options.rb', line 3 def end |
Class Method Details
.to_mongo_criteria(conditions, parent_key = nil) ⇒ Object
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/mongomapper/finder_options.rb', line 5 def self.to_mongo_criteria(conditions, parent_key=nil) criteria = {} conditions.each_pair do |field, value| case value when Array operator_present = field.to_s =~ /^\$/ criteria[field] = if operator_present value else {'$in' => value} end when Hash criteria[field] = to_mongo_criteria(value, field) else criteria[field] = value end end criteria end |
.to_mongo_fields(fields) ⇒ Object
55 56 57 58 59 60 61 62 63 |
# File 'lib/mongomapper/finder_options.rb', line 55 def self.to_mongo_fields(fields) return if fields.blank? if fields.is_a?(String) fields.split(',').map { |field| field.strip } else fields.flatten.compact end end |
.to_mongo_options(options) ⇒ Object
26 27 28 29 30 31 32 33 34 |
# File 'lib/mongomapper/finder_options.rb', line 26 def self.() = .dup { :fields => to_mongo_fields(.delete(:fields) || .delete(:select)), :offset => (.delete(:offset) || 0).to_i, :limit => (.delete(:limit) || 0).to_i, :sort => .delete(:sort) || to_mongo_sort(.delete(:order)) } end |
.to_mongo_sort(sort) ⇒ Object
65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/mongomapper/finder_options.rb', line 65 def self.to_mongo_sort(sort) return if sort.blank? pieces = sort.split(',') pairs = pieces.map { |s| to_mongo_sort_piece(s) } hash = OrderedHash.new pairs.each do |pair| field, sort_direction = pair hash[field] = sort_direction end hash.symbolize_keys end |
.to_mongo_sort_piece(str) ⇒ Object
78 79 80 81 82 83 |
# File 'lib/mongomapper/finder_options.rb', line 78 def self.to_mongo_sort_piece(str) field, direction = str.strip.split(' ') direction ||= 'ASC' direction = direction.upcase == 'ASC' ? 1 : -1 [field, direction] end |