module Garner::Mixins::Mongoid::Document

Public Class Methods

garnered_find(*args) click to toggle source

Find an object by _id, or other findable field, or by multiple findable fields, first trying to fetch from Garner’s cache.

@example Find by an id.

Garner::Mixins::Mongoid::Document.garnered_find(BSON::ObjectId.new)

@example Find by multiple id’s.

Garner::Mixins::Mongoid::Document.garnered_find(BSON::ObjectId.new, BSON::ObjectId.new)

@example Find by multiple id’s in an array.

Garner::Mixins::Mongoid::Document.garnered_find([ BSON::ObjectId.new, BSON::ObjectId.new ])

@return [ Array<Mongoid::Document>, Mongoid::Document ]

# File lib/garner/mixins/mongoid/document.rb, line 55
def self.garnered_find(*args)
  identity = Garner::Cache::Identity.new
  args.flatten.each do |arg|
    binding = identify(arg)
    identity = identity.bind(binding)
  end
  identity.key(garnered_find_args: args) do
    find(*args)
  end
end
identify(handle) click to toggle source
# File lib/garner/mixins/mongoid/document.rb, line 37
def self.identify(handle)
  Mongoid::Identity.from_class_and_handle(self, handle)
end
mongoid_superclasses() click to toggle source

Return an array of this class and all Mongoid superclasses.

@return [Array] An array of classes.

# File lib/garner/mixins/mongoid/document.rb, line 22
def self.mongoid_superclasses
  if superclass.include?(Mongoid::Document)
    [self] + superclass.mongoid_superclasses
  else
    [self]
  end
end
proxy_binding() click to toggle source

Return an object that can act as a binding on this class’s behalf.

@return [Mongoid::Document]

# File lib/garner/mixins/mongoid/document.rb, line 33
def self.proxy_binding
  _latest_by_updated_at
end

Protected Class Methods

_latest_by_updated_at() click to toggle source
# File lib/garner/mixins/mongoid/document.rb, line 72
def self._latest_by_updated_at
  # Only find the latest if we can order by :updated_at
  return nil unless fields['updated_at']
  only(:_id, :_type, :updated_at).order_by(updated_at: :desc).first
end

Public Instance Methods

identity_string() click to toggle source
# File lib/garner/mixins/mongoid/document.rb, line 12
def identity_string
  "#{self.class.name}/id=#{id}"
end
proxied_classes() click to toggle source
# File lib/garner/mixins/mongoid/document.rb, line 8
def proxied_classes
  self.class.mongoid_superclasses
end

Protected Instance Methods

_invalidate() click to toggle source
# File lib/garner/mixins/mongoid/document.rb, line 78
def _invalidate
  invalidation_strategy.apply(self)
  invalidation_strategy.apply(_root) if _root != self && Garner.config.invalidate_mongoid_root
end