module Poro::Contexts::MongoContext::FinderMethods

A mixin of MongoDB finder method implementations.

Public Instance Methods

data_store_cursor(opts) { |plain_object| ... } click to toggle source

Runs the given find parameters on MongoDB and returns a Mongo::Cursor object. Note that you must manually convert the results using this Context’s convert_to_plain_object(obj) method or you will get raw Mongo objects.

If a block is given, the cursor is automatically iterated over via the each method, but with the results pre-converterd. Note that the result set can change out from under you on an active system if you iterate in this way. Additionally, the returned cursor has been rewound, which means it may find different results!

This method is useful if you need to do something special, like only get one result at a time to save on memory.

WARNING: Even though the method currently does no filtering of the conditions, allowing advanced queries will work, in the future this may not be the case. If your query needs to do more than a simple query, it is better to use data_store_find_all.

# File lib/poro/contexts/mongo_context.rb, line 515
def data_store_cursor(opts) # :yields: plain_object
  find_opts = mongoize_find_opts(opts)
  cursor = data_store.find(opts[:conditions], find_opts)
  
  if( block_given? )
    cursor.each do |doc|
      plain_object = self.convert_to_plain_object(doc)
      yield(plain_object)
    end
    cursor.rewind!
  end
  
  return cursor
end

Private Instance Methods

data_store_find_all(*args, &block) click to toggle source
# File lib/poro/contexts/mongo_context.rb, line 542
def data_store_find_all(*args, &block)
  objects = data_store.find(*args, &block).to_a.map {|doc| self.convert_to_plain_object(doc)}
  objects.each {|obj| callback_event(:after_fetch, obj)}
  return objects
end
data_store_find_first(*args, &block) click to toggle source
# File lib/poro/contexts/mongo_context.rb, line 548
def data_store_find_first(*args, &block)
  obj = self.convert_to_plain_object( data_store.find_one(*args, &block) )
  callback_event(:after_fetch, obj)
  return obj
end
find_all(opts) click to toggle source
# File lib/poro/contexts/mongo_context.rb, line 532
def find_all(opts)
  find_opts = mongoize_find_opts(opts)
  return data_store_find_all(opts[:conditions], find_opts)
end
find_first(opts) click to toggle source
# File lib/poro/contexts/mongo_context.rb, line 537
def find_first(opts)
  find_opts = mongoize_find_opts(opts)
  return data_store_find_first(opts[:conditions], find_opts)
end
mongoize_find_opts(opts) click to toggle source

Takes find opts, runs them through clean_find_opts, and then converts them to Mongo’s find opts.

# File lib/poro/contexts/mongo_context.rb, line 556
def mongoize_find_opts(opts)
  opts = clean_find_opts(opts)
  
  find_opts = {}
  
  find_opts[:limit] = opts[:limit][:limit] if opts[:limit] && opts[:limit][:limit]
  find_opts[:offset] = opts[:limit][:skip] if opts[:limit] && opts[:limit][:skip]
  
  find_opts[:sort] = opts[:order].inject([]) {|a,(k,d)| a << [k, (d == :desc ? Mongo::DESCENDING : Mongo::ASCENDING)]} if opts[:order]
  
  return find_opts
end