Project

mongoload

0.0
No commit activity in last 3 years
No release in over 3 years
Automatic Mongoid eager loading
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 1.10
>= 0
~> 11.0
~> 4.0
~> 3.0

Runtime

>= 5.0.0
 Project Readme

Mongoload

Gem Version Build Status

Mongoload is a gem to perform eager loading automatically for Mongoid, inspired by Goldiloader.

Usage

Just install Mongoload. It will automatically eager load your reference relation the first time you access it.

Install

Add this line to your application's Gemfile:

gem 'mongoload'

Or install it yourself as:

$ gem install mongoload

Options

:auto_include

Mark if Mongoload should perform eager loading on the relation. Pass :auto_include option to the definition, the value should be true(by default) or false.

class User
  include Mongoid::Document
  has_one :device, auto_include: false # Do not perform automatic eager loading on :device relation
  has_many :posts

  field :username
end

# Following call will not trigger automatic eager loading on device
User.all.each(&:device)

:fully_load

Mark if Mongoload should perform eager loading on the relation, when accessed by following methods:

  • #first
  • #last
  • #size
  • #empty?

Pass :fully_load option to the definition, the value should be true or false(by default).

class Tag
  include Mongoid::Document
  has_and_belongs_to_many :posts, fully_load: true

  field :name
end

# Following calls will trigger automatic eager loading on posts
Tag.all.each { |tag| tag.posts.first }
Tag.all.each { |tag| tag.posts.last }
Tag.all.each { |tag| tag.posts.size }
Tag.all.each { |tag| tag.posts.empty? }

PS: As for #second, #third, #fourth, #fifth and #forty_two calls, mongoid will delegate them to the entries of a relation enumerable set, which makes them just same as a #to_a call.

Copyright

Copyright (c) 2016 Kaloku Sang

See LICENSE.txt for details.